Skip to content

Commit da57c42

Browse files
authored
Merge pull request korridor#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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 1 addition & 11 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: laravel

.travis.yml

Lines changed: 54 additions & 0 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 63 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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
];

0 commit comments

Comments
 (0)