Skip to content

Commit a1764e8

Browse files
committed
Introduce EnableAutoMapping and DisableAutoMapping
1 parent 4fd4d13 commit a1764e8

File tree

14 files changed

+114
-45
lines changed

14 files changed

+114
-45
lines changed

src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
7171

7272
/**
7373
* @ORM\Column(length=10)
74-
* @Assert\AutoMapping(false)
74+
* @Assert\DisableAutoMapping
7575
*/
7676
public $noAutoMapping;
7777

src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderNoAutoMappingEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* @ORM\Entity
19-
* @Assert\AutoMapping(false)
19+
* @Assert\DisableAutoMapping
2020
*
2121
* @author Kévin Dunglas <dunglas@gmail.com>
2222
*/
@@ -34,7 +34,7 @@ class DoctrineLoaderNoAutoMappingEntity
3434
public $maxLength;
3535

3636
/**
37-
* @Assert\AutoMapping(true)
37+
* @Assert\EnableAutoMapping
3838
* @ORM\Column(length=20)
3939
*/
4040
public $autoMappingExplicitlyEnabled;

src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderParentEntity;
2222
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
2323
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
24-
use Symfony\Component\Validator\Constraints\AutoMapping;
24+
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
2525
use Symfony\Component\Validator\Constraints\Length;
2626
use Symfony\Component\Validator\Mapping\CascadingStrategy;
2727
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -145,7 +145,7 @@ public function testLoadClassMetadata()
145145
$this->assertCount(1, $noAutoMappingMetadata);
146146
$noAutoMappingConstraints = $noAutoMappingMetadata[0]->getConstraints();
147147
$this->assertCount(1, $noAutoMappingConstraints);
148-
$this->assertInstanceOf(AutoMapping::class, $noAutoMappingConstraints[0]);
148+
$this->assertInstanceOf(DisableAutoMapping::class, $noAutoMappingConstraints[0]);
149149
}
150150

151151
public function testFieldMappingsConfiguration()
@@ -187,7 +187,7 @@ public function testClassValidator(bool $expected, string $classValidatorRegexp
187187
public function regexpProvider()
188188
{
189189
return [
190-
[false, null],
190+
[true, null],
191191
[true, '{^'.preg_quote(DoctrineLoaderEntity::class).'$|^'.preg_quote(Entity::class).'$}'],
192192
[false, '{^'.preg_quote(Entity::class).'$}'],
193193
];
@@ -208,7 +208,7 @@ public function testClassNoAutoMapping()
208208

209209
$classConstraints = $classMetadata->getConstraints();
210210
$this->assertCount(1, $classConstraints);
211-
$this->assertInstanceOf(AutoMapping::class, $classConstraints[0]);
211+
$this->assertInstanceOf(DisableAutoMapping::class, $classConstraints[0]);
212212

213213
$maxLengthMetadata = $classMetadata->getPropertyMetadata('maxLength');
214214
$this->assertEmpty($maxLengthMetadata);

src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1717
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1818
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19-
use Symfony\Component\Validator\Constraints\AutoMapping;
19+
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
20+
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
2021
use Symfony\Component\Validator\Constraints\Length;
2122
use Symfony\Component\Validator\Constraints\Valid;
2223
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -77,12 +78,9 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
7778
foreach ($metadata->getPropertyMetadata($mapping['fieldName']) as $propertyMetadata) {
7879
foreach ($propertyMetadata->getConstraints() as $constraint) {
7980
// Enabling or disabling auto-mapping explicitly always takes precedence
80-
if ($constraint instanceof AutoMapping) {
81-
// Enabling or disabling auto-mapping explicitly always takes precedence
82-
if (!$constraint->enabled) {
83-
continue 3;
84-
}
85-
81+
if ($constraint instanceof DisableAutoMapping) {
82+
continue 3;
83+
} elseif ($constraint instanceof EnableAutoMapping) {
8684
$enabledForProperty = true;
8785
} elseif ($constraint instanceof Length) {
8886
$lengthConstraint = $constraint;

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `AutoMapping` constraint to enable or disable auto mapping for class or a property
7+
* added `EnableAutoMapping` and `DisableAutoMapping` constraints to enable or disable auto mapping for class or a property
88
* using anything else than a `string` as the code of a `ConstraintViolation` is deprecated, a `string` type-hint will
99
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
1010
method in 5.0

src/Symfony/Component/Validator/Constraints/AutoMapping.php renamed to src/Symfony/Component/Validator/Constraints/DisableAutoMapping.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1616

1717
/**
18-
* Enables or disables auto mapping.
18+
* Disables auto mapping.
1919
*
2020
* Using the annotations on a property has higher precedence than using it on a class,
2121
* which has higher precedence than any configuration that might be defined outside the class.
@@ -24,10 +24,8 @@
2424
*
2525
* @author Kévin Dunglas <dunglas@gmail.com>
2626
*/
27-
class AutoMapping extends Constraint
27+
class DisableAutoMapping extends Constraint
2828
{
29-
public $enabled = true;
30-
3129
public function __construct($options = null)
3230
{
3331
if (\is_array($options) && \array_key_exists('groups', $options)) {
@@ -44,12 +42,4 @@ public function getTargets()
4442
{
4543
return [self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT];
4644
}
47-
48-
/**
49-
* {@inheritdoc}
50-
*/
51-
public function getDefaultOption(): ?string
52-
{
53-
return 'enabled';
54-
}
5545
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16+
17+
/**
18+
* Enables auto mapping.
19+
*
20+
* Using the annotations on a property has higher precedence than using it on a class,
21+
* which has higher precedence than any configuration that might be defined outside the class.
22+
*
23+
* @Annotation
24+
*
25+
* @author Kévin Dunglas <dunglas@gmail.com>
26+
*/
27+
class EnableAutoMapping extends Constraint
28+
{
29+
public function __construct($options = null)
30+
{
31+
if (\is_array($options) && \array_key_exists('groups', $options)) {
32+
throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__));
33+
}
34+
35+
parent::__construct($options);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getTargets()
42+
{
43+
return [self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT];
44+
}
45+
}

src/Symfony/Component/Validator/Mapping/Loader/AutoMappingTrait.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

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

14-
use Symfony\Component\Validator\Constraints\AutoMapping;
14+
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
15+
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
1516
use Symfony\Component\Validator\Mapping\ClassMetadata;
1617

1718
/**
@@ -25,8 +26,12 @@ private function isAutoMappingEnabledForClass(ClassMetadata $metadata, string $c
2526
{
2627
// Check if AutoMapping constraint is set first
2728
foreach ($metadata->getConstraints() as $constraint) {
28-
if ($constraint instanceof AutoMapping) {
29-
return $constraint->enabled;
29+
if ($constraint instanceof DisableAutoMapping) {
30+
return false;
31+
}
32+
33+
if ($constraint instanceof EnableAutoMapping) {
34+
return true;
3035
}
3136
}
3237

src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1717
use Symfony\Component\PropertyInfo\Type as PropertyInfoType;
1818
use Symfony\Component\Validator\Constraints\All;
19-
use Symfony\Component\Validator\Constraints\AutoMapping;
19+
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
20+
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
2021
use Symfony\Component\Validator\Constraints\NotBlank;
2122
use Symfony\Component\Validator\Constraints\NotNull;
2223
use Symfony\Component\Validator\Constraints\Type;
@@ -77,12 +78,12 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
7778
$allConstraint = null;
7879
foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) {
7980
foreach ($propertyMetadata->getConstraints() as $constraint) {
80-
if ($constraint instanceof AutoMapping) {
81-
// Enabling or disabling auto-mapping explicitly always takes precedence
82-
if (!$constraint->enabled) {
83-
continue 3;
84-
}
81+
// Enabling or disabling auto-mapping explicitly always takes precedence
82+
if ($constraint instanceof DisableAutoMapping) {
83+
continue 3;
84+
}
8585

86+
if ($constraint instanceof EnableAutoMapping) {
8687
$enabledForProperty = true;
8788
}
8889

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
16+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17+
18+
/**
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class DisableAutoMappingTest extends TestCase
22+
{
23+
public function testGroups()
24+
{
25+
$this->expectException(ConstraintDefinitionException::class);
26+
$this->expectExceptionMessage(sprintf('The option "groups" is not supported by the constraint "%s".', DisableAutoMapping::class));
27+
28+
new DisableAutoMapping(['groups' => 'foo']);
29+
}
30+
}

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