Passed
Push — main ( 77e259...371264 )
by Oscar
08:09 queued 06:34
created

Symbol::generateSymbol()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 9
nop 1
crap 5
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\Util\DomHelper;
15
16
class Symbol implements SvgInterface
17
{
18
    use SvgTrait;
0 ignored issues
show
Bug introduced by
The trait Ocubom\Twig\Extension\Svg\SvgTrait requires the property $ownerDocument which is not provided by Ocubom\Twig\Extension\Svg\Symbol.
Loading history...
19
20
    protected \DOMElement $ref;
21
22 3
    public function __construct(\DOMElement $svg, iterable $options = null)
23
    {
24 3
        $svg = Svg::createFromString(DomHelper::toXml($svg), $options)->getElement();
25
26 3
        $this->svg = $this->generateSymbol($svg);
27 3
        $this->ref = $this->generateReference($svg);
28
    }
29
30 3
    public function getId(): string
31
    {
32 3
        return $this->svg->getAttribute('id');
33
    }
34
35 3
    public function getReference(): \DOMElement
36
    {
37 3
        return $this->ref;
38
    }
39
40 3
    private function generateSymbol(\DOMElement $svg): \DOMElement
41
    {
42 3
        $node = DomHelper::createElement('symbol');
43
44
        /** @var \DOMAttr $attribute */
45 3
        foreach ($svg->attributes as $attribute) {
46 3
            if (!self::isReferenceAllowedAttribute($attribute)) {
47 3
                $node->setAttribute($attribute->name, $attribute->value);
48
            }
49
        }
50
51
        /** @var \DOMNode $child */
52 3
        foreach ($svg->childNodes as $child) {
53 3
            if (!self::isReferenceAllowedNode($child)) {
54 3
                DomHelper::appendChildNode($child, $node);
55
            }
56
        }
57
58
        // Add the identifier based on node contents
59 3
        $node->setAttribute('id', Ident::generate($node));
60
61 3
        return $node;
62
    }
63
64 3
    private function generateReference(\DOMElement $svg): \DOMElement
65
    {
66 3
        $node = DomHelper::createElement('svg');
67
68
        /** @var \DOMAttr $attribute */
69 3
        foreach ($svg->attributes as $attribute) {
70 3
            if (self::isReferenceAllowedAttribute($attribute)) {
71 3
                $node->setAttribute($attribute->name, $attribute->value);
72
            }
73
        }
74
75
        /** @var \DOMNode $child */
76 3
        foreach ($svg->childNodes as $child) {
77 3
            if (self::isReferenceAllowedNode($child)) {
78 1
                DomHelper::appendChildNode($child, $node);
79
            }
80
        }
81
82 3
        $use = DomHelper::appendChildElement(
83 3
            DomHelper::createElement('use'),
84 3
            $node
85 3
        );
86 3
        $use->setAttribute('xlink:href', '#'.$this->getId());
87
88 3
        return $node;
89
    }
90
91 3
    private static function isReferenceAllowedAttribute(\DOMAttr $value): bool
92
    {
93
        // Block list
94 3
        return !in_array(strtolower($value->name), [
95 3
            'viewbox',
96 3
        ]);
97
    }
98
99 3
    private static function isReferenceAllowedNode(\DOMNode $node): bool
100
    {
101 3
        return $node instanceof \DOMElement
102 3
            // Allow list
103 3
            && in_array(strtolower($node->tagName), [
104 3
                'title',
105 3
                'desc',
106 3
            ]);
107
    }
108
}
109