Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Normalizer/AbstractNormalizer.php

raw symbols SCIP
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\Serializer\Normalizer;
13
14 use Symfony\Component\Serializer\Exception\CircularReferenceException;
15 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16 use Symfony\Component\Serializer\Exception\LogicException;
17 use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
18 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
19 use Symfony\Component\Serializer\Exception\RuntimeException;
20 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
21 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
22 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23 use Symfony\Component\Serializer\SerializerAwareInterface;
24 use Symfony\Component\Serializer\SerializerAwareTrait;
25
26 /**
27 * Normalizer implementation.
28 *
29 * @author Kévin Dunglas <[email protected]>
30 */
31 abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
32 {
33 use ObjectToPopulateTrait;
34 use SerializerAwareTrait;
35
36 /* constants to configure the context */
37
38 /**
39 * How many loops of circular reference to allow while normalizing.
40 *
41 * The default value of 1 means that when we encounter the same object a
42 * second time, we consider that a circular reference.
43 *
44 * You can raise this value for special cases, e.g. in combination with the
45 * max depth setting of the object normalizer.
46 */
47 public const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
48
49 /**
50 * Instead of creating a new instance of an object, update the specified object.
51 *
52 * If you have a nested structure, child objects will be overwritten with
53 * new instances unless you set DEEP_OBJECT_TO_POPULATE to true.
54 */
55 public const OBJECT_TO_POPULATE = 'object_to_populate';
56
57 /**
58 * Only (de)normalize attributes that are in the specified groups.
59 */
60 public const GROUPS = 'groups';
61
62 /**
63 * Limit (de)normalize to the specified names.
64 *
65 * For nested structures, this list needs to reflect the object tree.
66 */
67 public const ATTRIBUTES = 'attributes';
68
69 /**
70 * If ATTRIBUTES are specified, and the source has fields that are not part of that list,
71 * either ignore those attributes (true) or throw an ExtraAttributesException (false).
72 */
73 public const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
74
75 /**
76 * Hashmap of default values for constructor arguments.
77 *
78 * The names need to match the parameter names in the constructor arguments.
79 */
80 public const DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments';
81
82 /**
83 * Hashmap of field name => callable to (de)normalize this field.
84 *
85 * The callable is called if the field is encountered with the arguments:
86 *
87 * - mixed $attributeValue value of this field
88 * - object|string $object the whole object being normalized or the object's class being denormalized
89 * - string $attributeName name of the attribute being (de)normalized
90 * - string $format the requested format
91 * - array $context the serialization context
92 */
93 public const CALLBACKS = 'callbacks';
94
95 /**
96 * Handler to call when a circular reference has been detected.
97 *
98 * If you specify no handler, a CircularReferenceException is thrown.
99 *
100 * The method will be called with ($object, $format, $context) and its
101 * return value is returned as the result of the normalize call.
102 */
103 public const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
104
105 /**
106 * Skip the specified attributes when normalizing an object tree.
107 *
108 * This list is applied to each element of nested structures.
109 *
110 * Note: The behaviour for nested structures is different from ATTRIBUTES
111 * for historical reason. Aligning the behaviour would be a BC break.
112 */
113 public const IGNORED_ATTRIBUTES = 'ignored_attributes';
114
115 /**
116 * Require all properties to be listed in the input instead of falling
117 * back to null for nullable ones.
118 */
119 public const REQUIRE_ALL_PROPERTIES = 'require_all_properties';
120
121 /**
122 * Flag to control whether a non-boolean value should be filtered using the
123 * filter_var function with the {@see https://www.php.net/manual/fr/filter.filters.validate.php}
124 * \FILTER_VALIDATE_BOOL filter before casting it to a boolean.
125 *
126 * "0", "false", "off", "no" and "" will be cast to false.
127 * "1", "true", "on" and "yes" will be cast to true.
128 */
129 public const FILTER_BOOL = 'filter_bool';
130
131 /**
132 * @internal
133 */
134 protected const CIRCULAR_REFERENCE_LIMIT_COUNTERS = 'circular_reference_limit_counters';
135
136 protected array $defaultContext = [
137 self::ALLOW_EXTRA_ATTRIBUTES => true,
138 self::CIRCULAR_REFERENCE_HANDLER => null,
139 self::CIRCULAR_REFERENCE_LIMIT => 1,
140 self::IGNORED_ATTRIBUTES => [],
141 ];
142
143 /**
144 * Sets the {@link ClassMetadataFactoryInterface} to use.
145 */
146 public function __construct(
147 protected ?ClassMetadataFactoryInterface $classMetadataFactory = null,
148 protected ?NameConverterInterface $nameConverter = null,
149 array $defaultContext = [],
150 ) {
151 $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
152
153 $this->validateCallbackContext($this->defaultContext, 'default');
154
155 if (isset($this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER]) && !\is_callable($this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER])) {
156 throw new InvalidArgumentException(\sprintf('Invalid callback found in the "%s" default context option.', self::CIRCULAR_REFERENCE_HANDLER));
157 }
158 }
159
160 /**
161 * Detects if the configured circular reference limit is reached.
162 *
163 * @throws CircularReferenceException
164 */
165 protected function isCircularReference(object $object, array &$context): bool
166 {
167 $objectId = spl_object_id($object);
168
169 $circularReferenceLimit = $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT];
170 if (isset($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId])) {
171 if ($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId] >= $circularReferenceLimit) {
172 unset($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId]);
173
174 return true;
175 }
176
177 ++$context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId];
178 } else {
179 $context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId] = 1;
180 }
181
182 return false;
183 }
184
185 /**
186 * Handles a circular reference.
187 *
188 * If a circular reference handler is set, it will be called. Otherwise, a
189 * {@class CircularReferenceException} will be thrown.
190 *
191 * @final
192 *
193 * @throws CircularReferenceException
194 */
195 protected function handleCircularReference(object $object, ?string $format = null, array $context = []): mixed
196 {
197 $circularReferenceHandler = $context[self::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER];
198 if ($circularReferenceHandler) {
199 return $circularReferenceHandler($object, $format, $context);
200 }
201
202 throw new CircularReferenceException(\sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', get_debug_type($object), $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT]));
203 }
204
205 /**
206 * Gets attributes to normalize using groups.
207 *
208 * @param bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
209 *
210 * @return string[]|AttributeMetadataInterface[]|bool
211 *
212 * @throws LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided
213 */
214 protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
215 {
216 $allowExtraAttributes = $context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES];
217 if (!$this->classMetadataFactory) {
218 if (!$allowExtraAttributes) {
219 throw new LogicException(\sprintf('A class metadata factory must be provided in the constructor when setting "%s" to false.', self::ALLOW_EXTRA_ATTRIBUTES));
220 }
221
222 return false;
223 }
224
225 $groups = $this->getGroups($context);
226
227 $allowedAttributes = [];
228 $ignoreUsed = false;
229
230 foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
231 if ($ignore = $attributeMetadata->isIgnored()) {
232 $ignoreUsed = true;
233 }
234
235 // If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
236 if (
237 !$ignore
238 && ([] === $groups || \in_array('*', $groups, true) || array_intersect($attributeMetadata->getGroups(), $groups))
239 && $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)
240 ) {
241 $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
242 }
243 }
244
245 if (!$ignoreUsed && $allowExtraAttributes) {
246 if ([] === $groups) {
247 return false;
248 }
249
250 if ([] === $allowedAttributes && \in_array('*', $groups, true)) {
251 return false;
252 }
253 }
254
255 return $allowedAttributes;
256 }
257
258 protected function getGroups(array $context): array
259 {
260 $groups = $context[self::GROUPS] ?? $this->defaultContext[self::GROUPS] ?? [];
261
262 return \is_scalar($groups) ? (array) $groups : $groups;
263 }
264
265 /**
266 * Is this attribute allowed?
267 */
268 protected function isAllowedAttribute(object|string $classOrObject, string $attribute, ?string $format = null, array $context = []): bool
269 {
270 $ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES];
271 if (\in_array($attribute, $ignoredAttributes, true)) {
272 return false;
273 }
274
275 $attributes = $context[self::ATTRIBUTES] ?? $this->defaultContext[self::ATTRIBUTES] ?? null;
276 if (isset($attributes[$attribute])) {
277 // Nested attributes
278 return true;
279 }
280
281 if (\is_array($attributes)) {
282 return \in_array($attribute, $attributes, true);
283 }
284
285 return true;
286 }
287
288 /**
289 * Normalizes the given data to an array. It's particularly useful during
290 * the denormalization process.
291 */
292 protected function prepareForDenormalization(mixed $data): array
293 {
294 return (array) $data;
295 }
296
297 /**
298 * Returns the method to use to construct an object. This method must be either
299 * the object constructor or static.
300 */
301 protected function getConstructor(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes): ?\ReflectionMethod
302 {
303 return $reflectionClass->getConstructor();
304 }
305
306 /**
307 * Instantiates an object using constructor parameters when needed.
308 *
309 * This method also allows to denormalize data into an existing object if
310 * it is present in the context with the object_to_populate. This object
311 * is removed from the context before being returned to avoid side effects
312 * when recursively normalizing an object graph.
313 *
314 * @throws RuntimeException
315 * @throws MissingConstructorArgumentsException
316 */
317 protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, ?string $format = null): object
318 {
319 if (null !== $object = $this->extractObjectToPopulate($class, $context, self::OBJECT_TO_POPULATE)) {
320 unset($context[self::OBJECT_TO_POPULATE]);
321
322 return $object;
323 }
324 // clean up even if no match
325 unset($context[static::OBJECT_TO_POPULATE]);
326
327 $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
328 if ($constructor) {
329 if (true !== $constructor->isPublic()) {
330 return $reflectionClass->newInstanceWithoutConstructor();
331 }
332
333 $constructorParameters = $constructor->getParameters();
334 $missingConstructorArguments = [];
335 $params = [];
336 $unsetKeys = [];
337 $collectedErrorCountBeforeConstructor = \count($context['not_normalizable_value_exceptions'] ?? []);
338
339 foreach ($constructorParameters as $constructorParameter) {
340 $paramName = $constructorParameter->name;
341 $attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
342 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
343
344 $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes, true);
345 $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
346 if ($constructorParameter->isVariadic()) {
347 if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
348 if (!\is_array($data[$key])) {
349 throw new RuntimeException(\sprintf('Cannot create an instance of "%s" from serialized data because the variadic parameter "%s" can only accept an array.', $class, $constructorParameter->name));
350 }
351
352 $variadicParameters = [];
353 foreach ($data[$key] as $parameterKey => $parameterData) {
354 try {
355 $variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
356 } catch (NotNormalizableValueException $exception) {
357 if (!isset($context['not_normalizable_value_exceptions'])) {
358 throw $exception;
359 }
360
361 $context['not_normalizable_value_exceptions'][] = $exception;
362 $params[$paramName] = $parameterData;
363 }
364 }
365
366 $params = array_merge(array_values($params), $variadicParameters);
367 $unsetKeys[] = $key;
368 }
369 } elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
370 $parameterData = $data[$key];
371 if (null === $parameterData && $constructorParameter->allowsNull()) {
372 $params[$paramName] = null;
373 $unsetKeys[] = $key;
374
375 continue;
376 }
377
378 try {
379 $params[$paramName] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
380 } catch (NotNormalizableValueException $exception) {
381 if (!isset($context['not_normalizable_value_exceptions'])) {
382 throw $exception;
383 }
384
385 $context['not_normalizable_value_exceptions'][] = $exception;
386 $params[$paramName] = $parameterData;
387 }
388
389 $unsetKeys[] = $key;
390 } elseif (\array_key_exists($key, $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
391 $params[$paramName] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
392 } elseif (\array_key_exists($key, $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
393 $params[$paramName] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
394 } elseif ($constructorParameter->isDefaultValueAvailable()) {
395 $params[$paramName] = $constructorParameter->getDefaultValue();
396 } elseif (!($context[self::REQUIRE_ALL_PROPERTIES] ?? $this->defaultContext[self::REQUIRE_ALL_PROPERTIES] ?? false) && $constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
397 $params[$paramName] = null;
398 } else {
399 if (!isset($context['not_normalizable_value_exceptions'])) {
400 $missingConstructorArguments[] = $constructorParameter->name;
401 continue;
402 }
403
404 $constructorParameterTypes = [];
405 $reflectionType = $constructorParameter->getType();
406 if ($reflectionType instanceof \ReflectionUnionType) {
407 foreach ($reflectionType->getTypes() as $reflectionType) {
408 $constructorParameterTypes[] = (string) $reflectionType;
409 }
410 } elseif ($reflectionType instanceof \ReflectionType) {
411 $constructorParameterTypes[] = (string) $reflectionType;
412 } else {
413 $constructorParameterTypes[] = 'unknown';
414 }
415
416 $exception = NotNormalizableValueException::createForUnexpectedDataType(
417 \sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name),
418 null,
419 $constructorParameterTypes,
420 $attributeContext['deserialization_path'] ?? null,
421 true
422 );
423 $context['not_normalizable_value_exceptions'][] = $exception;
424 }
425 }
426
427 if ($missingConstructorArguments) {
428 throw new MissingConstructorArgumentsException(\sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s".', $class, implode('", "$', $missingConstructorArguments)), 0, null, $missingConstructorArguments, $class);
429 }
430
431 if (!$constructor->isConstructor()) {
432 $instance = $constructor->invokeArgs(null, $params);
433
434 // do not set a parameter that has been set in the constructor
435 foreach ($unsetKeys as $key) {
436 unset($data[$key]);
437 }
438
439 return $instance;
440 }
441
442 try {
443 $instance = $reflectionClass->newInstanceArgs($params);
444
445 // do not set a parameter that has been set in the constructor
446 foreach ($unsetKeys as $key) {
447 unset($data[$key]);
448 }
449
450 return $instance;
451 } catch (\TypeError $e) {
452 if (!isset($context['not_normalizable_value_exceptions'])) {
453 throw $e;
454 }
455
456 // Only report the TypeError when no constructor-argument error was collected for
457 // this instantiation. When the loop above already recorded missing or invalid
458 // arguments, the TypeError is a downstream consequence of those errors and would
459 // just duplicate the report.
460 if (\count($context['not_normalizable_value_exceptions']) === $collectedErrorCountBeforeConstructor) {
461 $context['not_normalizable_value_exceptions'][] = NotNormalizableValueException::createForUnexpectedDataType(
462 \sprintf('Failed to create an instance of "%s" from constructor: %s', $class, $e->getMessage()),
463 null,
464 ['unknown'],
465 $context['deserialization_path'] ?? null,
466 false,
467 0,
468 $e,
469 );
470 }
471
472 return $reflectionClass->newInstanceWithoutConstructor();
473 }
474 }
475
476 if (!$reflectionClass->isInstantiable()) {
477 throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to create object because the class "%s" is not instantiable.', $class), $data, ['unknown'], $context['deserialization_path'] ?? null);
478 }
479
480 return new $class();
481 }
482
483 /**
484 * @internal
485 */
486 protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, mixed $parameterData, array $context, ?string $format = null): mixed
487 {
488 try {
489 if (($parameterType = $parameter->getType()) instanceof \ReflectionNamedType && !$parameterType->isBuiltin()) {
490 $parameterClass = $parameterType->getName();
491 new \ReflectionClass($parameterClass); // throws a \ReflectionException if the class doesn't exist
492
493 if (!$this->serializer instanceof DenormalizerInterface) {
494 throw new LogicException(\sprintf('Cannot create an instance of "%s" from serialized data because the serializer inject in "%s" is not a denormalizer.', $parameterClass, static::class));
495 }
496
497 $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName, $format));
498 }
499 } catch (\ReflectionException $e) {
500 throw new RuntimeException(\sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
501 } catch (MissingConstructorArgumentsException $e) {
502 if (!$parameter->getType()->allowsNull()) {
503 throw $e;
504 }
505
506 return null;
507 }
508
509 $parameterData = $this->applyCallbacks($parameterData, $class->getName(), $parameterName, $format, $context);
510
511 return $this->applyFilterBool($parameter, $parameterData, $context);
512 }
513
514 /**
515 * @internal
516 */
517 protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
518 {
519 if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
520 $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
521 } else {
522 unset($parentContext[self::ATTRIBUTES]);
523 }
524
525 return $parentContext;
526 }
527
528 /**
529 * Validate callbacks set in context.
530 *
531 * @param string $contextType Used to specify which context is invalid in exceptions
532 *
533 * @throws InvalidArgumentException
534 */
535 final protected function validateCallbackContext(array $context, string $contextType = ''): void
536 {
537 if (!isset($context[self::CALLBACKS])) {
538 return;
539 }
540
541 if (!\is_array($context[self::CALLBACKS])) {
542 throw new InvalidArgumentException(\sprintf('The "%s"%s context option must be an array of callables.', self::CALLBACKS, '' !== $contextType ? " $contextType" : ''));
543 }
544
545 foreach ($context[self::CALLBACKS] as $attribute => $callback) {
546 if (!\is_callable($callback)) {
547 throw new InvalidArgumentException(\sprintf('Invalid callback found for attribute "%s" in the "%s"%s context option.', $attribute, self::CALLBACKS, '' !== $contextType ? " $contextType" : ''));
548 }
549 }
550 }
551
552 final protected function applyCallbacks(mixed $value, object|string $object, string $attribute, ?string $format, array $context): mixed
553 {
554 /**
555 * @var callable|null
556 */
557 $callback = $context[self::CALLBACKS][$attribute] ?? $this->defaultContext[self::CALLBACKS][$attribute] ?? null;
558
559 return $callback ? $callback($value, $object, $attribute, $format, $context) : $value;
560 }
561
562 final protected function applyFilterBool(\ReflectionParameter $parameter, mixed $value, array $context): mixed
563 {
564 if (!($context[self::FILTER_BOOL] ?? false)) {
565 return $value;
566 }
567
568 if (!($parameterType = $parameter->getType()) instanceof \ReflectionNamedType || 'bool' !== $parameterType->getName()) {
569 return $value;
570 }
571
572 return filter_var($value, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? $value;
573 }
574
575 /**
576 * Computes the normalization context merged with current one. Metadata always wins over global context, as more specific.
577 *
578 * @internal
579 */
580 protected function getAttributeNormalizationContext(object $object, string $attribute, array $context): array
581 {
582 if (null === $metadata = $this->getAttributeMetadata($object, $attribute)) {
583 return $context;
584 }
585
586 return array_merge($context, $metadata->getNormalizationContextForGroups($this->getGroups($context)));
587 }
588
589 /**
590 * Computes the denormalization context merged with current one. Metadata always wins over global context, as more specific.
591 *
592 * @internal
593 */
594 protected function getAttributeDenormalizationContext(string $class, string $attribute, array $context): array
595 {
596 $context['deserialization_path'] = ($context['deserialization_path'] ?? false) ? $context['deserialization_path'].'.'.$attribute : $attribute;
597
598 if (null === $metadata = $this->getAttributeMetadata($class, $attribute)) {
599 return $context;
600 }
601
602 return array_merge($context, $metadata->getDenormalizationContextForGroups($this->getGroups($context)));
603 }
604
605 /**
606 * @internal
607 */
608 protected function getAttributeMetadata(object|string $objectOrClass, string $attribute): ?AttributeMetadataInterface
609 {
610 if (!$this->classMetadataFactory) {
611 return null;
612 }
613
614 return $this->classMetadataFactory->getMetadataFor($objectOrClass)->getAttributesMetadata()[$attribute] ?? null;
615 }
616 }