Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StringToDom | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
5.15 | |
0.00% |
0 / 1 |
| convert | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
5.15 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Converters; |
| 3 | |
| 4 | use Apie\Core\Utils\ConverterUtils; |
| 5 | use Apie\TypeConverter\ConverterInterface; |
| 6 | use DOMAttr; |
| 7 | use DOMDocument; |
| 8 | use DOMElement; |
| 9 | use ReflectionType; |
| 10 | |
| 11 | /** |
| 12 | * @implements ConverterInterface<?string, DOMAttr|DOMElement|null> |
| 13 | */ |
| 14 | class StringToDom implements ConverterInterface |
| 15 | { |
| 16 | public function convert(?string $input, ?ReflectionType $wantedType): DOMAttr|DOMElement|null |
| 17 | { |
| 18 | if ($input === null && $wantedType?->allowsNull()) { |
| 19 | return null; |
| 20 | } |
| 21 | $class = ConverterUtils::toReflectionClass($wantedType); |
| 22 | assert($class !== null); |
| 23 | if ($class->name === DOMAttr::class) { |
| 24 | if (preg_match('/^([A-Za-z_][A-Za-z0-9_.:-]*)="(.*)"$/s', $input, $matches)) { |
| 25 | return new DOMAttr($matches[1], htmlspecialchars_decode($matches[2], ENT_XML1)); |
| 26 | } |
| 27 | return new DOMAttr('value', $input); |
| 28 | } |
| 29 | |
| 30 | $document = new DOMDocument(); |
| 31 | $document->loadXML($input, LIBXML_NONET); |
| 32 | return $document->documentElement; |
| 33 | } |
| 34 | } |