Skip to content

Commit d72bdfd

Browse files
committed
First release
0 parents  commit d72bdfd

17 files changed

+689
-0
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
5+
end_of_line = lf
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
insert_final_newline = false
11+
12+
[*.php]
13+
14+
end_of_line = lf
15+
charset = utf-8
16+
indent_style = space
17+
indent_size = 4
18+
insert_final_newline = true
19+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/tests export-ignore
2+
/examples export-ignore
3+
/docker export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/phpcs.xml.dist export-ignore
8+
/captainhook.json export-ignore
9+
/.travis.yml export-ignore
10+
/ruleset.xml export-ignore
11+
/.editorconfig export-ignore
12+
/phpstan.neon export-ignore
13+
/psalm.xml export-ignore
14+
/psalm-baseline.xml export-ignore

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.idea
2+
/composer.lock
3+
/vendor
4+
/.phpunit*
5+
/tests/_output
6+
/tests/_reports
7+
/build

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
env:
2+
global:
3+
- CC_TEST_REPORTER_ID=5a4b76842bd20fccbb5e141c996a607c6de0287f1f98829c8f203998f3c5f922
4+
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
5+
language: php
6+
php:
7+
- '7.2'
8+
- '7.3'
9+
10+
before_script:
11+
- composer self-update
12+
- composer install --prefer-source --no-interaction --dev
13+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14+
- chmod +x ./cc-test-reporter
15+
- ./cc-test-reporter before-build
16+
17+
script:
18+
- vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml
19+
- vendor/bin/phpstan analyse src --level=7
20+
- vendor/bin/psalm
21+
- vendor/bin/phpcs --standard=ruleset.xml --extensions=php src tests
22+
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT; fi
23+
24+
notifications:
25+
email: false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Andreas Leathley
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 all
13+
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 THE
21+
SOFTWARE.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
Squirrel Validator Cascade
2+
==========================
3+
4+
[![Build Status](https://img.shields.io/travis/com/squirrelphp/validator-cascade.svg)](https://travis-ci.com/squirrelphp/validator-cascade) [![Test Coverage](https://api.codeclimate.com/v1/badges/e056be025c6db0eb31f1/test_coverage)](https://codeclimate.com/github/squirrelphp/validator-cascade/test_coverage) ![PHPStan](https://img.shields.io/badge/style-level%207-success.svg?style=flat-round&label=phpstan) [![Packagist Version](https://img.shields.io/packagist/v/squirrelphp/validator-cascade.svg?style=flat-round)](https://packagist.org/packages/squirrelphp/validator-cascade) [![PHP Version](https://img.shields.io/packagist/php-v/squirrelphp/validator-cascade.svg)](https://packagist.org/packages/squirrelphp/validator-cascade) [![Software License](https://img.shields.io/badge/license-MIT-success.svg?style=flat-round)](LICENSE)
5+
6+
Reimplements the `Valid` constraint in the Symfony Validator component as `Cascade` annotation which is much more straightforward to use and has no surprising behavior.
7+
8+
Installation
9+
------------
10+
11+
composer require squirrelphp/validator-cascade
12+
13+
Usage
14+
-----
15+
16+
`Cascade` makes sure an object (or an array of objects) are validated, so it cascades validation.
17+
18+
There are only two options:
19+
20+
- `groups` defines to which validation groups the `Cascade` constraint belongs to, with the same behavior as any regular validator constraint. If you do not define `groups` it is set to `Default` and the `Cascade` constraint is only executed if one of the validation groups matches.
21+
22+
- `trigger` defines which validation groups to trigger on the child object(s). By default only `Default` is triggered, so if you want any other validation groups to trigger you have to specify them with `trigger`. The validation groups of the "parent" are never cascaded.
23+
24+
That is it!
25+
26+
Example
27+
-------
28+
29+
Belong is a common example in real applications: You might have an order and multiple possible addresses for the order (one for shipping, one for invoice) with different requirements, and some addresses should be optional, but if they are specified they should still be validated in full.
30+
31+
`$shippingAddress` shows how to trigger specific validation groups in the child object, in this case to make the phone number a mandatory part of the information (often the case for shipping, but not necessarily for invoice or other address use) in addition to the "Default" constraints.
32+
33+
`$invoiceAddress` is only validated if the validation group "alternateInvoiceAddress" is passed to the validator (which could be done if the user selected an option like "choose different invoice address"). The phone number is optional, as we do not pass the `trigger` option so only the Default group is validated in the Adress object.
34+
35+
```php
36+
use Squirrel\ValidatorCascade\Cascade;
37+
use Symfony\Component\Validator\Constraints as Assert;
38+
39+
class Order
40+
{
41+
/**
42+
* Validate $shippingAddress if validation with no validation
43+
* group or the "Default" validation group is triggered
44+
*
45+
* Validates "Default" and "phoneNumberMandatory" validation groups in $shippingAddress
46+
*
47+
* @Assert\NotNull()
48+
* @Assert\Type(type="Address")
49+
* @Cascade(trigger={"Default", "phoneNumberMandatory"})
50+
*/
51+
public $shippingAddress;
52+
53+
/**
54+
* Validate $invoiceAddress only if validation group
55+
* "alternateInvoiceAddress" is passed to validator
56+
*
57+
* Validates only "Default" validation group in $invoiceAddress, so phone number is optional
58+
*
59+
* @Assert\NotNull()
60+
* @Assert\Type(type="Address")
61+
* @Cascade(groups={"alternateInvoiceAddress"})
62+
*/
63+
public $invoiceAddress;
64+
}
65+
66+
class Address
67+
{
68+
/**
69+
* @Assert\NotNull()
70+
* @Assert\NotBlank()
71+
* @Assert\Length(
72+
* min = 1,
73+
* max = 50
74+
* )
75+
*
76+
* @var string
77+
*/
78+
public $street = '';
79+
80+
/**
81+
* @Assert\NotNull()
82+
* @Assert\NotBlank()
83+
* @Assert\Length(
84+
* min = 1,
85+
* max = 50
86+
* )
87+
*
88+
* @var string
89+
*/
90+
public $city = '';
91+
92+
/**
93+
* @Assert\NotNull(groups={"phoneNumberMandatory"})
94+
* @Assert\NotBlank(groups={"phoneNumberMandatory"})
95+
* @Assert\Length(
96+
* min = 1,
97+
* max = 50
98+
* )
99+
*
100+
* @var string
101+
*/
102+
public $phoneNumber = '';
103+
}
104+
105+
$order = new Order();
106+
$order->shippingAddress = new Address();
107+
$order->invoiceAddress = new Address();
108+
109+
// This validates with the "Default" validation group,
110+
// so only shippingAddress must be specified
111+
$symfonyValidator->validate($order);
112+
113+
// This also validates the invoice address
114+
$symfonyValidator->validate($order, null, [
115+
"Default",
116+
"alternateInvoiceAddress",
117+
]);
118+
```

captainhook.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"commit-msg": {
3+
"enabled": true,
4+
"actions": [
5+
{
6+
"action": "\\CaptainHook\\App\\Hook\\Message\\Action\\Beams",
7+
"options": {
8+
"subjectLength": 50,
9+
"bodyLineLength": 72
10+
},
11+
"conditions": []
12+
}
13+
]
14+
},
15+
"pre-push": {
16+
"enabled": false,
17+
"actions": []
18+
},
19+
"pre-commit": {
20+
"enabled": true,
21+
"actions": [
22+
{
23+
"action": "\\CaptainHook\\App\\Hook\\PHP\\Action\\Linting",
24+
"options": [],
25+
"conditions": []
26+
},
27+
{
28+
"action": "vendor/bin/phpunit",
29+
"options": [],
30+
"conditions": []
31+
},
32+
{
33+
"action": "vendor/bin/phpstan analyse src --level=7",
34+
"options": [],
35+
"conditions": []
36+
},
37+
{
38+
"action": "vendor/bin/psalm",
39+
"options": [],
40+
"conditions": []
41+
},
42+
{
43+
"action": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src tests",
44+
"options": [],
45+
"conditions": []
46+
}
47+
]
48+
},
49+
"prepare-commit-msg": {
50+
"enabled": false,
51+
"actions": []
52+
},
53+
"post-commit": {
54+
"enabled": false,
55+
"actions": []
56+
},
57+
"post-merge": {
58+
"enabled": false,
59+
"actions": []
60+
},
61+
"post-checkout": {
62+
"enabled": false,
63+
"actions": []
64+
}
65+
}

composer.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "squirrelphp/validator-cascade",
3+
"type": "library",
4+
"description": "Cascade annotation for Symfony Validator annotation, reimplementing the Valid constraint in a more flexible and understandable way",
5+
"keywords": [
6+
"php",
7+
"symfony",
8+
"validator",
9+
"form",
10+
"valid",
11+
"cascade"
12+
],
13+
"homepage": "https://github.com/squirrelphp/validator-cascade",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Andreas Leathley",
18+
"email": "andreas.leathley@panaxis.ch"
19+
}
20+
],
21+
"require": {
22+
"php": "^7.2",
23+
"symfony/validator": "^4.0",
24+
"doctrine/annotations": "^1.6"
25+
},
26+
"require-dev": {
27+
"captainhook/plugin-composer": "^4.0",
28+
"diablomedia/phpunit-pretty-printer": "^4.0",
29+
"mockery/mockery": "^1.0",
30+
"phpstan/phpstan": "^0.11.5",
31+
"phpunit/phpunit": "^8.0",
32+
"slevomat/coding-standard": "^5.0",
33+
"squizlabs/php_codesniffer": "^3.2",
34+
"vimeo/psalm": "^3.2"
35+
},
36+
"config": {
37+
"sort-packages": false
38+
},
39+
"autoload": {
40+
"psr-4": {
41+
"Squirrel\\ValidatorCascade\\": "src/"
42+
}
43+
},
44+
"autoload-dev": {
45+
"psr-4": {
46+
"Squirrel\\ValidatorCascade\\Tests\\": "tests/",
47+
"Squirrel\\ValidatorCascade\\Examples\\": "examples/"
48+
}
49+
},
50+
"scripts": {
51+
"phpstan": "vendor/bin/phpstan analyse src --level=7",
52+
"psalm": "vendor/bin/psalm --show-info=false",
53+
"psalm_base": "vendor/bin/psalm --set-baseline=psalm-baseline.xml",
54+
"phpunit": "vendor/bin/phpunit --colors=always",
55+
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src tests",
56+
"phpcsfix": "vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --cache src tests",
57+
"codecoverage": "vendor/bin/phpunit --coverage-html tests/_reports"
58+
}
59+
}

examples/Address.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Squirrel\ValidatorCascade\Examples;
4+
5+
use Symfony\Component\Validator\Constraints as Assert;
6+
7+
class Address
8+
{
9+
/**
10+
* @Assert\NotNull()
11+
* @Assert\NotBlank()
12+
* @Assert\Length(
13+
* min = 1,
14+
* max = 50
15+
* )
16+
*
17+
* @var string
18+
*/
19+
public $street;
20+
21+
/**
22+
* @Assert\NotNull()
23+
* @Assert\NotBlank()
24+
* @Assert\Length(
25+
* min = 1,
26+
* max = 50
27+
* )
28+
*
29+
* @var string
30+
*/
31+
public $city;
32+
33+
/**
34+
* @Assert\NotNull(groups={"phoneNumberMandatory"})
35+
* @Assert\NotBlank(groups={"phoneNumberMandatory"})
36+
* @Assert\Length(
37+
* min = 1,
38+
* max = 50
39+
* )
40+
*
41+
* @var string
42+
*/
43+
public $phoneNumber;
44+
}

0 commit comments

Comments
 (0)