SvgTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 2
b 0
f 0
dl 0
loc 41
ccs 13
cts 15
cp 0.8667
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getElement() 0 3 1
A __serialize() 0 3 1
A toXml() 0 3 1
A __unserialize() 0 12 2
A toHtml() 0 3 1
A __toString() 0 3 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 8
    public function getElement(): \DOMElement
22
    {
23 8
        return $this->svg;
24
    }
25
26
    public function toHtml(bool $minify = false): string
27
    {
28
        return DomUtil::toHtml($this->svg, $minify);
29
    }
30
31 4
    public function toXml(bool $minify = false): string
32
    {
33 4
        return DomUtil::toXml($this->svg, $minify);
34
    }
35
36 1
    public function __toString(): string
37
    {
38 1
        return $this->toXml();
39
    }
40
41 1
    public function __serialize(): array
42
    {
43 1
        return ['svg' => (string) $this];
44
    }
45
46 1
    public function __unserialize(array $data): void
47
    {
48 1
        $doc = DomUtil::createDocument();
49 1
        if (false === $doc->loadXML($data['svg'])) {
50
            throw new ParseException(sprintf('Unable to load SVG string "%s".', func_get_arg(0))); // @codeCoverageIgnore
51
        }
52
53
        // Get first svg item
54 1
        $node = $doc->getElementsByTagName('svg')->item(0);
55
        assert($node instanceof \DOMElement);
56
57 1
        $this->svg = $node;
58
    }
59
}
60