Passed
Push — main ( 38d056...7e7092 )
by Oscar
12:07
created

SvgTrait::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-svg-extension
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Twig\Extension\Svg;
13
14
use Ocubom\Twig\Extension\Svg\Exception\ParseException;
15
use Ocubom\Twig\Extension\Svg\Util\DomUtil;
16
17
trait SvgTrait
18
{
19
    protected \DOMElement $svg;
20
21 7
    public function getElement(): \DOMElement
22
    {
23 7
        return $this->svg;
24
    }
25
26 4
    public function __toString(): string
27
    {
28 4
        return DomUtil::toXml($this->svg);
29
    }
30
31 1
    public function __serialize(): array
32
    {
33 1
        return ['svg' => (string) $this];
34
    }
35
36 1
    public function __unserialize(array $data): void
37
    {
38 1
        $doc = DomUtil::createDocument();
39 1
        if (false === $doc->loadXML($data['svg'])) {
40
            throw new ParseException(sprintf('Unable to load SVG string "%s".', func_get_arg(0))); // @codeCoverageIgnore
41
        }
42
43
        // Get first svg item
44 1
        $node = $doc->getElementsByTagName('svg')->item(0);
45
        assert($node instanceof \DOMElement);
46
47 1
        $this->svg = $node;
48
    }
49
}
50