1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inspirum\XML\Services; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class Formatter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Parse node name to namespace prefix and local name |
11
|
|
|
* |
12
|
|
|
* @param string $name |
13
|
|
|
* |
14
|
|
|
* @return array<int,string|null> |
15
|
|
|
*/ |
16
|
|
|
public static function parseQualifiedName(string $name): array |
17
|
|
|
{ |
18
|
|
|
static::validateElementName($name); |
|
|
|
|
19
|
|
|
|
20
|
|
|
return array_pad(explode(':', $name, 2), -2, null); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get local name from node name |
25
|
|
|
* |
26
|
|
|
* @param string $name |
27
|
|
|
* |
28
|
|
|
* @return string|null |
29
|
|
|
*/ |
30
|
|
|
public static function getLocalName(string $name): ?string |
31
|
|
|
{ |
32
|
|
|
return static::parseQualifiedName($name)[1]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get namespace prefix from node name |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* |
40
|
|
|
* @return string|null |
41
|
|
|
*/ |
42
|
|
|
public static function getNamespacePrefix(string $name): ?string |
43
|
|
|
{ |
44
|
|
|
return static::parseQualifiedName($name)[0]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validate element name |
49
|
|
|
* |
50
|
|
|
* @param string $value |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
* |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
private static function validateElementName(string $value): void |
57
|
|
|
{ |
58
|
|
|
$regex = '/^[a-zA-Z][a-zA-Z0-9]*(\:[a-zA-Z][a-zA-Z0-9]*)?$/'; |
59
|
|
|
|
60
|
|
|
if (preg_match($regex, $value) !== 1) { |
61
|
|
|
throw new InvalidArgumentException( |
62
|
|
|
sprintf('Element name or namespace prefix [%s] has invalid value', $value) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Normalize value. |
69
|
|
|
* |
70
|
|
|
* @param mixed $value |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public static function encodeValue($value) |
75
|
|
|
{ |
76
|
|
|
if (is_bool($value)) { |
77
|
|
|
$value = $value ? 'true' : 'false'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Normalize value. |
85
|
|
|
* |
86
|
|
|
* @param mixed $value |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public static function decodeValue($value) |
91
|
|
|
{ |
92
|
|
|
if (is_numeric($value)) { |
93
|
|
|
return $value + 0; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (in_array($value, ['true', 'True'])) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (in_array($value, ['false', 'False'])) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: