hasCacheableSupportsMethod example

if (!$this->serializer instanceof NormalizerInterface) {
            throw new LogicException('Cannot normalize object because injected serializer is not a normalizer.');
        }

        return $this->serializer->normalize($object->jsonSerialize()$format$context);
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            \JsonSerializable::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    /** * @param array $context */
    public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
    {
        return $data instanceof \JsonSerializable;
    }

    

class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
    public function getSupportedTypes(?string $format): array
    {
        return [
            \DateTimeZone::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    /** * @throws InvalidArgumentException */
    public function normalize(mixed $object, string $format = null, array $context = []): string
    {
        if (!$object instanceof \DateTimeZone) {
            throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
        }

        
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
    {
        parent::__construct($classMetadataFactory$nameConverter$propertyTypeExtractor$classDiscriminatorResolver$objectClassResolver$defaultContext);

        if (!isset($this->defaultContext[self::NORMALIZE_VISIBILITY])) {
            $this->defaultContext[self::NORMALIZE_VISIBILITY] = self::NORMALIZE_PUBLIC | self::NORMALIZE_PROTECTED | self::NORMALIZE_PRIVATE;
        }
    }

    public function getSupportedTypes(?string $format): array
    {
        return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
    }

    /** * @param array $context */
    public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
    {
        return parent::supportsNormalization($data$format) && $this->supports($data::class);
    }

    /** * @param array $context */
if (!$this->serializer instanceof NormalizerInterface) {
            throw new LogicException('Cannot normalize object because injected serializer is not a normalizer.');
        }

        return $this->serializer->normalize($object->jsonSerialize()$format$context);
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            \JsonSerializable::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    /** * @param array $context */
    public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
    {
        return $data instanceof \JsonSerializable;
    }

    
self::FORMAT_KEY => '%rP%yY%mM%dDT%hH%iM%sS',
    ];

    public function __construct(array $defaultContext = [])
    {
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            \DateInterval::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    /** * @throws InvalidArgumentException */
    public function normalize(mixed $object, string $format = null, array $context = []): string
    {
        if (!$object instanceof \DateInterval) {
            throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
        }

        

class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
    public function getSupportedTypes(?string $format): array
    {
        return [
            \DateTimeZone::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    /** * @throws InvalidArgumentException */
    public function normalize(mixed $object, string $format = null, array $context = []): string
    {
        if (!$object instanceof \DateTimeZone) {
            throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
        }

        

class GetSetMethodNormalizer extends AbstractObjectNormalizer
{
    private static $setterAccessibleCache = [];

    public function getSupportedTypes(?string $format): array
    {
        return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
    }

    /** * @param array $context */
    public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
    {
        return parent::supportsNormalization($data$format) && $this->supports($data::class);
    }

    /** * @param array $context */
if (!isset($this->normalizerCache[$format][$type])) {
            $this->normalizerCache[$format][$type] = [];

            foreach ($this->normalizers as $k => $normalizer) {
                if (!$normalizer instanceof NormalizerInterface) {
                    continue;
                }

                if (!method_exists($normalizer, 'getSupportedTypes')) {
                    trigger_deprecation('symfony/serializer', '6.3', '"%s" should implement "NormalizerInterface::getSupportedTypes(?string $format): array".', $normalizer::class);

                    if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
                        $this->normalizerCache[$format][$type][$k] = false;
                    } elseif ($normalizer->supportsNormalization($data$format$context)) {
                        $this->normalizerCache[$format][$type][$k] = true;
                        break;
                    }

                    continue;
                }

                $supportedTypes = $normalizer->getSupportedTypes($format);

                
public const PAYLOAD_FIELDS = 'payload_fields';

    public function __construct(
        private readonly array $defaultContext = [],
        private readonly ?NameConverterInterface $nameConverter = null,
    ) {
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            ConstraintViolationListInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    public function normalize(mixed $object, string $format = null, array $context = []): array
    {
        if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
            $payloadFieldsToSerialize = $context[self::PAYLOAD_FIELDS];
        } elseif (\array_key_exists(self::PAYLOAD_FIELDS, $this->defaultContext)) {
            $payloadFieldsToSerialize = $this->defaultContext[self::PAYLOAD_FIELDS];
        } else {
            $payloadFieldsToSerialize = [];
        }
private array $headerClassMap;
    private \ReflectionProperty $headersProperty;

    public function __construct(private readonly PropertyNormalizer $normalizer)
    {
        $this->headerClassMap = (new \ReflectionClassConstant(Headers::class, 'HEADER_CLASS_MAP'))->getValue();
        $this->headersProperty = new \ReflectionProperty(Headers::class, 'headers');
    }

    public function getSupportedTypes(?string $format): array
    {
        $isCacheable = __CLASS__ === static::class || $this->hasCacheableSupportsMethod();

        return [
            Message::class => $isCacheable,
            Headers::class => $isCacheable,
            HeaderInterface::class => $isCacheable,
            Address::class => $isCacheable,
            AbstractPart::class => $isCacheable,
        ];
    }

    public function setSerializer(SerializerInterface $serializer): void
    {


        parent::__construct($classMetadataFactory$nameConverter$propertyTypeExtractor$classDiscriminatorResolver$objectClassResolver$defaultContext);

        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();

        $this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class D $class)(...);
    }

    public function getSupportedTypes(?string $format): array
    {
        return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
    }

    /** * @deprecated since Symfony 6.3, use "getSupportedTypes()" instead */
    public function hasCacheableSupportsMethod(): bool
    {
        trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, implement "%s::getSupportedTypes()" instead.', __METHOD__, get_debug_type($this));

        return __CLASS__ === static::class;
    }

    

class GetSetMethodNormalizer extends AbstractObjectNormalizer
{
    private static array $setterAccessibleCache = [];

    public function getSupportedTypes(?string $format): array
    {
        return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
    }

    /** * @param array $context */
    public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
    {
        return parent::supportsNormalization($data$format) && $this->supports($data::class);
    }

    /** * @param array $context */
public const PAYLOAD_FIELDS = 'payload_fields';

    public function __construct(
        private readonly array $defaultContext = [],
        private readonly ?NameConverterInterface $nameConverter = null,
    ) {
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            ConstraintViolationListInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
        ];
    }

    public function normalize(mixed $object, string $format = null, array $context = []): array
    {
        if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
            $payloadFieldsToSerialize = $context[self::PAYLOAD_FIELDS];
        } elseif (\array_key_exists(self::PAYLOAD_FIELDS, $this->defaultContext)) {
            $payloadFieldsToSerialize = $this->defaultContext[self::PAYLOAD_FIELDS];
        } else {
            $payloadFieldsToSerialize = [];
        }

        return \array_key_exists(self::UNWRAP_PATH, $context) && !isset($context['unwrapped']);
    }

    /** * @deprecated since Symfony 6.3, use "getSupportedTypes()" instead */
    public function hasCacheableSupportsMethod(): bool
    {
        trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

        return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
    }
}

class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
{
    use ObjectToPopulateTrait;
    use SerializerAwareTrait;

    public function getSupportedTypes(?string $format): array
    {
        return [
            NormalizableInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
            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
    {
        
Home | Imprint | This part of the site doesn't use cookies.