Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
DomToString
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
4.43
0.00% covered (danger)
0.00%
0 / 1
 convert
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
4.43
1<?php
2namespace Apie\StorageMetadata\Converters;
3
4use Apie\TypeConverter\ConverterInterface;
5use DOMAttr;
6use DOMDocument;
7use DOMElement;
8use ReflectionType;
9
10/**
11 * @implements ConverterInterface<DOMAttr|DOMElement|null, ?string>
12 */
13class DomToString implements ConverterInterface
14{
15    public function convert(DOMAttr|DOMElement|null $input, ?ReflectionType $wantedType): ?string
16    {
17        if ($input === null) {
18            return null;
19        }
20        if ($input instanceof DOMAttr) {
21            return $input->name . '="' . htmlspecialchars($input->value, ENT_XML1 | ENT_QUOTES, 'UTF-8') . '"';
22        }
23
24        $document = $input->ownerDocument ?? new DOMDocument();
25        $node = $input;
26        if ($input->ownerDocument === null) {
27            $node = $document->importNode($input, true);
28            $document->appendChild($node);
29        }
30        return (string) $document->saveXML($node);
31    }
32}