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

Breadcrumb

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

class MimeMessageNormalizer

Normalize Mime message classes.

It forces the use of a PropertyNormalizer instance for normalization of all data objects composing a Message.

Emails using resources for any parts are not serializable.

Hierarchy

  • class \Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\SerializerAwareInterface

Expanded class hierarchy of MimeMessageNormalizer

File

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

Namespace

Symfony\Component\Serializer\Normalizer
View source
final class MimeMessageNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface {
    private NormalizerInterface&DenormalizerInterface $serializer;
    private array $headerClassMap;
    private \ReflectionProperty $headersProperty;
    public function __construct(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 {
        return [
            Message::class => true,
            Headers::class => true,
            HeaderInterface::class => true,
            Address::class => true,
            AbstractPart::class => true,
        ];
    }
    public function setSerializer(SerializerInterface $serializer) : void {
        if (!$serializer instanceof NormalizerInterface || !$serializer instanceof DenormalizerInterface) {
            throw new LogicException(\sprintf('The passed serializer should implement both NormalizerInterface and DenormalizerInterface, "%s" given.', get_debug_type($serializer)));
        }
        $this->serializer = $serializer;
        $this->normalizer
            ->setSerializer($serializer);
    }
    public function normalize(mixed $object, ?string $format = null, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
        if ($object instanceof Headers) {
            $ret = [];
            foreach ($this->headersProperty
                ->getValue($object) as $name => $header) {
                $ret[$name] = $this->serializer
                    ->normalize($header, $format, $context);
            }
            return $ret;
        }
        $ret = $this->normalizer
            ->normalize($object, $format, $context);
        if ($object instanceof AbstractPart) {
            $ret['class'] = $object::class;
            unset($ret['seekable'], $ret['cid'], $ret['handle']);
        }
        if ($object instanceof RawMessage && \array_key_exists('message', $ret) && null === $ret['message']) {
            unset($ret['message']);
        }
        return $ret;
    }
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []) : mixed {
        if (Headers::class === $type) {
            $ret = [];
            foreach ($data as $headers) {
                foreach ($headers as $header) {
                    $ret[] = $this->serializer
                        ->denormalize($header, $this->headerClassMap[strtolower($header['name'])] ?? UnstructuredHeader::class, $format, $context);
                }
            }
            return new Headers(...$ret);
        }
        if (AbstractPart::class === $type) {
            $type = $data['class'];
            unset($data['class']);
            $data['headers'] = $this->serializer
                ->denormalize($data['headers'], Headers::class, $format, $context);
        }
        return $this->normalizer
            ->denormalize($data, $type, $format, $context);
    }
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []) : bool {
        return $data instanceof Message || $data instanceof Headers || $data instanceof HeaderInterface || $data instanceof Address || $data instanceof AbstractPart;
    }
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []) : bool {
        return is_a($type, Message::class, true) || Headers::class === $type || AbstractPart::class === $type;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS public constant
MimeMessageNormalizer::$headerClassMap private property
MimeMessageNormalizer::$headersProperty private property
MimeMessageNormalizer::$serializer private property
MimeMessageNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides DenormalizerInterface::denormalize
MimeMessageNormalizer::getSupportedTypes public function Returns the types potentially supported by this normalizer. Overrides NormalizerInterface::getSupportedTypes
MimeMessageNormalizer::normalize public function Normalizes data into a set of arrays/scalars. Overrides NormalizerInterface::normalize
MimeMessageNormalizer::setSerializer public function Sets the owning Serializer object. Overrides SerializerAwareInterface::setSerializer
MimeMessageNormalizer::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer. Overrides DenormalizerInterface::supportsDenormalization
MimeMessageNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerInterface::supportsNormalization
MimeMessageNormalizer::__construct public function
RSS feed
Powered by Drupal