b2pweb /
bdf-serializer
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\Serializer\Normalizer; |
||
| 4 | |||
| 5 | use Bdf\Serializer\Context\DenormalizationContext; |
||
| 6 | use Bdf\Serializer\Context\NormalizationContext; |
||
| 7 | use Bdf\Serializer\Type\Type; |
||
| 8 | use DateTime; |
||
| 9 | use DateTimeImmutable; |
||
| 10 | use DateTimeInterface; |
||
| 11 | use DateTimeZone; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * DateTimeNormalizer |
||
| 15 | * |
||
| 16 | * @implements NormalizerInterface<\DateTime|\DateTimeImmutable> |
||
| 17 | */ |
||
| 18 | class DateTimeNormalizer implements NormalizerInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The default date format used for normalization |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $defaultFormat; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * DateTimeNormalizer constructor. |
||
| 29 | * |
||
| 30 | * @param string $defaultFormat The default date format used by normalization |
||
| 31 | */ |
||
| 32 | 194 | public function __construct(string $defaultFormat = DateTime::ATOM) |
|
| 33 | { |
||
| 34 | 194 | $this->defaultFormat = $defaultFormat; |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 16 | public function normalize($data, NormalizationContext $context) |
|
| 41 | { |
||
| 42 | 16 | $timezone = $context->option(NormalizationContext::TIMEZONE); |
|
| 43 | |||
| 44 | 16 | if ($timezone !== null) { |
|
| 45 | // Dont change date instance if it is not immutable |
||
| 46 | 6 | if (!$data instanceof DateTimeImmutable) { |
|
| 47 | 6 | $data = clone $data; |
|
| 48 | } |
||
| 49 | |||
| 50 | 6 | $data = $data->setTimezone(new DateTimeZone($timezone)); |
|
| 51 | } |
||
| 52 | |||
| 53 | 16 | return $data->format($context->option(NormalizationContext::DATETIME_FORMAT, $this->defaultFormat)); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | * |
||
| 59 | * @psalm-suppress InvalidNullableReturnType |
||
| 60 | */ |
||
| 61 | 26 | public function denormalize($data, Type $type, DenormalizationContext $context) |
|
| 62 | { |
||
| 63 | // @fixme does this case really occurs ? No other normalizer handle this case |
||
| 64 | 26 | if ($data === null) { |
|
| 65 | /** @psalm-suppress NullableReturnStatement */ |
||
| 66 | return null; |
||
| 67 | } |
||
| 68 | |||
| 69 | 26 | $className = $type->name(); |
|
| 70 | 26 | $format = $context->option(DenormalizationContext::DATETIME_FORMAT); |
|
| 71 | 26 | $timezoneHint = $context->option(DenormalizationContext::TIMEZONE_HINT); |
|
| 72 | 26 | $timezone = $context->option(DenormalizationContext::TIMEZONE); |
|
| 73 | |||
| 74 | 26 | if ($timezoneHint !== null) { |
|
| 75 | 4 | $timezoneHint = new DateTimeZone($timezoneHint); |
|
| 76 | } |
||
| 77 | |||
| 78 | 26 | if ($format !== null) { |
|
| 79 | /** @var \DateTime|\DateTimeImmutable $date */ |
||
| 80 | 8 | $date = $className::createFromFormat($format, $data, $timezoneHint); |
|
| 81 | } else { |
||
| 82 | /** @psalm-suppress UnsafeInstantiation */ |
||
| 83 | 20 | $date = new $className($data, $timezoneHint); |
|
| 84 | } |
||
| 85 | |||
| 86 | 26 | if ($timezone !== null) { |
|
| 87 | /** @psalm-suppress PossiblyFalseReference */ |
||
| 88 | 10 | $date = $date->setTimezone(new DateTimeZone($timezone)); |
|
| 89 | } |
||
| 90 | |||
| 91 | 26 | return $date; |
|
|
0 ignored issues
–
show
|
|||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | 166 | public function supports(string $className): bool |
|
| 98 | { |
||
| 99 | 166 | return is_subclass_of($className, DateTimeInterface::class); |
|
| 100 | } |
||
| 101 | } |
||
| 102 |