Skip to content

Commit da57c42

Browse files
authored
Merge pull request #1 from korridor/feature/unique_eloquent
Added UniqueEloquent rule; Renamed ExistEloquent to ExistsEloquent
2 parents 87a0f9e + 5a29192 commit da57c42

19 files changed

+602
-66
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
[*.md]
15+
trim_trailing_whitespace = false

.php_cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@
33
return PhpCsFixer\Config::create()
44
->setRiskyAllowed(false)
55
->setRules([
6-
'@Symfony' => true,
7-
'array_syntax' => ['syntax' => 'short'],
8-
'ordered_imports' => true,
9-
'protected_to_private' => false,
10-
// Part of future @Symfony ruleset in PHP-CS-Fixer To be removed from the config file once upgrading
11-
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
12-
// custom
13-
'phpdoc_separation' => false,
14-
'phpdoc_align' => false,
15-
'phpdoc_no_alias_tag' => false,
16-
'array_indentation' => true,
6+
'@PSR2' => true,
177
])
188
->setUsingCache(true)
199
->setFinder(

.styleci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: laravel

.travis.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
cache:
2+
directories:
3+
- $HOME/.composer/cache
4+
5+
language: php
6+
7+
matrix:
8+
include:
9+
- php: 7.1
10+
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' COMPOSER_FLAGS='--prefer-stable'
11+
- php: 7.2
12+
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' COMPOSER_FLAGS='--prefer-stable'
13+
- php: 7.3
14+
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' COMPOSER_FLAGS='--prefer-stable'
15+
- php: 7.1
16+
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' COMPOSER_FLAGS='--prefer-stable'
17+
- php: 7.2
18+
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' COMPOSER_FLAGS='--prefer-stable'
19+
- php: 7.3
20+
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' COMPOSER_FLAGS='--prefer-stable'
21+
- php: 7.1
22+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-lowest'
23+
- php: 7.1
24+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-stable'
25+
- php: 7.2
26+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-lowest'
27+
- php: 7.2
28+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-stable'
29+
- php: 7.3
30+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-lowest'
31+
- php: 7.3
32+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-stable'
33+
- php: 7.2
34+
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-lowest'
35+
- php: 7.2
36+
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-stable'
37+
- php: 7.3
38+
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-lowest'
39+
- php: 7.3
40+
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-stable'
41+
fast_finish: true
42+
43+
before_install:
44+
- travis_retry composer self-update
45+
- travis_retry composer require "laravel/framework:${LARAVEL}" "orchestra/testbench:${TESTBENCH}" --no-interaction --no-update
46+
47+
install:
48+
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction --no-suggest
49+
50+
before_script:
51+
- composer config discard-changes true
52+
53+
script:
54+
- vendor/bin/phpunit --coverage-text

composer.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
],
1212
"license": "MIT",
1313
"require": {
14-
"php": "7.*",
15-
"illuminate/support": "^5.8|^6.0"
14+
"php": "^7.1|^7.2|^7.3",
15+
"illuminate/support": "^5.6|^5.7|^5.8|^6.0"
1616
},
1717
"require-dev": {
18-
"orchestra/testbench": "^3.5|^4.0",
18+
"orchestra/testbench": "^3.6|^3.7|^3.8|^4.0",
1919
"phpunit/phpunit": "^7.0|^8.0",
2020
"friendsofphp/php-cs-fixer": "^2.15",
2121
"squizlabs/php_codesniffer": "^3.4"
@@ -44,9 +44,6 @@
4444
}
4545
},
4646
"config": {
47-
"sort-packages": true,
48-
"platform": {
49-
"php": "7.1"
50-
}
47+
"sort-packages": true
5148
}
5249
}

composer.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
</rule>
66

77
<file>src/</file>
8+
<file>resources/</file>
89
<file>tests/</file>
910
</ruleset>

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
stopOnFailure="false">
1212
<testsuites>
1313
<testsuite name="Feature">
14-
<directory>tests</directory>
14+
<directory>tests/Feature</directory>
1515
</testsuite>
1616
</testsuites>
1717
<filter>

readme.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
11
# Laravel model validation rules
22

3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/korridor/laravel-model-validation-rules?style=flat-square)](https://packagist.org/packages/korridor/laravel-model-validation-rules)
4+
[![License](https://img.shields.io/packagist/l/korridor/laravel-model-validation-rules?style=flat-square)](license.md)
5+
[![TravisCI](https://img.shields.io/travis/korridor/laravel-model-validation-rules?style=flat-square)](https://travis-ci.org/korridor/laravel-model-validation-rules)
6+
[![StyleCI](https://styleci.io/repos/208495858/shield)](https://styleci.io/repos/208495858)
7+
8+
This package is an alternative to the Laravel built-in validation rules `exists` and `unique`.
9+
It uses Eloquent models instead of directly querying the database.
10+
11+
**Advantages**
12+
- The rule can be easily extended with the Eloquent builder. (scopes etc.)
13+
- Softdeletes are working out of the box.
14+
- Logic implemented into the models work in the validation as well. (multi tenancy system, etc.)
15+
316
## Installation
417

18+
You can install the package via composer with following command:
19+
520
```bash
621
composer require korridor/laravel-model-validation-rules
722
```
823

24+
### Translation
25+
26+
If you want to customize the translations of the validation errors you can publish the translations
27+
of the package to the `resources/lang/vendor/modelValidationRules` folder.
28+
929
```bash
1030
php artisan vendor:publish --provider="Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"
1131
```
1232

13-
## Usage
33+
### Requirements
34+
35+
This package is tested for the following Laravel versions:
36+
37+
- 6.0
38+
- 5.8
39+
- 5.7 (stable only)
40+
- 5.6 (stable only)
41+
42+
## Usage examples
43+
44+
**PostStoreRequest**
45+
46+
```php
47+
public function rules()
48+
{
49+
$postId = $this->post->id;
50+
51+
return [
52+
'username' => [new UniqueEloquent(User::class, 'username')],
53+
'title' => ['string'],
54+
'content' => ['string'],
55+
'comments.*.id' => [
56+
'nullable',
57+
new ExistEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
58+
return $builder->where('post_id', $postId);
59+
}),
60+
],
61+
'comments.*.content' => ['string']
62+
];
63+
}
64+
```
65+
66+
**PostUpdateRequest**
1467

1568
```php
1669
public function rules()
@@ -19,6 +72,7 @@ public function rules()
1972

2073
return [
2174
'id' => [new ExistEloquent(Post::class)],
75+
'username' => [new UniqueEloquent(User::class, 'username')->ignore($postId)],
2276
'title' => ['string'],
2377
'content' => ['string'],
2478
'comments.*.id' => [
@@ -34,6 +88,8 @@ public function rules()
3488

3589
## Contributing
3690

91+
I am open for suggestions and contributions. Just create an issue or a pull request.
92+
3793
### Testing
3894

3995
```bash
@@ -48,6 +104,11 @@ composer fix
48104
composer lint
49105
```
50106

107+
## Credits
108+
109+
The structure of the repository and the TestClass is inspired by the
110+
project [laravel-validation-rules](https://github.com/spatie/laravel-validation-rules) by [spatie](https://github.com/spatie).
111+
51112
## License
52113

53-
The MIT License (MIT). Please see [license file](license.md) for more information.
114+
This package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.

resources/lang/de/validation.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

33
return [
4-
'exist_model' => 'Die Ressource existiert nicht.'
4+
'exists_model' => 'Die Ressource existiert nicht.',
5+
'unique_model' => 'Die Ressource existiert bereits.',
56
];

resources/lang/en/validation.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

33
return [
4-
'exist_model' => 'The resource does not exist.'
4+
'exists_model' => 'The resource does not exist.',
5+
'unique_model' => 'The resource already exists.',
56
];

src/ModelValidationServiceProvider.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function register()
2121
*/
2222
public function boot()
2323
{
24-
$this->publishes([
25-
__DIR__.'/../resources/lang' => resource_path('lang/vendor/modelValidationRules'),
26-
]);
27-
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'modelValidationRules');
24+
$this->publishes([
25+
__DIR__.'/../resources/lang' => resource_path('lang/vendor/modelValidationRules'),
26+
]);
27+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'modelValidationRules');
2828
}
2929
}

src/Rules/ExistEloquent.php src/Rules/ExistsEloquent.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Korridor\LaravelModelValidationRules\Rules;
44

55
use Closure;
6-
use Eloquent;
6+
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Contracts\Validation\Rule;
88

9-
class ExistEloquent implements Rule
9+
class ExistsEloquent implements Rule
1010
{
1111
/**
1212
* @var string
@@ -44,7 +44,7 @@ public function __construct(string $model, ?string $key = null, ?Closure $builde
4444
{
4545
$this->model = $model;
4646
$this->key = $key;
47-
$this->builderClosure = $builderClosure;
47+
$this->setBuilderClosure($builderClosure);
4848
}
4949

5050
/**
@@ -58,10 +58,10 @@ public function passes($attribute, $value): bool
5858
{
5959
$this->attribute = $attribute;
6060
$this->value = $value;
61-
/** @var Eloquent $builder */
61+
/** @var Model $builder */
6262
$builder = new $this->model();
6363
if (null === $this->key) {
64-
$builder = $builder->where($builder->getKeyName(), $value);
64+
$builder = $builder->where((new $this->model())->getKeyName(), $value);
6565
} else {
6666
$builder = $builder->where($this->key, $value);
6767
}
@@ -80,10 +80,29 @@ public function passes($attribute, $value): bool
8080
*/
8181
public function message(): string
8282
{
83-
return trans('modelValidationRules::validation.exist_model', [
84-
'attribute' => $this->attribute,
85-
'model' => class_basename($this->model),
86-
'value' => $this->value,
87-
]);
83+
return trans('modelValidationRules::validation.exists_model', [
84+
'attribute' => $this->attribute,
85+
'model' => class_basename($this->model),
86+
'value' => $this->value,
87+
]);
88+
}
89+
90+
/**
91+
* @param Closure|null $builderClosure
92+
*/
93+
public function setBuilderClosure(?Closure $builderClosure)
94+
{
95+
$this->builderClosure = $builderClosure;
96+
}
97+
98+
/**
99+
* @param Closure $builderClosure
100+
* @return $this
101+
*/
102+
public function query(Closure $builderClosure): self
103+
{
104+
$this->setBuilderClosure($builderClosure);
105+
106+
return $this;
88107
}
89108
}

0 commit comments

Comments
 (0)