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\DomUtil; |
15
|
|
|
|
16
|
|
|
class Symbol implements SvgInterface |
17
|
|
|
{ |
18
|
|
|
use SvgTrait; |
19
|
|
|
|
20
|
|
|
protected \DOMElement $ref; |
21
|
|
|
|
22
|
4 |
|
public function __construct(\DOMElement $svg, iterable $options = null) |
23
|
|
|
{ |
24
|
4 |
|
$svg = (new Svg($svg, $options))->getElement(); |
25
|
|
|
|
26
|
4 |
|
$this->svg = $this->generateSymbol($svg); |
27
|
4 |
|
$this->ref = $this->generateReference($svg); |
28
|
|
|
} |
29
|
|
|
|
30
|
4 |
|
public function getId(): string |
31
|
|
|
{ |
32
|
4 |
|
return $this->svg->getAttribute('id'); |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
public function getReference(): \DOMElement |
36
|
|
|
{ |
37
|
4 |
|
return $this->ref; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
private function generateSymbol(\DOMElement $svg): \DOMElement |
41
|
|
|
{ |
42
|
4 |
|
$node = DomUtil::createElement('symbol'); |
43
|
|
|
|
44
|
|
|
/** @var \DOMAttr $attribute */ |
45
|
4 |
|
foreach ($svg->attributes as $attribute) { |
46
|
4 |
|
if (!self::isReferenceAllowedAttribute($attribute)) { |
47
|
4 |
|
$node->setAttribute($attribute->name, $attribute->value); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @var \DOMNode $child */ |
52
|
4 |
|
foreach ($svg->childNodes as $child) { |
53
|
4 |
|
if (!self::isReferenceAllowedNode($child)) { |
54
|
4 |
|
DomUtil::appendChildNode($child, $node); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Add the identifier based on node contents |
59
|
4 |
|
$node->setAttribute('id', DomUtil::generateId($node)); |
60
|
|
|
|
61
|
4 |
|
return $node; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
private function generateReference(\DOMElement $svg): \DOMElement |
65
|
|
|
{ |
66
|
4 |
|
$node = DomUtil::createElement('svg'); |
67
|
|
|
|
68
|
|
|
/** @var \DOMAttr $attribute */ |
69
|
4 |
|
foreach ($svg->attributes as $attribute) { |
70
|
4 |
|
if (self::isReferenceAllowedAttribute($attribute)) { |
71
|
4 |
|
$node->setAttribute($attribute->name, $attribute->value); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @var \DOMNode $child */ |
76
|
4 |
|
foreach ($svg->childNodes as $child) { |
77
|
4 |
|
if (self::isReferenceAllowedNode($child)) { |
78
|
1 |
|
DomUtil::appendChildNode($child, $node); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
$use = DomUtil::appendChildElement( |
83
|
4 |
|
DomUtil::createElement('use'), |
84
|
4 |
|
$node |
85
|
4 |
|
); |
86
|
4 |
|
$use->setAttribute('xlink:href', '#'.$this->getId()); |
87
|
|
|
|
88
|
4 |
|
return $node; |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
private static function isReferenceAllowedAttribute(\DOMAttr $value): bool |
92
|
|
|
{ |
93
|
|
|
// Block list |
94
|
4 |
|
return !in_array(mb_strtolower($value->name), [ |
95
|
4 |
|
'viewbox', |
96
|
4 |
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
private static function isReferenceAllowedNode(\DOMNode $node): bool |
100
|
|
|
{ |
101
|
4 |
|
return $node instanceof \DOMElement |
102
|
4 |
|
// Allow list |
103
|
4 |
|
&& in_array(mb_strtolower($node->tagName), [ |
104
|
4 |
|
'title', |
105
|
4 |
|
'desc', |
106
|
4 |
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|