-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateTimeNormalizer.php
181 lines (161 loc) · 5.87 KB
/
DateTimeNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Serializer\Normalizers;
use DateTimeInterface;
use Exception;
use Illuminate\Support\Facades\Date;
use LastDragon_ru\LaraASP\Core\Utils\Cast;
use LastDragon_ru\LaraASP\Serializer\Normalizers\Traits\WithDefaultContext;
use Override;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer as SymfonyDateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function get_debug_type;
use function is_a;
use function is_string;
use function sprintf;
/**
* Normalizes/Denormalizes an object implementing the {@see DateTimeInterface}
* to/from a date string.
*
* It is designed specially for Laravel and respects how we are usually work
* with Dates. Here are few differences/improvements from
* {@see SymfonyDateTimeNormalizer}:
*
* - It supports denormalization for any object that implements {@see DateTimeInterface}.
* - It uses {@see Date::createFromFormat()} for denormalization to make sure
* that the proper `DateTime` instance will be used.
* - If {@see Date::createFromFormat()} failed and {@see self::ContextFallback}
* enabled the {@see Date::make()} will be used. It may be useful, for example,
* if {@see self::ContextFormat} was changed.
*
* @see DateTimeNormalizerContextBuilder
* @see SymfonyDateTimeNormalizer
*/
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface {
use WithDefaultContext;
final public const string ContextFormat = self::class.'@format';
final public const string ContextFormatDefault = DateTimeInterface::RFC3339_EXTENDED;
final public const string ContextFallback = self::class.'@fallback';
final public const bool ContextFallbackDefault = false;
/**
* @param array<string, mixed> $defaultContext
*/
public function __construct(array $defaultContext = []) {
$this->setDefaultContext($defaultContext);
}
/**
* @return array<class-string, bool>
*/
#[Override]
public function getSupportedTypes(?string $format): array {
return [
DateTimeInterface::class => self::class === static::class,
];
}
/**
* @param array<array-key, mixed> $context
*/
#[Override]
public function normalize(mixed $object, ?string $format = null, array $context = []): string {
if (!($object instanceof DateTimeInterface)) {
throw new InvalidArgumentException(
sprintf(
'The `$object` expected to be a `%s`, `%s` given.',
DateTimeInterface::class,
get_debug_type($object),
),
);
}
return $object->format(
$this->getContextOptionFormat($context),
);
}
/**
* @param array<array-key, mixed> $context
*/
#[Override]
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool {
return $data instanceof DateTimeInterface;
}
/**
* @param array<array-key, mixed> $context
*/
#[Override]
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed {
// Just for the case
if (!is_string($data)) {
throw new InvalidArgumentException(
sprintf(
'The `$data` expected to be a `%s`, `%s` given.',
'string',
get_debug_type($data),
),
);
}
if (!is_a($type, DateTimeInterface::class, true)) {
throw new InvalidArgumentException(
sprintf(
'The `$type` expected to be a `%s`, `%s` given.',
DateTimeInterface::class,
get_debug_type($data),
),
);
}
// Facade is called to make sure that the expected DateTime class will be used.
$fallback = $this->getContextOptionFallback($context);
$format = $this->getContextOptionFormat($context);
$result = null;
$error = null;
try {
$result = Date::createFromFormat($format, $data);
} catch (Exception $exception) {
if ($fallback) {
$error = $exception;
} else {
throw $exception;
}
}
if (!($result instanceof DateTimeInterface) && $fallback) {
try {
$result = Date::make($data);
} catch (Exception $exception) {
throw $error ?? $exception;
}
}
if (!($result instanceof DateTimeInterface)) {
throw new UnexpectedValueException(
sprintf(
'The `%s` cannot be parsed to `DateTime`.',
$data,
),
);
}
return $result;
}
/**
* @param array<array-key, mixed> $context
*/
#[Override]
public function supportsDenormalization(
mixed $data,
string $type,
?string $format = null,
array $context = [],
): bool {
return is_a($type, DateTimeInterface::class, true);
}
/**
* @param array<array-key, mixed> $context
*/
protected function getContextOptionFormat(array $context): string {
return Cast::toString($this->getContextOption($context, self::ContextFormat, self::ContextFormatDefault));
}
/**
* @param array<array-key, mixed> $context
*/
protected function getContextOptionFallback(array $context): bool {
return Cast::toBool($this->getContextOption($context, self::ContextFallback, self::ContextFallbackDefault));
}
}