-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90d7ed3
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Fluoresce\ValidateEmbedded\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | ||
* | ||
* @author Jaik Dean <jaik@fluoresce.co> | ||
*/ | ||
class Validate extends Constraint | ||
{ | ||
/** | ||
* @var array Embedded groups | ||
*/ | ||
public $embeddedGroups; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Fluoresce\ValidateEmbedded\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* Validates objects embedded within properties on a parent object. | ||
* | ||
* @author Jaik Dean <jaik@fluoresce.co> | ||
*/ | ||
class ValidateValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof Validate) { | ||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Validate'); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if (!is_object($value) && !is_array($value)) { | ||
throw new UnexpectedTypeException($value, 'object'); | ||
} | ||
|
||
// Validate the embedded object(s) | ||
$validator = $this->context->getValidator(); | ||
$violations = $validator->validate($value, null, $constraint->embeddedGroups); | ||
|
||
// Add violations to the calling context | ||
foreach ($violations as $violation) { | ||
$this->context->buildViolation($violation->getMessage()) | ||
->atPath($violation->getPropertyPath()) | ||
->setCode($violation->getCode()) | ||
->addViolation(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2016 Fluoresce Ltd | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
Validate Embedded | ||
================= | ||
|
||
This library provides the `Validate` constraint for the | ||
[Symfony Validator Component](https://github.com/symfony/validator). | ||
|
||
This constraint behaves similar to the built-in `Valid` constraint, but respects | ||
the `groups` option. Additionally, it adds an option to specify the validation | ||
groups to target on the embedded object(s). | ||
|
||
## Installation | ||
|
||
Open a command console, enter your project directory and execute the following | ||
command to download the latest stable version of this bundle: | ||
|
||
```bash | ||
$ composer require fluoresce/validate-embedded | ||
``` | ||
|
||
This command requires you to have Composer installed globally, as explained in | ||
the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the | ||
Composer documentation. | ||
|
||
## Documentation | ||
|
||
_The following examples assume you are using the full Symfony framework, though | ||
this package can be used in any project that has the Symfony Validator Component | ||
available_ | ||
|
||
### Basic Usage | ||
|
||
This example shows an `Author` which has a collection of `Book`s. When | ||
validation is run for `group1` on `Author`, it will cascade to all embedded | ||
`Book` instances. | ||
|
||
The behaviour here is slightly different to the standard `Valid` constraint, as | ||
validation of the `Book` instances is run with the default group. | ||
|
||
```php | ||
<?php | ||
|
||
use Fluoresce\ValidateEmbedded\Constraints as Fluoresce; | ||
|
||
class Author | ||
{ | ||
/** | ||
* @var Book[] | ||
* @Fluoresce\Validate(groups={"group1"}); | ||
*/ | ||
private $books; | ||
} | ||
``` | ||
|
||
```php | ||
<?php | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Book | ||
{ | ||
/** | ||
* @var string | ||
* @Assert\NotBlank() | ||
*/ | ||
private $title; | ||
} | ||
``` | ||
|
||
### Specifying Embedded Validation Groups | ||
|
||
Imagine we want to specify different validation groups to be run on the `Book` | ||
instances. We can target these by specifying them in the `Validate` annotation. | ||
|
||
```php | ||
<?php | ||
|
||
use Fluoresce\ValidateEmbedded\Constraints as Fluoresce; | ||
|
||
class Author | ||
{ | ||
/** | ||
* @var Book[] | ||
* @Fluoresce\Validate(embeddedGroups={"bookgroup1"}); | ||
*/ | ||
private $books; | ||
} | ||
``` | ||
|
||
```php | ||
<?php | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Book | ||
{ | ||
/** | ||
* @var string | ||
* @Assert\NotBlank(groups={"bookgroup1"}) | ||
*/ | ||
private $title; | ||
} | ||
``` | ||
|
||
Now whenever an `Author` instance is validated, the embedded `Book`s will be | ||
validated with validation group `bookgroup1`. | ||
|
||
### Combining All Options | ||
|
||
This example combines behaviour from the previous ones to show how it operates | ||
together. | ||
|
||
```php | ||
<?php | ||
|
||
use Fluoresce\ValidateEmbedded\Constraints as Fluoresce; | ||
|
||
class Author | ||
{ | ||
/** | ||
* @var Book[] | ||
* @Fluoresce\Validate(groups={"group1"}, embeddedGroups={"bookgroup1"}); | ||
*/ | ||
private $books; | ||
} | ||
``` | ||
|
||
```php | ||
<?php | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Book | ||
{ | ||
/** | ||
* @var string | ||
* @Assert\NotBlank(groups={"bookgroup1"}) | ||
*/ | ||
private $title; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "fluoresce/validate-embedded", | ||
"description": "Constraint enabling group-based validation of embedded objects with the Symfony Validator Component.", | ||
"keywords": ["validate", "validation", "validator", "constraint", "symfony2", "symfony3"], | ||
"homepage": "https://github.com/fluoresceco/validate-embedded", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jaik Dean", | ||
"email": "jaik@fluoresce.co" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.9", | ||
"symfony/validator": "~2.8|~3.0" | ||
}, | ||
"minimum-stability": "stable", | ||
"autoload": { | ||
"psr-4": { | ||
"Fluoresce\\ValidateEmbedded\\": "" | ||
} | ||
} | ||
} |