| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\Mime\Address; |
| 15 |
use Symfony\Component\Mime\Header\HeaderInterface; |
| 16 |
use Symfony\Component\Mime\Header\Headers; |
| 17 |
use Symfony\Component\Mime\Header\UnstructuredHeader; |
| 18 |
use Symfony\Component\Mime\Message; |
| 19 |
use Symfony\Component\Mime\Part\AbstractPart; |
| 20 |
use Symfony\Component\Mime\RawMessage; |
| 21 |
use Symfony\Component\Serializer\Exception\LogicException; |
| 22 |
use Symfony\Component\Serializer\SerializerAwareInterface; |
| 23 |
use Symfony\Component\Serializer\SerializerInterface; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
final class MimeMessageNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
| 34 |
{ |
| 35 |
private NormalizerInterface&DenormalizerInterface $serializer; |
| 36 |
private array $headerClassMap; |
| 37 |
private \ReflectionProperty $headersProperty; |
| 38 |
|
| 39 |
public function __construct(private readonly PropertyNormalizer $normalizer) |
| 40 |
{ |
| 41 |
$this->headerClassMap = (new \ReflectionClassConstant(Headers::class, 'HEADER_CLASS_MAP'))->getValue(); |
| 42 |
$this->headersProperty = new \ReflectionProperty(Headers::class, 'headers'); |
| 43 |
} |
| 44 |
|
| 45 |
public function getSupportedTypes(?string $format): array |
| 46 |
{ |
| 47 |
return [ |
| 48 |
Message::class => true, |
| 49 |
Headers::class => true, |
| 50 |
HeaderInterface::class => true, |
| 51 |
Address::class => true, |
| 52 |
AbstractPart::class => true, |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
public function setSerializer(SerializerInterface $serializer): void |
| 57 |
{ |
| 58 |
if (!$serializer instanceof NormalizerInterface || !$serializer instanceof DenormalizerInterface) { |
| 59 |
throw new LogicException(\sprintf('The passed serializer should implement both NormalizerInterface and DenormalizerInterface, "%s" given.', get_debug_type($serializer))); |
| 60 |
} |
| 61 |
$this->serializer = $serializer; |
| 62 |
$this->normalizer->setSerializer($serializer); |
| 63 |
} |
| 64 |
|
| 65 |
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null |
| 66 |
{ |
| 67 |
if ($data instanceof Headers) { |
| 68 |
$ret = []; |
| 69 |
foreach ($this->headersProperty->getValue($data) as $name => $header) { |
| 70 |
$ret[$name] = $this->serializer->normalize($header, $format, $context); |
| 71 |
} |
| 72 |
|
| 73 |
return $ret; |
| 74 |
} |
| 75 |
|
| 76 |
$ret = $this->normalizer->normalize($data, $format, $context); |
| 77 |
|
| 78 |
if ($data instanceof AbstractPart) { |
| 79 |
$ret['class'] = $data::class; |
| 80 |
unset($ret['seekable'], $ret['cid'], $ret['handle']); |
| 81 |
} |
| 82 |
|
| 83 |
if ($data instanceof RawMessage && \array_key_exists('message', $ret) && null === $ret['message']) { |
| 84 |
unset($ret['message']); |
| 85 |
} |
| 86 |
|
| 87 |
return $ret; |
| 88 |
} |
| 89 |
|
| 90 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 91 |
{ |
| 92 |
if (Headers::class === $type) { |
| 93 |
$ret = []; |
| 94 |
foreach ($data as $headers) { |
| 95 |
foreach ($headers as $header) { |
| 96 |
$ret[] = $this->serializer->denormalize($header, $this->headerClassMap[strtolower($header['name'])] ?? UnstructuredHeader::class, $format, $context); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return new Headers(...$ret); |
| 101 |
} |
| 102 |
|
| 103 |
if (AbstractPart::class === $type) { |
| 104 |
$type = $data['class']; |
| 105 |
unset($data['class']); |
| 106 |
$data['headers'] = $this->serializer->denormalize($data['headers'], Headers::class, $format, $context); |
| 107 |
} |
| 108 |
|
| 109 |
return $this->normalizer->denormalize($data, $type, $format, $context); |
| 110 |
} |
| 111 |
|
| 112 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 113 |
{ |
| 114 |
return $data instanceof Message || $data instanceof Headers || $data instanceof HeaderInterface || $data instanceof Address || $data instanceof AbstractPart; |
| 115 |
} |
| 116 |
|
| 117 |
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 118 |
{ |
| 119 |
return is_a($type, Message::class, true) || Headers::class === $type || AbstractPart::class === $type; |
| 120 |
} |
| 121 |
} |