1616use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
1717use Symfony \Component \PropertyInfo \Type as PropertyInfoType ;
1818use Symfony \Component \Validator \Constraints \All ;
19+ use Symfony \Component \Validator \Constraints \DisableAutoMapping ;
20+ use Symfony \Component \Validator \Constraints \EnableAutoMapping ;
1921use Symfony \Component \Validator \Constraints \NotBlank ;
2022use Symfony \Component \Validator \Constraints \NotNull ;
2123use Symfony \Component \Validator \Constraints \Type ;
2830 */
2931final class PropertyInfoLoader implements LoaderInterface
3032{
33+ use AutoMappingTrait;
34+
3135 private $ listExtractor ;
3236 private $ typeExtractor ;
3337 private $ accessExtractor ;
@@ -47,14 +51,12 @@ public function __construct(PropertyListExtractorInterface $listExtractor, Prope
4751 public function loadClassMetadata (ClassMetadata $ metadata ): bool
4852 {
4953 $ className = $ metadata ->getClassName ();
50- if (null !== $ this ->classValidatorRegexp && !preg_match ($ this ->classValidatorRegexp , $ className )) {
51- return false ;
52- }
53-
5454 if (!$ properties = $ this ->listExtractor ->getProperties ($ className )) {
5555 return false ;
5656 }
5757
58+ $ loaded = false ;
59+ $ enabledForClass = $ this ->isAutoMappingEnabledForClass ($ metadata , $ this ->classValidatorRegexp );
5860 foreach ($ properties as $ property ) {
5961 if (false === $ this ->accessExtractor ->isWritable ($ className , $ property )) {
6062 continue ;
@@ -69,12 +71,22 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
6971 continue ;
7072 }
7173
74+ $ enabledForProperty = $ enabledForClass ;
7275 $ hasTypeConstraint = false ;
7376 $ hasNotNullConstraint = false ;
7477 $ hasNotBlankConstraint = false ;
7578 $ allConstraint = null ;
7679 foreach ($ metadata ->getPropertyMetadata ($ property ) as $ propertyMetadata ) {
7780 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+
7890 if ($ constraint instanceof Type) {
7991 $ hasTypeConstraint = true ;
8092 } elseif ($ constraint instanceof NotNull) {
@@ -87,6 +99,11 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
8799 }
88100 }
89101
102+ if (!$ enabledForProperty ) {
103+ continue ;
104+ }
105+
106+ $ loaded = true ;
90107 $ builtinTypes = [];
91108 $ nullable = false ;
92109 $ scalar = true ;
@@ -118,7 +135,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
118135 }
119136 }
120137
121- return true ;
138+ return $ loaded ;
122139 }
123140
124141 private function getTypeConstraint (string $ builtinType , PropertyInfoType $ type ): Type
0 commit comments