16
16
use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
17
17
use Symfony \Component \PropertyInfo \Type as PropertyInfoType ;
18
18
use Symfony \Component \Validator \Constraints \All ;
19
+ use Symfony \Component \Validator \Constraints \DisableAutoMapping ;
20
+ use Symfony \Component \Validator \Constraints \EnableAutoMapping ;
19
21
use Symfony \Component \Validator \Constraints \NotBlank ;
20
22
use Symfony \Component \Validator \Constraints \NotNull ;
21
23
use Symfony \Component \Validator \Constraints \Type ;
28
30
*/
29
31
final class PropertyInfoLoader implements LoaderInterface
30
32
{
33
+ use AutoMappingTrait;
34
+
31
35
private $ listExtractor ;
32
36
private $ typeExtractor ;
33
37
private $ accessExtractor ;
@@ -47,14 +51,12 @@ public function __construct(PropertyListExtractorInterface $listExtractor, Prope
47
51
public function loadClassMetadata (ClassMetadata $ metadata ): bool
48
52
{
49
53
$ className = $ metadata ->getClassName ();
50
- if (null !== $ this ->classValidatorRegexp && !preg_match ($ this ->classValidatorRegexp , $ className )) {
51
- return false ;
52
- }
53
-
54
54
if (!$ properties = $ this ->listExtractor ->getProperties ($ className )) {
55
55
return false ;
56
56
}
57
57
58
+ $ loaded = false ;
59
+ $ enabledForClass = $ this ->isAutoMappingEnabledForClass ($ metadata , $ this ->classValidatorRegexp );
58
60
foreach ($ properties as $ property ) {
59
61
if (false === $ this ->accessExtractor ->isWritable ($ className , $ property )) {
60
62
continue ;
@@ -69,12 +71,22 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
69
71
continue ;
70
72
}
71
73
74
+ $ enabledForProperty = $ enabledForClass ;
72
75
$ hasTypeConstraint = false ;
73
76
$ hasNotNullConstraint = false ;
74
77
$ hasNotBlankConstraint = false ;
75
78
$ allConstraint = null ;
76
79
foreach ($ metadata ->getPropertyMetadata ($ property ) as $ propertyMetadata ) {
77
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
+ }
85
+
86
+ if ($ constraint instanceof EnableAutoMapping) {
87
+ $ enabledForProperty = true ;
88
+ }
89
+
78
90
if ($ constraint instanceof Type) {
79
91
$ hasTypeConstraint = true ;
80
92
} elseif ($ constraint instanceof NotNull) {
@@ -87,6 +99,11 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
87
99
}
88
100
}
89
101
102
+ if (!$ enabledForProperty ) {
103
+ continue ;
104
+ }
105
+
106
+ $ loaded = true ;
90
107
$ builtinTypes = [];
91
108
$ nullable = false ;
92
109
$ scalar = true ;
@@ -118,7 +135,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
118
135
}
119
136
}
120
137
121
- return true ;
138
+ return $ loaded ;
122
139
}
123
140
124
141
private function getTypeConstraint (string $ builtinType , PropertyInfoType $ type ): Type
0 commit comments