extractObjectToPopulate example

DenormalizableInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
    {
        return $object->normalize($this->serializer, $format$context);
    }

    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
    {
        $object = $this->extractObjectToPopulate($type$context) ?? new $type();
        $object->denormalize($this->serializer, $data$format$context);

        return $object;
    }

    /** * Checks if the given class implements the NormalizableInterface. * * @param mixed $data Data to normalize * @param string|null $format The format being (de-)serialized from or into * @param array $context */
unset($data[$element]);
        }

        return $data;
    }

    /** * @return class-string */
    private function getMappedClass(array $data, string $class, array $context): string
    {
        if (null !== $object = $this->extractObjectToPopulate($class$context, self::OBJECT_TO_POPULATE)) {
            return $object::class;
        }

        if (!$mapping = $this->classDiscriminatorResolver?->getMappingForClass($class)) {
            return $class;
        }

        if (null === $type = $data[$mapping->getTypeProperty()] ?? null) {
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty()$class), null, ['string']isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false);
        }

        
DenormalizableInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
    {
        return $object->normalize($this->serializer, $format$context);
    }

    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
    {
        $object = $this->extractObjectToPopulate($type$context) ?? new $type();
        $object->denormalize($this->serializer, $data$format$context);

        return $object;
    }

    /** * Checks if the given class implements the NormalizableInterface. * * @param mixed $data Data to normalize * @param string|null $format The format being (de-)serialized from or into * @param array $context */

    protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)
    {
        if (null !== $object = $this->extractObjectToPopulate($class$context, self::OBJECT_TO_POPULATE)) {
            unset($context[self::OBJECT_TO_POPULATE]);

            return $object;
        }
        // clean up even if no match         unset($context[static::OBJECT_TO_POPULATE]);

        $constructor = $this->getConstructor($data$class$context$reflectionClass$allowedAttributes);
        if ($constructor) {
            $context['has_constructor'] = true;
            if (true !== $constructor->isPublic()) {
                
unset($data[$element]);
        }

        return $data;
    }

    /** * @return class-string */
    private function getMappedClass(array $data, string $class, array $context): string
    {
        if (null !== $object = $this->extractObjectToPopulate($class$context, self::OBJECT_TO_POPULATE)) {
            return $object::class;
        }

        if (!$mapping = $this->classDiscriminatorResolver?->getMappingForClass($class)) {
            return $class;
        }

        if (null === $type = $data[$mapping->getTypeProperty()] ?? null) {
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty()$class), null, ['string']isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false);
        }

        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;

class ObjectToPopulateTraitTest extends TestCase
{
    use ObjectToPopulateTrait;

    public function testExtractObjectToPopulateReturnsNullWhenKeyIsMissing()
    {
        $object = $this->extractObjectToPopulate(ProxyDummy::class[]);

        $this->assertNull($object);
    }

    public function testExtractObjectToPopulateReturnsNullWhenNonObjectIsProvided()
    {
        $object = $this->extractObjectToPopulate(ProxyDummy::class[
            'object_to_populate' => 'not an object',
        ]);

        $this->assertNull($object);
    }

    protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)
    {
        if (null !== $object = $this->extractObjectToPopulate($class$context, self::OBJECT_TO_POPULATE)) {
            unset($context[self::OBJECT_TO_POPULATE]);

            return $object;
        }
        // clean up even if no match         unset($context[static::OBJECT_TO_POPULATE]);

        $constructor = $this->getConstructor($data$class$context$reflectionClass$allowedAttributes);
        if ($constructor) {
            $context['has_constructor'] = true;
            if (true !== $constructor->isPublic()) {
                
Home | Imprint | This part of the site doesn't use cookies.