Skip to content

Commit db37354

Browse files
committed
simplify callback logic
1 parent 3205952 commit db37354

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1516

1617
/**
1718
* @Annotation
@@ -29,20 +30,22 @@ class Unique extends Constraint
2930
];
3031

3132
public $message = 'This collection should contain only unique elements.';
32-
33-
/**
34-
* @var string|callable
35-
*/
36-
public $valueNormalizer;
33+
public $normalizer;
3734

3835
public function __construct(
3936
array $options = null,
4037
string $message = null,
38+
callable $normalizer = null,
4139
array $groups = null,
4240
$payload = null
4341
) {
4442
parent::__construct($options, $groups, $payload);
4543

4644
$this->message = $message ?? $this->message;
45+
$this->normalizer = $normalizer ?? $this->normalizer;
46+
47+
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
48+
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
49+
}
4750
}
4851
}

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1817
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1918

@@ -40,9 +39,9 @@ public function validate($value, Constraint $constraint)
4039
}
4140

4241
$collectionElements = [];
43-
$valueNormalizer = $this->getValueNormalizer($constraint);
42+
$normalizer = $this->getNormalizer($constraint);
4443
foreach ($value as $element) {
45-
$element = $valueNormalizer($element);
44+
$element = $normalizer($element);
4645

4746
if (\in_array($element, $collectionElements, true)) {
4847
$this->context->buildViolation($constraint->message)
@@ -56,26 +55,14 @@ public function validate($value, Constraint $constraint)
5655
}
5756
}
5857

59-
private function getValueNormalizer(Unique $unique)
58+
private function getNormalizer(Unique $unique): callable
6059
{
61-
$normalizer = $unique->valueNormalizer;
62-
63-
if (null === $normalizer) {
60+
if (null === $unique->normalizer) {
6461
return static function ($value) {
6562
return $value;
6663
};
6764
}
6865

69-
if (\is_callable($normalizer)) {
70-
return $normalizer;
71-
}
72-
73-
if (\is_array($normalizer) && !\is_callable($normalizer) && isset($normalizer[0]) && \is_object($normalizer[0])) {
74-
$normalizer[0] = \get_class($normalizer[0]);
75-
76-
return $normalizer;
77-
}
78-
79-
throw new ConstraintDefinitionException(json_encode($normalizer).' in Unique constraint is not a valid callable.');
66+
return $unique->normalizer;
8067
}
8168
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ public function testAttributes()
3535
self::assertSame(['my_group'], $cConstraint->groups);
3636
self::assertSame('some attached data', $cConstraint->payload);
3737
}
38+
39+
public function testInvalidNormalizerThrowsException()
40+
{
41+
$this->expectException('Symfony\Component\Validator\Exception\InvalidArgumentException');
42+
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).');
43+
new Unique(['normalizer' => 'Unknown Callable']);
44+
}
45+
46+
public function testInvalidNormalizerObjectThrowsException()
47+
{
48+
$this->expectException('Symfony\Component\Validator\Exception\InvalidArgumentException');
49+
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
50+
new Unique(['normalizer' => new \stdClass()]);
51+
}
3852
}
3953

4054
class UniqueDummy

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testExpectsUniqueObjects($callback)
127127
$value = [$object1, $object2, $object3];
128128

129129
$this->validator->validate($value, new Unique([
130-
'valueNormalizer' => $callback,
130+
'normalizer' => $callback,
131131
]));
132132

133133
$this->assertNoViolation();
@@ -154,7 +154,7 @@ public function testExpectsNonUniqueObjects($callback)
154154

155155
$this->validator->validate($value, new Unique([
156156
'message' => 'myMessage',
157-
'valueNormalizer' => $callback,
157+
'normalizer' => $callback,
158158
]));
159159

160160
$this->buildViolation('myMessage')
@@ -183,7 +183,7 @@ public function testExpectsInvalidNonStrictComparison()
183183

184184
$this->validator->validate([1, '1', 1.0, '1.0'], new Unique([
185185
'message' => 'myMessage',
186-
'valueNormalizer' => $callback,
186+
'normalizer' => $callback,
187187
]));
188188

189189
$this->buildViolation('myMessage')
@@ -199,7 +199,7 @@ public function testExpectsValidNonStrictComparison()
199199
};
200200

201201
$this->validator->validate([1, '2', 3, '4.0'], new Unique([
202-
'valueNormalizer' => $callback,
202+
'normalizer' => $callback,
203203
]));
204204

205205
$this->assertNoViolation();
@@ -213,7 +213,7 @@ public function testExpectsInvalidCaseInsensitiveComparison()
213213

214214
$this->validator->validate(['Hello', 'hello', 'HELLO', 'hellO'], new Unique([
215215
'message' => 'myMessage',
216-
'valueNormalizer' => $callback,
216+
'normalizer' => $callback,
217217
]));
218218

219219
$this->buildViolation('myMessage')
@@ -229,7 +229,7 @@ public function testExpectsValidCaseInsensitiveComparison()
229229
};
230230

231231
$this->validator->validate(['Hello', 'World'], new Unique([
232-
'valueNormalizer' => $callback,
232+
'normalizer' => $callback,
233233
]));
234234

235235
$this->assertNoViolation();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy