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