*/
-class AttributeLoader extends AnnotationLoader
+class AttributeLoader implements LoaderInterface
{
- public function __construct()
+ public function loadClassMetadata(ClassMetadata $metadata): bool
+ {
+ $reflClass = $metadata->getReflectionClass();
+ $className = $reflClass->name;
+ $success = false;
+
+ foreach ($this->getAttributes($reflClass) as $constraint) {
+ if ($constraint instanceof GroupSequence) {
+ $metadata->setGroupSequence($constraint->groups);
+ } elseif ($constraint instanceof GroupSequenceProvider) {
+ $metadata->setGroupSequenceProvider(true);
+ } elseif ($constraint instanceof Constraint) {
+ $metadata->addConstraint($constraint);
+ }
+
+ $success = true;
+ }
+
+ foreach ($reflClass->getProperties() as $property) {
+ if ($property->getDeclaringClass()->name === $className) {
+ foreach ($this->getAttributes($property) as $constraint) {
+ if ($constraint instanceof Constraint) {
+ $metadata->addPropertyConstraint($property->name, $constraint);
+ }
+
+ $success = true;
+ }
+ }
+ }
+
+ foreach ($reflClass->getMethods() as $method) {
+ if ($method->getDeclaringClass()->name === $className) {
+ foreach ($this->getAttributes($method) as $constraint) {
+ if ($constraint instanceof Callback) {
+ $constraint->callback = $method->getName();
+
+ $metadata->addConstraint($constraint);
+ } elseif ($constraint instanceof Constraint) {
+ if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
+ $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
+ } else {
+ 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));
+ }
+ }
+
+ $success = true;
+ }
+ }
+ }
+
+ return $success;
+ }
+
+ private function getAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionProperty $reflection): iterable
{
- parent::__construct(null);
+ foreach ($reflection->getAttributes(GroupSequence::class) as $attribute) {
+ yield $attribute->newInstance();
+ }
+ foreach ($reflection->getAttributes(GroupSequenceProvider::class) as $attribute) {
+ yield $attribute->newInstance();
+ }
+ foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
+ yield $attribute->newInstance();
+ }
}
}
diff --git a/src/Symfony/Component/Validator/Tests/ConstraintTest.php b/src/Symfony/Component/Validator/Tests/ConstraintTest.php
index 3d233c17815b..80e33c7b722a 100644
--- a/src/Symfony/Component/Validator/Tests/ConstraintTest.php
+++ b/src/Symfony/Component/Validator/Tests/ConstraintTest.php
@@ -245,7 +245,7 @@ public function testOptionsWithInvalidInternalPointer()
$this->assertEquals('foo', $constraint->property1);
}
- public function testAnnotationSetUndefinedDefaultOption()
+ public function testAttributeSetUndefinedDefaultOption()
{
$this->expectException(ConstraintDefinitionException::class);
$this->expectExceptionMessage('No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".');
diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php
index 084b192b6437..e888baa7a659 100644
--- a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php
+++ b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php
@@ -205,7 +205,7 @@ public function testConstraintGetTargets()
$this->assertEquals($targets, $constraint->getTargets());
}
- // Should succeed. Needed when defining constraints as annotations.
+ // Should succeed. Needed when defining constraints as attributes.
public function testNoConstructorArguments()
{
$constraint = new Callback();
@@ -213,14 +213,14 @@ public function testNoConstructorArguments()
$this->assertSame([Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT], $constraint->getTargets());
}
- public function testAnnotationInvocationSingleValued()
+ public function testAttributeInvocationSingleValued()
{
$constraint = new Callback(['value' => 'validateStatic']);
$this->assertEquals(new Callback('validateStatic'), $constraint);
}
- public function testAnnotationInvocationMultiValued()
+ public function testAttributeInvocationMultiValued()
{
$constraint = new Callback(['value' => [__CLASS__.'_Class', 'validateCallback']]);
diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php
index 104c90773264..c52cd4e69d39 100644
--- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php
+++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php
@@ -283,7 +283,7 @@ public function testDefaultOption()
$this->assertEquals(5, $constraint->max);
}
- public function testConstraintAnnotationDefaultOption()
+ public function testConstraintAttributeDefaultOption()
{
$constraint = new Count(['value' => 5, 'exactMessage' => 'message']);
diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php
index 793491fc29b4..03bd37674922 100644
--- a/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php
+++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php
@@ -70,7 +70,7 @@ public function testConstraintDefaultOption()
self::assertEquals(5, $constraint->max);
}
- public function testConstraintAnnotationDefaultOption()
+ public function testConstraintAttributeDefaultOption()
{
$constraint = new Length(['value' => 5, 'exactMessage' => 'message']);
diff --git a/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php b/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php
index 03a856c6ac43..12d2bd146dda 100644
--- a/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php
+++ b/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php
@@ -19,7 +19,6 @@
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\MissingOptionsException;
use Symfony\Component\Validator\Mapping\ClassMetadata;
-use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\WhenTestWithAttributes;
diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php
index c8975dbd2938..f9cb0da9b2d3 100644
--- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php
+++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php
@@ -29,7 +29,6 @@
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
-use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -37,7 +36,7 @@ class AttributeLoaderTest extends TestCase
{
public function testLoadClassMetadataReturnsTrueIfSuccessful()
{
- $loader = $this->createAnnotationLoader();
+ $loader = $this->createAttributeLoader();
$metadata = new ClassMetadata($this->getFixtureNamespace().'\Entity');
$this->assertTrue($loader->loadClassMetadata($metadata));
@@ -45,7 +44,7 @@ public function testLoadClassMetadataReturnsTrueIfSuccessful()
public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
{
- $loader = $this->createAnnotationLoader();
+ $loader = $this->createAttributeLoader();
$metadata = new ClassMetadata('\stdClass');
$this->assertFalse($loader->loadClassMetadata($metadata));
@@ -53,7 +52,7 @@ public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
public function testLoadClassMetadata()
{
- $loader = $this->createAnnotationLoader();
+ $loader = $this->createAttributeLoader();
$namespace = $this->getFixtureNamespace();
$metadata = new ClassMetadata($namespace.'\Entity');
@@ -105,11 +104,11 @@ public function testLoadClassMetadata()
}
/**
- * Test MetaData merge with parent annotation.
+ * Test MetaData merge with parent attribute.
*/
public function testLoadParentClassMetadata()
{
- $loader = $this->createAnnotationLoader();
+ $loader = $this->createAttributeLoader();
$namespace = $this->getFixtureNamespace();
// Load Parent MetaData
@@ -124,11 +123,11 @@ public function testLoadParentClassMetadata()
}
/**
- * Test MetaData merge with parent annotation.
+ * Test MetaData merge with parent attribute.
*/
public function testLoadClassMetadataAndMerge()
{
- $loader = $this->createAnnotationLoader();
+ $loader = $this->createAttributeLoader();
$namespace = $this->getFixtureNamespace();
// Load Parent MetaData
@@ -196,9 +195,9 @@ public function testLoadClassMetadataAndMerge()
$this->assertInstanceOf(NotNull::class, $otherMetadata[1]->getConstraints()[0]);
}
- public function testLoadGroupSequenceProviderAnnotation()
+ public function testLoadGroupSequenceProviderAttribute()
{
- $loader = $this->createAnnotationLoader();
+ $loader = $this->createAttributeLoader();
$namespace = $this->getFixtureNamespace();
$metadata = new ClassMetadata($namespace.'\GroupSequenceProviderEntity');
@@ -211,7 +210,7 @@ public function testLoadGroupSequenceProviderAnnotation()
$this->assertEquals($expected, $metadata);
}
- protected function createAnnotationLoader(): AnnotationLoader
+ protected function createAttributeLoader(): AttributeLoader
{
return new AttributeLoader();
}
diff --git a/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php b/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
index f2253697cdd8..c57a507e2557 100644
--- a/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
+++ b/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
@@ -13,7 +13,6 @@
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemPoolInterface;
-use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ObjectInitializerInterface;
use Symfony\Component\Validator\Validator\RecursiveValidator;
@@ -22,8 +21,6 @@
class ValidatorBuilderTest extends TestCase
{
- use ExpectDeprecationTrait;
-
private ValidatorBuilder $builder;
protected function setUp(): void
@@ -73,24 +70,6 @@ public function testAddMethodMappings()
$this->assertSame($this->builder, $this->builder->addMethodMappings([]));
}
- /**
- * @group legacy
- */
- public function testExpectDeprecationWhenEnablingAnnotationMapping()
- {
- $this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::enableAnnotationMapping()" is deprecated, use "enableAttributeMapping()" instead.');
- $this->assertSame($this->builder, $this->builder->enableAnnotationMapping());
- }
-
- /**
- * @group legacy
- */
- public function testExpectDeprecationWhenDisablingAnnotationMapping()
- {
- $this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::disableAnnotationMapping()" is deprecated, use "disableAttributeMapping()" instead.');
- $this->assertSame($this->builder, $this->builder->disableAnnotationMapping());
- }
-
public function testDisableAttributeMapping()
{
$this->assertSame($this->builder, $this->builder->disableAttributeMapping());
diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php
index ec7e6e77bc7e..44f7161fac1e 100644
--- a/src/Symfony/Component/Validator/ValidatorBuilder.php
+++ b/src/Symfony/Component/Validator/ValidatorBuilder.php
@@ -16,7 +16,6 @@
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
-use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
@@ -181,18 +180,6 @@ public function addMethodMappings(array $methodNames): static
return $this;
}
- /**
- * @deprecated since Symfony 6.4, use "enableAttributeMapping()" instead.
- *
- * @return $this
- */
- public function enableAnnotationMapping(): static
- {
- trigger_deprecation('symfony/validator', '6.4', 'Method "%s()" is deprecated, use "enableAttributeMapping()" instead.', __METHOD__);
-
- return $this->enableAttributeMapping();
- }
-
/**
* Enables attribute-based constraint mapping.
*
@@ -209,18 +196,6 @@ public function enableAttributeMapping(): static
return $this;
}
- /**
- * @deprecated since Symfony 6.4, use "disableAttributeMapping()" instead
- *
- * @return $this
- */
- public function disableAnnotationMapping(): static
- {
- trigger_deprecation('symfony/validator', '6.4', 'Method "%s()" is deprecated, use "disableAttributeMapping()" instead.', __METHOD__);
-
- return $this->disableAttributeMapping();
- }
-
/**
* Disables attribute-based constraint mapping.
*
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