Skip to content

Commit d0eb13e

Browse files
Rebase and update to latest CS
1 parent fc66683 commit d0eb13e

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* added UATP cards support to `CardSchemeValidator`
99
* added option `allowNull` to NotBlank constraint
1010
* added `Json` constraint
11+
* added `Unique` constraint
1112

1213
4.2.0
1314
-----

src/Symfony/Component/Validator/Constraints/Unique.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
*/
2222
class Unique extends Constraint
2323
{
24-
const IS_NOT_UNIQUE = '7911c98d-b845-4da0-94b7-a8dac36bc55a';
24+
public const IS_NOT_UNIQUE = '7911c98d-b845-4da0-94b7-a8dac36bc55a';
2525

26-
protected static $errorNames = array(
26+
protected static $errorNames = [
2727
self::IS_NOT_UNIQUE => 'IS_NOT_UNIQUE',
28-
);
28+
];
2929

3030
public $message = 'This collection should contain only unique elements';
3131
}

src/Symfony/Component/Validator/Constraints/UniqueValidator.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
16+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1717

1818
/**
1919
* @author Yevgeniy Zholkevskiy <[email protected]>
@@ -33,13 +33,13 @@ public function validate($value, Constraint $constraint)
3333
return;
3434
}
3535

36-
if (!is_array($value) && !$value instanceof \IteratorAggregate) {
37-
throw new UnexpectedTypeException($value, 'IteratorAggregate');
36+
if (!\is_array($value) && !$value instanceof \IteratorAggregate) {
37+
throw new UnexpectedValueException($value, 'array|IteratorAggregate');
3838
}
3939

40-
$collectionElements = array();
40+
$collectionElements = [];
4141
foreach ($value as $element) {
42-
if (in_array($element, $collectionElements, true)) {
42+
if (\in_array($element, $collectionElements, true)) {
4343
$this->context->buildViolation($constraint->message)
4444
->setParameter('{{ value }}', $this->formatValue($value))
4545
->setCode(Unique::IS_NOT_UNIQUE)

src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function createValidator()
2323
}
2424

2525
/**
26-
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
26+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
2727
*/
2828
public function testExpectsUniqueConstraintCompatibleType()
2929
{
@@ -42,29 +42,29 @@ public function testValidValues($value)
4242

4343
public function getValidValues()
4444
{
45-
return array(
46-
yield 'null' => array(array(null)),
47-
yield 'empty array' => array(array()),
48-
yield 'single integer' => array(array(5)),
49-
yield 'single string' => array(array('a')),
50-
yield 'single object' => array(array(new \stdClass())),
51-
yield 'unique booleans' => array(array(true, false)),
52-
yield 'unique integers' => array(array(1, 2, 3, 4, 5, 6)),
53-
yield 'unique floats' => array(array(0.1, 0.2, 0.3)),
54-
yield 'unique strings' => array(array('a', 'b', 'c')),
55-
yield 'unique arrays' => array(array(array(1, 2), array(2, 4), array(4, 6))),
56-
yield 'unique objects' => array(array(new \stdClass(), new \stdClass())),
57-
);
45+
return [
46+
yield 'null' => [[null]],
47+
yield 'empty array' => [[]],
48+
yield 'single integer' => [[5]],
49+
yield 'single string' => [['a']],
50+
yield 'single object' => [[new \stdClass()]],
51+
yield 'unique booleans' => [[true, false]],
52+
yield 'unique integers' => [[1, 2, 3, 4, 5, 6]],
53+
yield 'unique floats' => [[0.1, 0.2, 0.3]],
54+
yield 'unique strings' => [['a', 'b', 'c']],
55+
yield 'unique arrays' => [[[1, 2], [2, 4], [4, 6]]],
56+
yield 'unique objects' => [[new \stdClass(), new \stdClass()]],
57+
];
5858
}
5959

6060
/**
6161
* @dataProvider getInvalidValues
6262
*/
6363
public function testInvalidValues($value)
6464
{
65-
$constraint = new Unique(array(
65+
$constraint = new Unique([
6666
'message' => 'myMessage',
67-
));
67+
]);
6868
$this->validator->validate($value, $constraint);
6969

7070
$this->buildViolation('myMessage')
@@ -77,13 +77,13 @@ public function getInvalidValues()
7777
{
7878
$object = new \stdClass();
7979

80-
return array(
81-
yield 'not unique booleans' => array(array(true, true)),
82-
yield 'not unique integers' => array(array(1, 2, 3, 3)),
83-
yield 'not unique floats' => array(array(0.1, 0.2, 0.1)),
84-
yield 'not unique string' => array(array('a', 'b', 'a')),
85-
yield 'not unique arrays' => array(array(array(1, 1), array(2, 3), array(1, 1))),
86-
yield 'not unique objects' => array(array($object, $object)),
87-
);
80+
return [
81+
yield 'not unique booleans' => [[true, true]],
82+
yield 'not unique integers' => [[1, 2, 3, 3]],
83+
yield 'not unique floats' => [[0.1, 0.2, 0.1]],
84+
yield 'not unique string' => [['a', 'b', 'a']],
85+
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]]],
86+
yield 'not unique objects' => [[$object, $object]],
87+
];
8888
}
8989
}

0 commit comments

Comments
 (0)