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
|
|
|
public function __construct($intervalSpec, $pattern = 'PnYnMnDTnHnMnS') |
15
|
|
|
{ |
16
|
|
|
parent::__construct(trim($intervalSpec, '-')); |
17
|
|
|
if ($intervalSpec[0] == '-') { |
18
|
|
|
$this->invert = 1; |
19
|
|
|
} |
20
|
|
|
$this->pattern = trim($pattern); |
21
|
|
|
$this->patternLen = strlen($this->pattern); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* formating the interval like ISO 8601 (PnYnMnDTnHnMnS). |
26
|
|
|
* |
27
|
|
|
* @return string|null |
28
|
|
|
*/ |
29
|
|
|
public function __toString() |
30
|
|
|
{ |
31
|
|
|
$sReturn = $this->handleSign(); |
32
|
|
|
$tSeen = false; |
33
|
|
|
for ($i = 0; $i < $this->patternLen; $i++) { |
34
|
|
|
$sReturn .= $this->handleChar($i, $tSeen); |
35
|
|
|
} |
36
|
|
|
return $sReturn; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
private function handleSign() |
43
|
|
|
{ |
44
|
|
|
if ($this->invert === 1) { |
45
|
|
|
return '-'; |
46
|
|
|
} |
47
|
|
|
return ""; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $i |
52
|
|
|
* @param bool $tSeen |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
private function handleChar($i, &$tSeen) |
57
|
|
|
{ |
58
|
|
|
switch ($this->pattern[$i]) { |
59
|
|
|
case 'n': |
60
|
|
|
return $this->handleN($i, $tSeen); |
61
|
|
|
case 'T': |
62
|
|
|
return $this->handleT($tSeen); |
63
|
|
|
default: |
64
|
|
|
return $this->handleOther($i); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $i |
70
|
|
|
* @param bool $tSeen |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
private function handleN($i, $tSeen) |
75
|
|
|
{ |
76
|
|
|
$v = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]); |
77
|
|
|
return $this->$v; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param bool $tSeen |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
private function handleT(&$tSeen) |
86
|
|
|
{ |
87
|
|
|
$tSeen = true; |
88
|
|
|
return 'T'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $i |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
private function handleOther($i) |
97
|
|
|
{ |
98
|
|
|
return $this->pattern[$i]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.