feature #26555 [Validator] Add constraint on unique elements collection(Assert\Unique) (zenmate, nicolas-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Validator] Add constraint on unique elements collection(Assert\Unique)

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #26535
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

d0eb13e55a Rebase and update to latest CS
fc66683cf2 Add UniqueCollection constraint and validator
This commit is contained in:
Fabien Potencier 2019-03-25 12:42:44 +01:00
commit c1467446ad
4 changed files with 174 additions and 0 deletions

View File

@ -8,6 +8,7 @@ CHANGELOG
* added UATP cards support to `CardSchemeValidator`
* added option `allowNull` to NotBlank constraint
* added `Json` constraint
* added `Unique` constraint
4.2.0
-----

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com>
*/
class Unique extends Constraint
{
public const IS_NOT_UNIQUE = '7911c98d-b845-4da0-94b7-a8dac36bc55a';
protected static $errorNames = [
self::IS_NOT_UNIQUE => 'IS_NOT_UNIQUE',
];
public $message = 'This collection should contain only unique elements';
}

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* @author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com>
*/
class UniqueValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Unique) {
throw new UnexpectedTypeException($constraint, Unique::class);
}
if (null === $value) {
return;
}
if (!\is_array($value) && !$value instanceof \IteratorAggregate) {
throw new UnexpectedValueException($value, 'array|IteratorAggregate');
}
$collectionElements = [];
foreach ($value as $element) {
if (\in_array($element, $collectionElements, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Unique::IS_NOT_UNIQUE)
->addViolation();
return;
}
$collectionElements[] = $element;
}
}
}

View File

@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\Unique;
use Symfony\Component\Validator\Constraints\UniqueValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
class UniqueValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
{
return new UniqueValidator();
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsUniqueConstraintCompatibleType()
{
$this->validator->validate('', new Unique());
}
/**
* @dataProvider getValidValues
*/
public function testValidValues($value)
{
$this->validator->validate($value, new Unique());
$this->assertNoViolation();
}
public function getValidValues()
{
return [
yield 'null' => [[null]],
yield 'empty array' => [[]],
yield 'single integer' => [[5]],
yield 'single string' => [['a']],
yield 'single object' => [[new \stdClass()]],
yield 'unique booleans' => [[true, false]],
yield 'unique integers' => [[1, 2, 3, 4, 5, 6]],
yield 'unique floats' => [[0.1, 0.2, 0.3]],
yield 'unique strings' => [['a', 'b', 'c']],
yield 'unique arrays' => [[[1, 2], [2, 4], [4, 6]]],
yield 'unique objects' => [[new \stdClass(), new \stdClass()]],
];
}
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value)
{
$constraint = new Unique([
'message' => 'myMessage',
]);
$this->validator->validate($value, $constraint);
$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
public function getInvalidValues()
{
$object = new \stdClass();
return [
yield 'not unique booleans' => [[true, true]],
yield 'not unique integers' => [[1, 2, 3, 3]],
yield 'not unique floats' => [[0.1, 0.2, 0.1]],
yield 'not unique string' => [['a', 'b', 'a']],
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]]],
yield 'not unique objects' => [[$object, $object]],
];
}
}