Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
DateIntervalConverter | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 1 |
read | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
6.00 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Serialization\Converter; |
6 | |
7 | use DateInterval; |
8 | use DateTime; |
9 | use DateTimeImmutable; |
10 | use DateTimeInterface; |
11 | use Error; |
12 | use PeServer\Core\Text; |
13 | use PeServer\Core\Throws\InvalidOperationException; |
14 | use PeServer\Core\Time; |
15 | use ReflectionNamedType; |
16 | |
17 | |
18 | /** |
19 | * @extends TypeConverterBase<DateInterval|null> |
20 | */ |
21 | class DateIntervalConverter extends TypeConverterBase |
22 | { |
23 | #region TypeConverterBase |
24 | |
25 | public function read(string $name, ReflectionNamedType $type, mixed $raw): DateInterval|DateTimeInterface|null |
26 | { |
27 | $targetClassName = $type->getName(); |
28 | |
29 | if (is_string($raw) && !Text::isNullOrWhiteSpace($raw)) { |
30 | return Time::create($raw); |
31 | } |
32 | |
33 | if ($type->allowsNull()) { |
34 | return null; |
35 | } |
36 | |
37 | throw new InvalidOperationException("$name: $targetClassName"); |
38 | } |
39 | |
40 | #endregion |
41 | } |