Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Normalizer/UidNormalizer.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Normalizer;
13
14 use Symfony\Component\Serializer\Exception\LogicException;
15 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
16 use Symfony\Component\Uid\AbstractUid;
17
18 final class UidNormalizer implements NormalizerInterface, DenormalizerInterface
19 {
20 public const NORMALIZATION_FORMAT_KEY = 'uid_normalization_format';
21
22 public const NORMALIZATION_FORMAT_CANONICAL = 'canonical';
23 public const NORMALIZATION_FORMAT_BASE58 = 'base58';
24 public const NORMALIZATION_FORMAT_BASE32 = 'base32';
25 public const NORMALIZATION_FORMAT_RFC4122 = 'rfc4122';
26 public const NORMALIZATION_FORMAT_RFC9562 = self::NORMALIZATION_FORMAT_RFC4122; // RFC 9562 obsoleted RFC 4122 but the format is the same
27
28 public const NORMALIZATION_FORMATS = [
29 self::NORMALIZATION_FORMAT_CANONICAL,
30 self::NORMALIZATION_FORMAT_BASE58,
31 self::NORMALIZATION_FORMAT_BASE32,
32 self::NORMALIZATION_FORMAT_RFC4122,
33 ];
34
35 private array $defaultContext = [
36 self::NORMALIZATION_FORMAT_KEY => self::NORMALIZATION_FORMAT_CANONICAL,
37 ];
38
39 public function __construct(array $defaultContext = [])
40 {
41 $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
42 }
43
44 public function getSupportedTypes(?string $format): array
45 {
46 return [
47 AbstractUid::class => true,
48 ];
49 }
50
51 public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
52 {
53 return match ($context[self::NORMALIZATION_FORMAT_KEY] ?? $this->defaultContext[self::NORMALIZATION_FORMAT_KEY]) {
54 self::NORMALIZATION_FORMAT_CANONICAL => (string) $data,
55 self::NORMALIZATION_FORMAT_BASE58 => $data->toBase58(),
56 self::NORMALIZATION_FORMAT_BASE32 => $data->toBase32(),
57 self::NORMALIZATION_FORMAT_RFC4122 => $data->toRfc4122(),
58 default => throw new LogicException(\sprintf('The "%s" format is not valid.', $context[self::NORMALIZATION_FORMAT_KEY] ?? $this->defaultContext[self::NORMALIZATION_FORMAT_KEY])),
59 };
60 }
61
62 public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
63 {
64 return $data instanceof AbstractUid;
65 }
66
67 public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
68 {
69 try {
70 return $type::fromString($data);
71 } catch (\InvalidArgumentException|\TypeError) {
72 throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The data is not a valid "%s" string representation.', $type), $data, ['string'], $context['deserialization_path'] ?? null, true);
73 }
74 }
75
76 public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
77 {
78 return is_subclass_of($type, AbstractUid::class, true);
79 }
80 }