1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Handler\DataTypeHandler; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* DateInterval handler |
8
|
|
|
* |
9
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Handler\DateIntervalHandler |
10
|
|
|
*/ |
11
|
|
|
class DateIntervalHandler implements DataTypeHandlerInterface |
12
|
|
|
{ |
13
|
|
|
const PARAMETER_FORMAT = 0; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
3 |
|
public function toResource($value, string $type, array $parameters) |
19
|
|
|
{ |
20
|
3 |
|
if ($value === null) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
if (! $value instanceof \DateInterval) { |
25
|
|
|
$value = new \DateInterval((string) $value); |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
|
if (isset($parameters[self::PARAMETER_FORMAT])) { |
29
|
1 |
|
return $value->format($parameters[self::PARAMETER_FORMAT]); |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
$format = $this->resolveFormat($value); |
33
|
|
|
|
34
|
2 |
|
if ($format !== '') { |
35
|
1 |
|
return $value->format($format); |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Resolve ISO_8601 duration format |
41
|
|
|
* |
42
|
|
|
* @param \DateInterval $interval |
43
|
|
|
* @return string | null |
44
|
|
|
*/ |
45
|
2 |
|
protected function resolveFormat(\DateInterval $interval): string |
46
|
|
|
{ |
47
|
2 |
|
$baseFormat = $this->resolveBaseFormat($interval); |
48
|
2 |
|
$timeFormat = $this->resolveTimeFormat($interval); |
49
|
|
|
|
50
|
2 |
|
if ($baseFormat === '') { |
51
|
1 |
|
if ($timeFormat === '') { |
52
|
1 |
|
return ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return 'PT' . $timeFormat; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
if ($timeFormat === '') { |
59
|
1 |
|
return 'P' . $baseFormat; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 'P' . $baseFormat . 'T' . $timeFormat; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Resolve base part of interval format |
67
|
|
|
* |
68
|
|
|
* @param \DateInterval $interval |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
2 |
|
protected function resolveBaseFormat(\DateInterval $interval): string |
72
|
|
|
{ |
73
|
2 |
|
$format = ''; |
74
|
|
|
|
75
|
2 |
|
if ($interval->y > 0) { |
76
|
1 |
|
$format .= '%yY'; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
if ($interval->m > 0) { |
80
|
|
|
$format .= '%mM'; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if ($interval->d > 0) { |
84
|
|
|
$format .= '%dD'; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $format; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Resolve time part of interval format |
92
|
|
|
* |
93
|
|
|
* @param \DateInterval $interval |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
protected function resolveTimeFormat(\DateInterval $interval): string |
97
|
|
|
{ |
98
|
2 |
|
$format = ''; |
99
|
|
|
|
100
|
2 |
|
if ($interval->h > 0) { |
101
|
|
|
$format .= '%hH'; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if ($interval->i > 0) { |
105
|
|
|
$format .= '%iM'; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
if ($interval->s > 0) { |
109
|
|
|
$format .= '%sS'; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
return $format; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function fromResource($value, string $type, array $parameters) |
119
|
|
|
{ |
120
|
1 |
|
if ($value === null) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
if ($value instanceof \DateInterval) { |
125
|
|
|
return $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return new \DateInterval((string) $value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function supports(): array |
135
|
|
|
{ |
136
|
|
|
return ['dateinterval']; |
137
|
|
|
} |
138
|
|
|
} |