Skip to content

Commit c7a457f

Browse files
[FrameworkBundle][Validator] Remove remaining deprecations
1 parent 24c0c5e commit c7a457f

13 files changed

+91
-167
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ CHANGELOG
1515
* Remove the annotation reader parameter from the constructor signature of `AnnotationLoader`
1616
* Remove `ValidatorBuilder::setDoctrineAnnotationReader()`
1717
* Remove `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
18+
* Remove `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
19+
* Remove `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead
20+
* Remove `AnnotationLoader`, use `AttributeLoader` instead
1821

1922
6.4
2023
---

Constraints/Callback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Callback extends Constraint
2626

2727
public function __construct(array|string|callable $callback = null, array $groups = null, mixed $payload = null, array $options = [])
2828
{
29-
// Invocation through annotations with an array parameter only
29+
// Invocation through attributes with an array parameter only
3030
if (\is_array($callback) && 1 === \count($callback) && isset($callback['value'])) {
3131
$callback = $callback['value'];
3232
}

Constraints/When.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use Symfony\Component\Validator\Constraint;
1717
use Symfony\Component\Validator\Exception\LogicException;
1818

19-
/**
20-
* @Target({"CLASS", "PROPERTY", "METHOD", "ANNOTATION"})
21-
*/
2219
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2320
class When extends Composite
2421
{

Mapping/Loader/AnnotationLoader.php

Lines changed: 0 additions & 96 deletions
This file was deleted.

Mapping/Loader/AttributeLoader.php

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,85 @@
1111

1212
namespace Symfony\Component\Validator\Mapping\Loader;
1313

14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Constraints\Callback;
16+
use Symfony\Component\Validator\Constraints\GroupSequence;
17+
use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
18+
use Symfony\Component\Validator\Exception\MappingException;
19+
use Symfony\Component\Validator\Mapping\ClassMetadata;
20+
1421
/**
1522
* Loads validation metadata using PHP attributes.
1623
*
1724
* @author Bernhard Schussek <[email protected]>
1825
* @author Alexander M. Turek <[email protected]>
1926
* @author Alexandre Daubois <[email protected]>
2027
*/
21-
class AttributeLoader extends AnnotationLoader
28+
class AttributeLoader implements LoaderInterface
2229
{
23-
public function __construct()
30+
public function loadClassMetadata(ClassMetadata $metadata): bool
31+
{
32+
$reflClass = $metadata->getReflectionClass();
33+
$className = $reflClass->name;
34+
$success = false;
35+
36+
foreach ($this->getAttributes($reflClass) as $constraint) {
37+
if ($constraint instanceof GroupSequence) {
38+
$metadata->setGroupSequence($constraint->groups);
39+
} elseif ($constraint instanceof GroupSequenceProvider) {
40+
$metadata->setGroupSequenceProvider(true);
41+
} elseif ($constraint instanceof Constraint) {
42+
$metadata->addConstraint($constraint);
43+
}
44+
45+
$success = true;
46+
}
47+
48+
foreach ($reflClass->getProperties() as $property) {
49+
if ($property->getDeclaringClass()->name === $className) {
50+
foreach ($this->getAttributes($property) as $constraint) {
51+
if ($constraint instanceof Constraint) {
52+
$metadata->addPropertyConstraint($property->name, $constraint);
53+
}
54+
55+
$success = true;
56+
}
57+
}
58+
}
59+
60+
foreach ($reflClass->getMethods() as $method) {
61+
if ($method->getDeclaringClass()->name === $className) {
62+
foreach ($this->getAttributes($method) as $constraint) {
63+
if ($constraint instanceof Callback) {
64+
$constraint->callback = $method->getName();
65+
66+
$metadata->addConstraint($constraint);
67+
} elseif ($constraint instanceof Constraint) {
68+
if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
69+
$metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
70+
} else {
71+
throw new MappingException(sprintf('The constraint on "%s::%s()" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
72+
}
73+
}
74+
75+
$success = true;
76+
}
77+
}
78+
}
79+
80+
return $success;
81+
}
82+
83+
private function getAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionProperty $reflection): iterable
2484
{
25-
parent::__construct(null);
85+
foreach ($reflection->getAttributes(GroupSequence::class) as $attribute) {
86+
yield $attribute->newInstance();
87+
}
88+
foreach ($reflection->getAttributes(GroupSequenceProvider::class) as $attribute) {
89+
yield $attribute->newInstance();
90+
}
91+
foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
92+
yield $attribute->newInstance();
93+
}
2694
}
2795
}

Tests/ConstraintTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function testOptionsWithInvalidInternalPointer()
245245
$this->assertEquals('foo', $constraint->property1);
246246
}
247247

248-
public function testAnnotationSetUndefinedDefaultOption()
248+
public function testAttributeSetUndefinedDefaultOption()
249249
{
250250
$this->expectException(ConstraintDefinitionException::class);
251251
$this->expectExceptionMessage('No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".');

Tests/Constraints/CallbackValidatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,22 @@ public function testConstraintGetTargets()
205205
$this->assertEquals($targets, $constraint->getTargets());
206206
}
207207

208-
// Should succeed. Needed when defining constraints as annotations.
208+
// Should succeed. Needed when defining constraints as attributes.
209209
public function testNoConstructorArguments()
210210
{
211211
$constraint = new Callback();
212212

213213
$this->assertSame([Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT], $constraint->getTargets());
214214
}
215215

216-
public function testAnnotationInvocationSingleValued()
216+
public function testAttributeInvocationSingleValued()
217217
{
218218
$constraint = new Callback(['value' => 'validateStatic']);
219219

220220
$this->assertEquals(new Callback('validateStatic'), $constraint);
221221
}
222222

223-
public function testAnnotationInvocationMultiValued()
223+
public function testAttributeInvocationMultiValued()
224224
{
225225
$constraint = new Callback(['value' => [__CLASS__.'_Class', 'validateCallback']]);
226226

Tests/Constraints/CountValidatorTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function testDefaultOption()
283283
$this->assertEquals(5, $constraint->max);
284284
}
285285

286-
public function testConstraintAnnotationDefaultOption()
286+
public function testConstraintAttributeDefaultOption()
287287
{
288288
$constraint = new Count(['value' => 5, 'exactMessage' => 'message']);
289289

Tests/Constraints/LengthTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testConstraintDefaultOption()
7070
self::assertEquals(5, $constraint->max);
7171
}
7272

73-
public function testConstraintAnnotationDefaultOption()
73+
public function testConstraintAttributeDefaultOption()
7474
{
7575
$constraint = new Length(['value' => 5, 'exactMessage' => 'message']);
7676

Tests/Constraints/WhenTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
2020
use Symfony\Component\Validator\Exception\MissingOptionsException;
2121
use Symfony\Component\Validator\Mapping\ClassMetadata;
22-
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
2322
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
2423
use Symfony\Component\Validator\Tests\Constraints\Fixtures\WhenTestWithAttributes;
2524

0 commit comments

Comments
 (0)