Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Normalizer/DateTimeZoneNormalizer.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\InvalidArgumentException;
15 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
16
17 /**
18 * Normalizes a {@see \DateTimeZone} object to a timezone string.
19 *
20 * @author Jérôme Desjardins <[email protected]m>
21 */
22 final class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface
23 {
24 public function getSupportedTypes(?string $format): array
25 {
26 return [
27 \DateTimeZone::class => true,
28 ];
29 }
30
31 /**
32 * @throws InvalidArgumentException
33 */
34 public function normalize(mixed $data, ?string $format = null, array $context = []): string
35 {
36 if (!$data instanceof \DateTimeZone) {
37 throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
38 }
39
40 return $data->getName();
41 }
42
43 public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
44 {
45 return $data instanceof \DateTimeZone;
46 }
47
48 /**
49 * @throws NotNormalizableValueException
50 */
51 public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): \DateTimeZone
52 {
53 if ('' === $data || null === $data) {
54 throw NotNormalizableValueException::createForUnexpectedDataType('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.', $data, ['string'], $context['deserialization_path'] ?? null, true);
55 }
56
57 try {
58 return new \DateTimeZone($data);
59 } catch (\Exception $e) {
60 throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, ['string'], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
61 }
62 }
63
64 public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
65 {
66 return \DateTimeZone::class === $type;
67 }
68 }