1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PagOnline; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class IgfsUtils. |
9
|
|
|
*/ |
10
|
|
|
class IgfsUtils |
11
|
|
|
{ |
12
|
|
|
public const DATE_FORMATS = [ |
13
|
|
|
'j-M-Y H:i:s.000P', |
14
|
|
|
'j-M-Y H:i:s.00P', |
15
|
|
|
'j-M-Y H:i:s.0P', |
16
|
|
|
'j-M-Y H:i:sP', |
17
|
|
|
'j-M-Y H:i:s.000', |
18
|
|
|
'j-M-Y H:i:s.00', |
19
|
|
|
'j-M-Y H:i:s.0', |
20
|
|
|
'j-M-Y H:i:s', |
21
|
|
|
'Y-m-d H:i:sP', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get value from array map. |
26
|
|
|
* |
27
|
|
|
* @param array $map |
28
|
|
|
* @param string $key |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
9 |
|
public static function getValue($map, $key) |
33
|
|
|
{ |
34
|
9 |
|
return $map[$key] ?? null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* TODO: migrate to UUID? |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
16 |
|
public static function getUniqueBoundaryValue(): string |
43
|
|
|
{ |
44
|
16 |
|
return \uniqid(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Transform DOM nodes to associative array. |
49
|
|
|
* |
50
|
|
|
* @param \SimpleXMLElement $nodes |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
9 |
|
public static function parseResponseFields(\SimpleXMLElement $nodes): array |
55
|
|
|
{ |
56
|
9 |
|
$fields = []; |
57
|
9 |
|
foreach ($nodes->children() as $item) { |
58
|
9 |
|
if (\count($item) === 0) { |
|
|
|
|
59
|
9 |
|
$fields[$item->getName()] = \trim((string) $item); |
60
|
|
|
} else { |
61
|
3 |
|
$fields[$item->getName()] = (string) $item->asXML(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
return $fields; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Format timestamp to XML Gregorian Calendar (?). |
70
|
|
|
* |
71
|
|
|
* @param int $timestamp |
72
|
|
|
* |
73
|
|
|
* @return null|string |
74
|
|
|
*/ |
75
|
15 |
|
public static function formatXMLGregorianCalendar($timestamp): ?string |
76
|
|
|
{ |
77
|
15 |
|
$integerTimestamp = (int) $timestamp; |
78
|
15 |
|
if ($integerTimestamp === 0) { |
79
|
9 |
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
$dateTimeObject = (new DateTimeImmutable())->setTimestamp($integerTimestamp); |
83
|
|
|
|
84
|
6 |
|
return $dateTimeObject->format('Y-m-d'). |
85
|
6 |
|
'T'. |
86
|
6 |
|
$dateTimeObject->format('H:i:s'). |
87
|
6 |
|
$dateTimeObject->format('P'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param null|string $text |
92
|
|
|
* |
93
|
|
|
* @return null|\DateTimeImmutable |
94
|
|
|
*/ |
95
|
4 |
|
public static function parseXMLGregorianCalendar(?string $text) |
96
|
|
|
{ |
97
|
4 |
|
if (empty($text)) { |
98
|
1 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
$date = null; |
102
|
4 |
|
$text = \str_replace('T', ' ', $text); |
103
|
4 |
|
foreach (self::DATE_FORMATS as $dateFormat) { |
104
|
4 |
|
$date = self::parseDateFormat($text, $dateFormat); |
105
|
4 |
|
if ($date !== null) { |
106
|
3 |
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
return $date; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $text |
115
|
|
|
* @param string $format Date format |
116
|
|
|
* |
117
|
|
|
* @return null|\DateTimeImmutable |
118
|
|
|
*/ |
119
|
6 |
|
public static function parseDateFormat(string $text, string $format): ?DateTimeImmutable |
120
|
|
|
{ |
121
|
6 |
|
$date = DateTimeImmutable::createFromFormat($format, $text); |
122
|
|
|
|
123
|
6 |
|
return $date ?: null; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|