Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ObjectNormalizer.php

class ObjectNormalizer

Converts between objects and arrays using the PropertyAccess component.

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

  • class \Symfony\Component\Serializer\Normalizer\AbstractNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\SerializerAwareInterface uses \Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait, \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer extends \Symfony\Component\Serializer\Normalizer\AbstractNormalizer
      • class \Symfony\Component\Serializer\Normalizer\ObjectNormalizer extends \Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer

Expanded class hierarchy of ObjectNormalizer

File

vendor/symfony/serializer/Normalizer/ObjectNormalizer.php, line 33

Namespace

Symfony\Component\Serializer\Normalizer
View source
final class ObjectNormalizer extends AbstractObjectNormalizer {
    private static $reflectionCache = [];
    private static $isReadableCache = [];
    private static $isWritableCache = [];
    protected PropertyAccessorInterface $propertyAccessor;
    protected $propertyInfoExtractor;
    private $writeInfoExtractor;
    private readonly \Closure $objectClassResolver;
    public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory = null, ?NameConverterInterface $nameConverter = null, ?PropertyAccessorInterface $propertyAccessor = null, ?PropertyTypeExtractorInterface $propertyTypeExtractor = null, ?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, ?callable $objectClassResolver = null, array $defaultContext = [], ?PropertyInfoExtractorInterface $propertyInfoExtractor = null) {
        if (!class_exists(PropertyAccess::class)) {
            throw new LogicException('The ObjectNormalizer class requires the "PropertyAccess" component. Try running "composer require symfony/property-access".');
        }
        parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
        $this->objectClassResolver = ($objectClassResolver ?? static fn($class) => \is_object($class) ? $class::class : $class)(...);
        $this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
        $this->writeInfoExtractor = new ReflectionExtractor();
    }
    public function getSupportedTypes(?string $format) : array {
        return [
            'object' => true,
        ];
    }
    protected function extractAttributes(object $object, ?string $format = null, array $context = []) : array {
        if (\stdClass::class === $object::class) {
            return array_keys((array) $object);
        }
        // If not using groups, detect manually
        $attributes = [];
        // methods
        $class = ($this->objectClassResolver)($object);
        $reflClass = new \ReflectionClass($class);
        foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
            if (0 !== $reflMethod->getNumberOfRequiredParameters() || $reflMethod->isStatic() || $reflMethod->isConstructor() || $reflMethod->isDestructor()) {
                continue;
            }
            $name = $reflMethod->name;
            $attributeName = null;
            // ctype_lower check to find out if method looks like accessor but actually is not, e.g. hash, cancel
            if (3 < \strlen($name) && !ctype_lower($name[3]) && match ($name[0]) {    'g' => str_starts_with($name, 'get'),
                'h' => str_starts_with($name, 'has'),
                'c' => str_starts_with($name, 'can'),
                default => false,
            
            }) {
                // getters, hassers and canners
                $attributeName = substr($name, 3);
                if (!$reflClass->hasProperty($attributeName)) {
                    $attributeName = lcfirst($attributeName);
                }
            }
            elseif ('is' !== $name && str_starts_with($name, 'is') && !ctype_lower($name[2])) {
                // issers
                $attributeName = substr($name, 2);
                if (!$reflClass->hasProperty($attributeName)) {
                    $attributeName = lcfirst($attributeName);
                }
            }
            if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) {
                $attributes[$attributeName] = true;
            }
        }
        // properties
        foreach ($reflClass->getProperties() as $reflProperty) {
            if (!$reflProperty->isPublic()) {
                continue;
            }
            if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
                continue;
            }
            $attributes[$reflProperty->name] = true;
        }
        return array_keys($attributes);
    }
    protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []) : mixed {
        $mapping = $this->classDiscriminatorResolver?->getMappingForMappedObject($object);
        return $attribute === $mapping?->getTypeProperty() ? $mapping : $this->propertyAccessor
            ->getValue($object, $attribute);
    }
    protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []) : void {
        try {
            $this->propertyAccessor
                ->setValue($object, $attribute, $value);
        } catch (NoSuchPropertyException) {
            // Properties not found are ignored
        }
    }
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false) : array|bool {
        if (false === ($allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString))) {
            return false;
        }
        if (null !== $this->classDiscriminatorResolver) {
            $class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
            if (null !== ($discriminatorMapping = $this->classDiscriminatorResolver
                ->getMappingForMappedObject($classOrObject))) {
                $allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
            }
            if (null !== ($discriminatorMapping = $this->classDiscriminatorResolver
                ->getMappingForClass($class))) {
                $attributes = [];
                foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
                    $attributes[] = parent::getAllowedAttributes($mappedClass, $context, $attributesAsString);
                }
                $allowedAttributes = array_merge($allowedAttributes, ...$attributes);
            }
        }
        return $allowedAttributes;
    }
    protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []) : bool {
        if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
            return false;
        }
        $class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
        if ($context['_read_attributes'] ?? true) {
            if (!isset(self::$isReadableCache[$class . $attribute])) {
                self::$isReadableCache[$class . $attribute] = $this->propertyInfoExtractor
                    ->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute) || \is_object($classOrObject) && $this->propertyAccessor
                    ->isReadable($classOrObject, $attribute);
            }
            return self::$isReadableCache[$class . $attribute];
        }
        if (!isset(self::$isWritableCache[$class . $attribute])) {
            if (str_contains($attribute, '.')) {
                self::$isWritableCache[$class . $attribute] = true;
            }
            else {
                self::$isWritableCache[$class . $attribute] = $this->propertyInfoExtractor
                    ->isWritable($class, $attribute) || ($writeInfo = $this->writeInfoExtractor
                    ->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType();
            }
        }
        return self::$isWritableCache[$class . $attribute];
    }
    private function hasAttributeAccessorMethod(string $class, string $attribute) : bool {
        if (!isset(self::$reflectionCache[$class])) {
            self::$reflectionCache[$class] = new \ReflectionClass($class);
        }
        $reflection = self::$reflectionCache[$class];
        if (!$reflection->hasMethod($attribute)) {
            return false;
        }
        $method = $reflection->getMethod($attribute);
        return !$method->isStatic() && !$method->getAttributes(Ignore::class) && !$method->getNumberOfRequiredParameters();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractNormalizer::$defaultContext protected property
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES public constant If ATTRIBUTES are specified, and the source has fields that are not part of that list,
either ignore those attributes (true) or throw an ExtraAttributesException (false).
AbstractNormalizer::applyCallbacks final protected function
AbstractNormalizer::applyFilterBool final protected function
AbstractNormalizer::ATTRIBUTES public constant Limit (de)normalize to the specified names.
AbstractNormalizer::CALLBACKS public constant Hashmap of field name =&gt; callable to (de)normalize this field.
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER public constant Handler to call when a circular reference has been detected.
AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT public constant How many loops of circular reference to allow while normalizing.
AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT_COUNTERS protected constant @internal
AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS public constant Hashmap of default values for constructor arguments.
AbstractNormalizer::FILTER_BOOL public constant Flag to control whether a non-boolean value should be filtered using the
filter_var function with the {\FILTER_VALIDATE_BOOL filter before casting it to a boolean.
AbstractNormalizer::getAttributeDenormalizationContext protected function Computes the denormalization context merged with current one. Metadata always wins over global context, as more specific.
AbstractNormalizer::getAttributeMetadata protected function @internal
AbstractNormalizer::getAttributeNormalizationContext protected function Computes the normalization context merged with current one. Metadata always wins over global context, as more specific.
AbstractNormalizer::getConstructor protected function Returns the method to use to construct an object. This method must be either
the object constructor or static.
AbstractNormalizer::getGroups protected function
AbstractNormalizer::GROUPS public constant Only (de)normalize attributes that are in the specified groups.
AbstractNormalizer::handleCircularReference protected function Handles a circular reference.
AbstractNormalizer::IGNORED_ATTRIBUTES public constant Skip the specified attributes when normalizing an object tree.
AbstractNormalizer::isCircularReference protected function Detects if the configured circular reference limit is reached.
AbstractNormalizer::OBJECT_TO_POPULATE public constant Instead of creating a new instance of an object, update the specified object.
AbstractNormalizer::prepareForDenormalization protected function Normalizes the given data to an array. It&#039;s particularly useful during
the denormalization process.
AbstractNormalizer::REQUIRE_ALL_PROPERTIES public constant Require all properties to be listed in the input instead of falling
back to null for nullable ones.
AbstractNormalizer::validateCallbackContext final protected function Validate callbacks set in context.
AbstractObjectNormalizer::$attributesCache private property
AbstractObjectNormalizer::$classDiscriminatorResolver protected property
AbstractObjectNormalizer::$typeCache private property
AbstractObjectNormalizer::createChildContext protected function Overwritten to update the cache key for the child. Overrides AbstractNormalizer::createChildContext
AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE public constant Flag to tell the denormalizer to also populate existing objects on
attributes of the main object.
AbstractObjectNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides DenormalizerInterface::denormalize
AbstractObjectNormalizer::denormalizeParameter protected function @internal Overrides AbstractNormalizer::denormalizeParameter
AbstractObjectNormalizer::DEPTH_KEY_PATTERN public constant How to track the current depth in the context.
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT public constant While denormalizing, we can verify that type matches.
AbstractObjectNormalizer::ENABLE_MAX_DEPTH public constant Set to true to respect the max depth metadata on fields.
AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY public constant Specify which context key are not relevant to determine which attributes
of an object to (de)normalize.
AbstractObjectNormalizer::getAttributes protected function Gets and caches attributes for the given object, format and context.
AbstractObjectNormalizer::getCacheKey private function Builds the cache key for the attributes cache.
AbstractObjectNormalizer::getMappedClass private function
AbstractObjectNormalizer::getNestedAttributes private function Returns all attributes with a SerializedPath attribute and the respective path.
AbstractObjectNormalizer::getPropertyType private function BC layer for PropertyTypeExtractorInterface::getTypes().
Can be removed as soon as PropertyTypeExtractorInterface::getTypes() is removed (8.0).
AbstractObjectNormalizer::getType private function
AbstractObjectNormalizer::instantiateObject protected function Instantiates an object using constructor parameters when needed. Overrides AbstractNormalizer::instantiateObject
AbstractObjectNormalizer::isMaxDepthReached private function Is the max depth reached for the given attribute?
AbstractObjectNormalizer::isUninitializedValueError private function This error may occur when specific object normalizer implementation gets attribute value
by accessing a public uninitialized property or by calling a method accessing such property.
AbstractObjectNormalizer::MAX_DEPTH_HANDLER public constant Callback to allow to set a value for an attribute when the max depth has
been reached.
AbstractObjectNormalizer::normalize public function Normalizes data into a set of arrays/scalars. Overrides NormalizerInterface::normalize
AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS public constant Flag to control whether an empty object should be kept as an object (in
JSON: {}) or converted to a list (in JSON: []).
AbstractObjectNormalizer::removeNestedValue private function
AbstractObjectNormalizer::SKIP_NULL_VALUES public constant Flag to control whether fields with the value `null` should be output
when normalizing or omitted.
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES public constant Flag to control whether uninitialized PHP&gt;=7.4 typed class properties
should be excluded when normalizing.
AbstractObjectNormalizer::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer. Overrides DenormalizerInterface::supportsDenormalization 2
AbstractObjectNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerInterface::supportsNormalization 2
AbstractObjectNormalizer::updateData private function Sets an attribute and apply the name converter if necessary.
AbstractObjectNormalizer::validateAndDenormalize private function Validates the submitted data and denormalizes it.
AbstractObjectNormalizer::validateAndDenormalizeLegacy private function Validates the submitted data and denormalizes it.
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS public constant
ObjectNormalizer::$isReadableCache private static property
ObjectNormalizer::$isWritableCache private static property
ObjectNormalizer::$objectClassResolver private property Overrides AbstractObjectNormalizer::$objectClassResolver
ObjectNormalizer::$propertyAccessor protected property
ObjectNormalizer::$propertyInfoExtractor protected property
ObjectNormalizer::$reflectionCache private static property
ObjectNormalizer::$writeInfoExtractor private property
ObjectNormalizer::extractAttributes protected function Extracts attributes to normalize from the class of the given object, format and context. Overrides AbstractObjectNormalizer::extractAttributes
ObjectNormalizer::getAllowedAttributes protected function Gets attributes to normalize using groups. Overrides AbstractNormalizer::getAllowedAttributes
ObjectNormalizer::getAttributeValue protected function Gets the attribute value. Overrides AbstractObjectNormalizer::getAttributeValue
ObjectNormalizer::getSupportedTypes public function Returns the types potentially supported by this normalizer. Overrides NormalizerInterface::getSupportedTypes
ObjectNormalizer::hasAttributeAccessorMethod private function
ObjectNormalizer::isAllowedAttribute protected function Is this attribute allowed? Overrides AbstractNormalizer::isAllowedAttribute
ObjectNormalizer::setAttributeValue protected function Overrides AbstractObjectNormalizer::setAttributeValue
ObjectNormalizer::__construct public function Sets the {@link ClassMetadataFactoryInterface} to use. Overrides AbstractObjectNormalizer::__construct
ObjectToPopulateTrait::extractObjectToPopulate protected function Extract the `object_to_populate` field from the context if it exists
and is an instance of the provided $class.
SerializerAwareTrait::$serializer protected property
SerializerAwareTrait::setSerializer public function
RSS feed
Powered by Drupal