Skip to content

Commit c66168c

Browse files
committed
Create package
0 parents  commit c66168c

File tree

14 files changed

+622
-0
lines changed

14 files changed

+622
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[{package.json,*.scss,*.css}]
15+
indent_size = 2
16+
17+
[*.md]
18+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
.idea
3+
composer.lock

.scrutinizer.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
filter:
2+
excluded_paths:
3+
- "tests/"
4+
checks:
5+
php:
6+
code_rating: true
7+
remove_extra_empty_lines: true
8+
remove_php_closing_tag: true
9+
remove_trailing_whitespace: true
10+
fix_use_statements:
11+
remove_unused: true
12+
preserve_multiple: false
13+
preserve_blanklines: true
14+
order_alphabetically: true
15+
fix_php_opening_tag: true
16+
fix_linefeed: true
17+
fix_line_ending: true
18+
fix_identation_4spaces: true
19+
fix_doc_comments: true
20+
build:
21+
tests:
22+
override:
23+
-
24+
command: 'vendor/bin/phpunit --coverage-clover=coverage.clover'
25+
coverage:
26+
file: 'coverage.clover'
27+
format: 'clover'

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
sudo: false
9+
10+
cache:
11+
directories:
12+
- $HOME/.composer/cache
13+
14+
before_script:
15+
- travis_retry composer self-update
16+
- travis_retry composer update --no-interaction
17+
18+
script:
19+
- vendor/bin/phpunit

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All Notable changes to `RouteKeyExists` will be documented in this file.
4+
5+
## 1.0.0 (2017-10-25)
6+
7+
- Version 1.0.0 of `RouteKeyExists`

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2017 Ivan Vermeyen (<ivan@codezero.be>)
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Laravel Route Key Exists
2+
3+
[![GitHub release](https://img.shields.io/github/release/codezero-be/laravel-route-key-exists.svg)]()
4+
[![License](https://img.shields.io/packagist/l/codezero/laravel-route-key-exists.svg)]()
5+
[![Build Status](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/badges/build.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/build-status/master)
6+
[![Code Coverage](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master)
7+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/codezero/laravel-route-key-exists.svg)](https://packagist.org/packages/codezero/laravel-route-key-exists)
9+
10+
#### Laravel validation rule to check if a custom route key exists.
11+
12+
Laravel's `exists` rule checks a database table for a column with a given value. This validation rule uses the `resolveRouteBinding()` method on a model to check if a given value exists.
13+
14+
## Requirements
15+
16+
- PHP >= 7.0
17+
- [Laravel](https://laravel.com/) >= 5.5
18+
19+
## Installation
20+
21+
Require the package via Composer:
22+
23+
```
24+
composer require codezero/laravel-route-key-exists
25+
```
26+
## Some Background Info
27+
28+
Laravel's [implicit route model binding](https://laravel.com/docs/5.5/routing#route-model-binding) allows you to automatically resolve a model by type hinting it in a controller. Furthermore, you can change the route key that is used to query the database in your model:
29+
30+
```php
31+
public function getRouteKeyName()
32+
{
33+
return 'id';
34+
}
35+
```
36+
37+
This also works when you are using a custom or computed route key in your model:
38+
39+
```php
40+
public function getRouteKey()
41+
{
42+
// "encode" the route key
43+
return "foo-{$this->id}";
44+
}
45+
46+
public function resolveRouteBinding($value)
47+
{
48+
// "decode" the route key
49+
$id = (int) str_replace('foo-', '', $value);
50+
51+
// resolve from the database
52+
return $this->where('id', $id)->first();
53+
}
54+
```
55+
56+
But what if you are sending a custom key in a POST request and you want to validate it? Unlike Laravel's [`exists`](https://laravel.com/docs/5.5/validation#rule-exists) rule, this validation rule uses the `resolveRouteBinding()` method to check if the key is valid.
57+
58+
## Usage
59+
60+
Let's say you have a model with an ID of `1`, but `getRouteKey()` returns the encoded value of `1234`.
61+
62+
In your validation rules, new up `\CodeZero\RouteKeyExists\RouteKeyExists` and pass the model's class name to the constructor:
63+
64+
```php
65+
request()->validate([
66+
'model_id' => new RouteKeyExists(Model::class),
67+
]);
68+
```
69+
70+
Here, `model_id` is the encoded value, which will be resolved using the `resolveRouteBinding()` method on your model. If it can't be resolved, validation fails.
71+
72+
Possibly, you will need the actual ID to work with when validation passes. Tack on `replace()` to the rule and `model_id` will be updated to the actual ID:
73+
74+
```php
75+
request()->validate([
76+
'model_id' => (new RouteKeyExists(Model::class))->replace(),
77+
]);
78+
79+
$id = request('model_id');
80+
```
81+
82+
Or maybe you want to keep the encoded ID in the request, but add the actual ID as well. Just tack on `add()` and specify an attribute name:
83+
84+
```php
85+
request()->validate([
86+
'model_id' => (new RouteKeyExists(Model::class))->add('actual_id'),
87+
]);
88+
89+
$id = request('actual_id');
90+
```
91+
92+
Beware that `actual_id` is not included on the `$attributes` array, since it is not being validated.
93+
94+
## Useful Packages
95+
96+
- This rule works perfectly with the [`laravel-optimus`](https://github.com/cybercog/laravel-optimus) model trait.
97+
98+
## Testing
99+
100+
```
101+
vendor/bin/phpunit
102+
```
103+
104+
## Security
105+
106+
If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.
107+
108+
## Changelog
109+
110+
See a list of important changes in the [changelog](https://github.com/codezero-be/laravel-route-key-exists/blob/master/CHANGELOG.md).
111+
112+
## License
113+
114+
The MIT License (MIT). Please see [License File](https://github.com/codezero-be/laravel-route-key-exists/blob/master/LICENSE.md) for more information.

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "codezero/laravel-route-key-exists",
3+
"description": "Laravel validation rule to check if a custom route key exists.",
4+
"keywords": [
5+
"laravel",
6+
"route",
7+
"key",
8+
"id",
9+
"model",
10+
"binding"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Ivan Vermeyen",
16+
"email": "ivan@codezero.be"
17+
}
18+
],
19+
"require": {
20+
"php": ">=7.0.0"
21+
},
22+
"require-dev": {
23+
"orchestra/testbench": "~3.5.0",
24+
"phpunit/phpunit": "~6.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"CodeZero\\RouteKeyExists\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"CodeZero\\RouteKeyExists\\Tests\\": "tests"
34+
}
35+
},
36+
"minimum-stability": "stable",
37+
"sort-packages": true,
38+
"optimize-autoloader": true
39+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Tests">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
<php>
22+
<env name="APP_ENV" value="testing"/>
23+
<env name="CACHE_DRIVER" value="array"/>
24+
<env name="SESSION_DRIVER" value="array"/>
25+
<env name="QUEUE_DRIVER" value="sync"/>
26+
<env name="DB_CONNECTION" value="sqlite"/>
27+
<env name="DB_DATABASE" value=":memory:"/>
28+
</php>
29+
</phpunit>

0 commit comments

Comments
 (0)