Skip to content

Commit 27254bc

Browse files
Merge branch '4.4' into 5.0
* 4.4: [Validator] Fix auto-mapping constraints should not be validated [Debug] Updated the README to deprecate the component [Cache] fix memory leak when using PhpFilesAdapter [Yaml] Implement multiline string as scalar block for tagged values [HttpFoundation] Use `Cache-Control: must-revalidate` only if explicit lifetime has been given [FrameworkBundle] Use UserInterface to @return in getUser method [CI] Replace php7.4snapshot with php7.4 in Travis configuration [ExpressionLanguage][Node][BinaryNode] Process division by zero Fixing bad order of operations with null coalescing operator forward caught exception [Validator][ConstraintValidator] Stop passing unnecessary timezone argument to \DateTime add tags before processing them [FrameworkBundle][ContainerLintCommand] Reinitialize bundles when the container is reprepared [Process] change the syntax of portable prepared command lines [MonologBridge] Fix debug processor datetime type
2 parents b816858 + 894ca8a commit 27254bc

File tree

7 files changed

+100
-32
lines changed

7 files changed

+100
-32
lines changed

ConstraintValidator.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ protected function formatValue($value, int $format = 0)
9393
// neither the native nor the stub IntlDateFormatter support
9494
// DateTimeImmutable as of yet
9595
if (!$value instanceof \DateTime) {
96-
$value = new \DateTime(
97-
$value->format('Y-m-d H:i:s.u e'),
98-
$value->getTimezone()
99-
);
96+
$value = new \DateTime($value->format('Y-m-d H:i:s.u e'));
10097
}
10198

10299
return $formatter->format($value);

Mapping/AutoMappingStrategy.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mapping;
13+
14+
/**
15+
* Specifies how the auto-mapping feature should behave.
16+
*
17+
* @author Maxime Steinhausser <[email protected]>
18+
*/
19+
final class AutoMappingStrategy
20+
{
21+
/**
22+
* Nothing explicitly set, rely on auto-mapping configured regex.
23+
*/
24+
public const NONE = 0;
25+
26+
/**
27+
* Explicitly enabled.
28+
*/
29+
public const ENABLED = 1;
30+
31+
/**
32+
* Explicitly disabled.
33+
*/
34+
public const DISABLED = 2;
35+
36+
/**
37+
* Not instantiable.
38+
*/
39+
private function __construct()
40+
{
41+
}
42+
}

Mapping/GenericMetadata.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
namespace Symfony\Component\Validator\Mapping;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
<<<<<<< HEAD
16+
=======
17+
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
18+
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
19+
use Symfony\Component\Validator\Constraints\Length;
20+
use Symfony\Component\Validator\Constraints\NotBlank;
21+
>>>>>>> 4.4
1522
use Symfony\Component\Validator\Constraints\Traverse;
1623
use Symfony\Component\Validator\Constraints\Valid;
1724
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -73,6 +80,19 @@ class GenericMetadata implements MetadataInterface
7380
*/
7481
public $traversalStrategy = TraversalStrategy::NONE;
7582

83+
/**
84+
* Is auto-mapping enabled?
85+
*
86+
* @var int
87+
*
88+
* @see AutoMappingStrategy
89+
*
90+
* @internal This property is public in order to reduce the size of the
91+
* class' serialized representation. Do not access it. Use
92+
* {@link getAutoMappingStrategy()} instead.
93+
*/
94+
public $autoMappingStrategy = AutoMappingStrategy::NONE;
95+
7696
/**
7797
* Returns the names of the properties that should be serialized.
7898
*
@@ -85,6 +105,7 @@ public function __sleep()
85105
'constraintsByGroup',
86106
'cascadingStrategy',
87107
'traversalStrategy',
108+
'autoMappingStrategy',
88109
];
89110
}
90111

@@ -137,6 +158,13 @@ public function addConstraint(Constraint $constraint)
137158
return $this;
138159
}
139160

161+
if ($constraint instanceof DisableAutoMapping || $constraint instanceof EnableAutoMapping) {
162+
$this->autoMappingStrategy = $constraint instanceof EnableAutoMapping ? AutoMappingStrategy::ENABLED : AutoMappingStrategy::DISABLED;
163+
164+
// The constraint is not added
165+
return $this;
166+
}
167+
140168
$this->constraints[] = $constraint;
141169

142170
foreach ($constraint->groups as $group) {
@@ -205,4 +233,12 @@ public function getTraversalStrategy()
205233
{
206234
return $this->traversalStrategy;
207235
}
236+
237+
/**
238+
* @see AutoMappingStrategy
239+
*/
240+
public function getAutoMappingStrategy(): int
241+
{
242+
return $this->autoMappingStrategy;
243+
}
208244
}

Mapping/Loader/AutoMappingTrait.php

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

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

14-
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
15-
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
14+
use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
1615
use Symfony\Component\Validator\Mapping\ClassMetadata;
1716

1817
/**
@@ -25,14 +24,8 @@ trait AutoMappingTrait
2524
private function isAutoMappingEnabledForClass(ClassMetadata $metadata, string $classValidatorRegexp = null): bool
2625
{
2726
// Check if AutoMapping constraint is set first
28-
foreach ($metadata->getConstraints() as $constraint) {
29-
if ($constraint instanceof DisableAutoMapping) {
30-
return false;
31-
}
32-
33-
if ($constraint instanceof EnableAutoMapping) {
34-
return true;
35-
}
27+
if (AutoMappingStrategy::NONE !== $strategy = $metadata->getAutoMappingStrategy()) {
28+
return AutoMappingStrategy::ENABLED === $strategy;
3629
}
3730

3831
// Fallback on the config

Mapping/Loader/PropertyInfoLoader.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
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\DisableAutoMapping;
20-
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
2119
use Symfony\Component\Validator\Constraints\NotBlank;
2220
use Symfony\Component\Validator\Constraints\NotNull;
2321
use Symfony\Component\Validator\Constraints\Type;
22+
use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
2423
use Symfony\Component\Validator\Mapping\ClassMetadata;
2524

2625
/**
@@ -77,16 +76,16 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
7776
$hasNotBlankConstraint = false;
7877
$allConstraint = null;
7978
foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) {
80-
foreach ($propertyMetadata->getConstraints() as $constraint) {
81-
// Enabling or disabling auto-mapping explicitly always takes precedence
82-
if ($constraint instanceof DisableAutoMapping) {
83-
continue 3;
84-
}
79+
// Enabling or disabling auto-mapping explicitly always takes precedence
80+
if (AutoMappingStrategy::DISABLED === $propertyMetadata->getAutoMappingStrategy()) {
81+
continue 2;
82+
}
8583

86-
if ($constraint instanceof EnableAutoMapping) {
87-
$enabledForProperty = true;
88-
}
84+
if (AutoMappingStrategy::ENABLED === $propertyMetadata->getAutoMappingStrategy()) {
85+
$enabledForProperty = true;
86+
}
8987

88+
foreach ($propertyMetadata->getConstraints() as $constraint) {
9089
if ($constraint instanceof Type) {
9190
$hasTypeConstraint = true;
9291
} elseif ($constraint instanceof NotNull) {

Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ protected static function addPhp5Dot5Comparisons(array $comparisons)
5353

5454
foreach ($comparison as $i => $value) {
5555
if ($value instanceof \DateTime) {
56-
$comparison[$i] = new \DateTimeImmutable(
57-
$value->format('Y-m-d H:i:s.u e'),
58-
$value->getTimezone()
59-
);
56+
$comparison[$i] = new \DateTimeImmutable($value->format('Y-m-d H:i:s.u e'));
6057
$add = true;
6158
} elseif ('DateTime' === $value) {
6259
$comparison[$i] = 'DateTimeImmutable';

Tests/Mapping/Loader/PropertyInfoLoaderTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
1616
use Symfony\Component\PropertyInfo\Type;
1717
use Symfony\Component\Validator\Constraints\All;
18-
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
1918
use Symfony\Component\Validator\Constraints\Iban;
2019
use Symfony\Component\Validator\Constraints\NotBlank;
2120
use Symfony\Component\Validator\Constraints\NotNull;
2221
use Symfony\Component\Validator\Constraints\Type as TypeConstraint;
22+
use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
2323
use Symfony\Component\Validator\Mapping\ClassMetadata;
2424
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
25+
use Symfony\Component\Validator\Mapping\PropertyMetadata;
2526
use Symfony\Component\Validator\Tests\Fixtures\Entity;
2627
use Symfony\Component\Validator\Tests\Fixtures\PropertyInfoLoaderEntity;
2728
use Symfony\Component\Validator\Tests\Fixtures\PropertyInfoLoaderNoAutoMappingEntity;
@@ -164,11 +165,12 @@ public function testLoadClassMetadata()
164165
$readOnlyMetadata = $classMetadata->getPropertyMetadata('readOnly');
165166
$this->assertEmpty($readOnlyMetadata);
166167

168+
/** @var PropertyMetadata[] $noAutoMappingMetadata */
167169
$noAutoMappingMetadata = $classMetadata->getPropertyMetadata('noAutoMapping');
168170
$this->assertCount(1, $noAutoMappingMetadata);
171+
$this->assertSame(AutoMappingStrategy::DISABLED, $noAutoMappingMetadata[0]->getAutoMappingStrategy());
169172
$noAutoMappingConstraints = $noAutoMappingMetadata[0]->getConstraints();
170-
$this->assertCount(1, $noAutoMappingConstraints);
171-
$this->assertInstanceOf(DisableAutoMapping::class, $noAutoMappingConstraints[0]);
173+
$this->assertCount(0, $noAutoMappingConstraints, 'DisableAutoMapping constraint is not added in the list');
172174
}
173175

174176
/**
@@ -222,8 +224,10 @@ public function testClassNoAutoMapping()
222224
->getValidator()
223225
;
224226

227+
/** @var ClassMetadata $classMetadata */
225228
$classMetadata = $validator->getMetadataFor(new PropertyInfoLoaderNoAutoMappingEntity());
226229
$this->assertEmpty($classMetadata->getPropertyMetadata('string'));
227-
$this->assertCount(3, $classMetadata->getPropertyMetadata('autoMappingExplicitlyEnabled')[0]->constraints);
230+
$this->assertCount(2, $classMetadata->getPropertyMetadata('autoMappingExplicitlyEnabled')[0]->constraints);
231+
$this->assertSame(AutoMappingStrategy::ENABLED, $classMetadata->getPropertyMetadata('autoMappingExplicitlyEnabled')[0]->getAutoMappingStrategy());
228232
}
229233
}

0 commit comments

Comments
 (0)