createChildContext example

protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, mixed $parameterData, array $context, string $format = null): mixed
    {
        try {
            if (($parameterType = $parameter->getType()) instanceof \ReflectionNamedType && !$parameterType->isBuiltin()) {
                $parameterClass = $parameterType->getName();
                new \ReflectionClass($parameterClass); // throws a \ReflectionException if the class doesn't exist
                if (!$this->serializer instanceof DenormalizerInterface) {
                    throw new LogicException(sprintf('Cannot create an instance of "%s" from serialized data because the serializer inject in "%s" is not a denormalizer.', $parameterClassstatic::class));
                }

                $parameterData = $this->serializer->denormalize($parameterData$parameterClass$format$this->createChildContext($context$parameterName$format));
            }
        } catch (\ReflectionException $e) {
            throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
        } catch (MissingConstructorArgumentsException $e) {
            if (!$parameter->getType()->allowsNull()) {
                throw $e;
            }

            return null;
        }

        
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, mixed $parameterData, array $context, string $format = null): mixed
    {
        try {
            if (($parameterType = $parameter->getType()) instanceof \ReflectionNamedType && !$parameterType->isBuiltin()) {
                $parameterClass = $parameterType->getName();
                new \ReflectionClass($parameterClass); // throws a \ReflectionException if the class doesn't exist
                if (!$this->serializer instanceof DenormalizerInterface) {
                    throw new LogicException(sprintf('Cannot create an instance of "%s" from serialized data because the serializer inject in "%s" is not a denormalizer.', $parameterClassstatic::class));
                }

                $parameterData = $this->serializer->denormalize($parameterData$parameterClass$format$this->createChildContext($context$parameterName$format));
            }
        } catch (\ReflectionException $e) {
            throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
        } catch (MissingConstructorArgumentsException $e) {
            if (!$parameter->getType()->allowsNull()) {
                throw $e;
            }

            return null;
        }

        
$attributeContext = $this->getAttributeNormalizationContext($object$attribute$context);

            if (null === $attributeValue || \is_scalar($attributeValue)) {
                $data = $this->updateData($data$attribute$attributeValue$class$format$attributeContext$attributesMetadata$classMetadata);
                continue;
            }

            if (!$this->serializer instanceof NormalizerInterface) {
                throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute));
            }

            $childContext = $this->createChildContext($attributeContext$attribute$format);

            $data = $this->updateData($data$attribute$this->serializer->normalize($attributeValue$format$childContext)$class$format$attributeContext$attributesMetadata$classMetadata);
        }

        $preserveEmptyObjects = $context[self::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[self::PRESERVE_EMPTY_OBJECTS] ?? false;
        if ($preserveEmptyObjects && !$data) {
            return new \ArrayObject();
        }

        return $data;
    }

    
$attributeContext = $this->getAttributeNormalizationContext($object$attribute$context);

            if (null === $attributeValue || \is_scalar($attributeValue)) {
                $data = $this->updateData($data$attribute$attributeValue$class$format$attributeContext$attributesMetadata$classMetadata);
                continue;
            }

            if (!$this->serializer instanceof NormalizerInterface) {
                throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute));
            }

            $childContext = $this->createChildContext($attributeContext$attribute$format);

            $data = $this->updateData($data$attribute$this->serializer->normalize($attributeValue$format$childContext)$class$format$attributeContext$attributesMetadata$classMetadata);
        }

        $preserveEmptyObjects = $context[self::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[self::PRESERVE_EMPTY_OBJECTS] ?? false;
        if ($preserveEmptyObjects && !$data) {
            return new \ArrayObject();
        }

        return $data;
    }

    
Home | Imprint | This part of the site doesn't use cookies.