1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\xsdTypes\AxillaryClasses; |
4
|
|
|
|
5
|
|
|
class XMLDateInterval extends \DateInterval |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* formating string like ISO 8601 (PnYnMnDTnHnMnS). |
9
|
|
|
*/ |
10
|
|
|
const INTERVAL_ISO8601 = 'P%yY%mM%dDT%hH%iM%sS'; |
11
|
|
|
private $pattern; |
12
|
|
|
private $patternLen; |
13
|
|
|
|
14
|
|
|
private $fractionOfSecond; |
15
|
|
|
|
16
|
|
|
public function __construct($intervalSpec, $pattern = 'PnYnMnDTnHnMnS') |
17
|
|
|
{ |
18
|
|
|
if (-1 === version_compare(PHP_VERSION, '7.1.0')) { |
19
|
|
|
$intervalSpec = $this->handleFractionOfSecond($intervalSpec); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
parent::__construct(trim($intervalSpec, '-')); |
22
|
|
|
if ($intervalSpec[0] == '-') { |
23
|
|
|
$this->invert = 1; |
24
|
|
|
} |
25
|
|
|
$this->pattern = trim($pattern); |
26
|
|
|
$this->patternLen = strlen($this->pattern); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function handleFractionOfSecond($intervalSpec) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$re = '/(?:[0-5][0-9]|60)(.\d+)S/'; |
|
|
|
|
32
|
|
|
preg_match_all($re, $intervalSpec, $matches, PREG_SET_ORDER, 0); |
33
|
|
|
if (1 != count($matches) || strpos($intervalSpec, '.') === false) { |
34
|
|
|
return $intervalSpec; |
35
|
|
|
} |
36
|
|
|
$this->fractionOfSecond = trim($matches[0][1], '.'); |
37
|
|
|
return str_replace($matches[0][1], '', $intervalSpec); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* formating the interval like ISO 8601 (PnYnMnDTnHnMnS). |
42
|
|
|
* |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
|
|
public function __toString() |
46
|
|
|
{ |
47
|
|
|
$sReturn = $this->handleSign(); |
48
|
|
|
$tSeen = false; |
49
|
|
|
for ($i = 0; $i < $this->patternLen; $i++) { |
50
|
|
|
$sReturn .= $this->handleChar($i, $tSeen); |
51
|
|
|
} |
52
|
|
|
return $sReturn; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function handleSign() |
59
|
|
|
{ |
60
|
|
|
if ($this->invert === 1) { |
61
|
|
|
return '-'; |
62
|
|
|
} |
63
|
|
|
return ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $i |
68
|
|
|
* @param bool $tSeen |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function handleChar($i, &$tSeen) |
73
|
|
|
{ |
74
|
|
|
switch ($this->pattern[$i]) { |
75
|
|
|
case 'n': |
76
|
|
|
return $this->handleN($i, $tSeen); |
77
|
|
|
case 'T': |
78
|
|
|
return $this->handleT($tSeen); |
79
|
|
|
default: |
80
|
|
|
return $this->handleOther($i); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $i |
86
|
|
|
* @param bool $tSeen |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
private function handleN($i, $tSeen) |
91
|
|
|
{ |
92
|
|
|
$componentProperty = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]); |
93
|
|
|
if ('s' === $componentProperty) { |
94
|
|
|
return $this->handleS(); |
95
|
|
|
} |
96
|
|
|
return $this->$componentProperty; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function handleS() |
100
|
|
|
{ |
101
|
|
|
return $this->f === 0 ? $this->s : trim($this->s . '.' . $this->f, '.'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param bool $tSeen |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
private function handleT(&$tSeen) |
110
|
|
|
{ |
111
|
|
|
$tSeen = true; |
112
|
|
|
return 'T'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $i |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function handleOther($i) |
121
|
|
|
{ |
122
|
|
|
return $this->pattern[$i]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function __get($name) |
126
|
|
|
{ |
127
|
|
|
if ($name == 'f') { |
128
|
|
|
return $this->fractionOfSecond; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|