forked from phpstan/phpstan-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlParameterMapFactory.php
124 lines (100 loc) · 2.82 KB
/
XmlParameterMapFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php declare(strict_types = 1);
namespace PHPStan\Symfony;
use InvalidArgumentException;
use PHPStan\ShouldNotHappenException;
use SimpleXMLElement;
use function base64_decode;
use function count;
use function file_get_contents;
use function is_numeric;
use function ksort;
use function simplexml_load_string;
use function sprintf;
use function strpos;
final class XmlParameterMapFactory implements ParameterMapFactory
{
private ?string $containerXml = null;
public function __construct(?string $containerXmlPath)
{
$this->containerXml = $containerXmlPath;
}
public function create(): ParameterMap
{
if ($this->containerXml === null) {
return new FakeParameterMap();
}
$fileContents = file_get_contents($this->containerXml);
if ($fileContents === false) {
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXml));
}
$xml = @simplexml_load_string($fileContents);
if ($xml === false) {
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXml));
}
/** @var Parameter[] $parameters */
$parameters = [];
if (count($xml->parameters) > 0) {
foreach ($xml->parameters->parameter as $def) {
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();
$parameter = new Parameter(
(string) $attrs->key,
$this->getNodeValue($def),
);
$parameters[$parameter->getKey()] = $parameter;
}
}
ksort($parameters);
return new DefaultParameterMap($parameters);
}
/**
* @return array<mixed>|bool|float|int|string
*/
private function getNodeValue(SimpleXMLElement $def)
{
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();
$value = null;
switch ((string) $attrs->type) {
case 'collection':
$value = [];
$children = $def->children();
if ($children === null) {
throw new ShouldNotHappenException();
}
foreach ($children as $child) {
/** @var SimpleXMLElement $childAttrs */
$childAttrs = $child->attributes();
if (isset($childAttrs->key)) {
$value[(string) $childAttrs->key] = $this->getNodeValue($child);
} else {
$value[] = $this->getNodeValue($child);
}
}
break;
case 'string':
$value = (string) $def;
break;
case 'binary':
$value = base64_decode((string) $def, true);
if ($value === false) {
throw new InvalidArgumentException(sprintf('Parameter "%s" of binary type is not valid base64 encoded string.', (string) $attrs->key));
}
break;
default:
$value = (string) $def;
if (is_numeric($value)) {
if (strpos($value, '.') !== false) {
$value = (float) $value;
} else {
$value = (int) $value;
}
} elseif ($value === 'true') {
$value = true;
} elseif ($value === 'false') {
$value = false;
}
}
return $value;
}
}