|
1
|
|
|
<?php |
|
2
|
|
|
namespace nstdio\svg\import; |
|
3
|
|
|
|
|
4
|
|
|
use nstdio\svg\container\SVG; |
|
5
|
|
|
use nstdio\svg\ElementInterface; |
|
6
|
|
|
use nstdio\svg\util\KeyValueWriter; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Importer |
|
11
|
|
|
* |
|
12
|
|
|
* @package nstdio\svg\import |
|
13
|
|
|
* @author Edgar Asatryan <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Importer implements ImportInterface |
|
16
|
|
|
{ |
|
17
|
|
|
public static $map = [ |
|
18
|
|
|
'animate' => ['animation', ['attributeName', 'from', 'to', 'dur']], |
|
19
|
|
|
'mpath' => ['animation', []], |
|
20
|
|
|
'animateMotion' => ['animation', []], |
|
21
|
|
|
'set' => ['animation', []], |
|
22
|
|
|
'svg' => ['container', []], |
|
23
|
|
|
'a' => ['container', []], |
|
24
|
|
|
'defs' => ['container', []], |
|
25
|
|
|
'g' => ['container', []], |
|
26
|
|
|
'marker' => ['container', []], |
|
27
|
|
|
'mask' => ['container', []], |
|
28
|
|
|
'pattern' => ['container', []], |
|
29
|
|
|
'switch' => ['container', []], |
|
30
|
|
|
'symbol' => ['container', []], |
|
31
|
|
|
'desc' => ['desc', ['nodeValue']], |
|
32
|
|
|
'metadata' => ['desc', ['nodeValue']], |
|
33
|
|
|
'title' => ['desc', ['nodeValue']], |
|
34
|
|
|
'filter' => ['filter', []], |
|
35
|
|
|
'feBlend' => ['filter', []], |
|
36
|
|
|
'feColorMatrix' => ['filter', []], |
|
37
|
|
|
'feComponentTransfer' => ['filter', []], |
|
38
|
|
|
'feComposite' => ['filter', []], |
|
39
|
|
|
'feConvolveMatrix' => ['filter', []], |
|
40
|
|
|
'feDiffuseLighting' => ['filter', []], |
|
41
|
|
|
'feDisplacementMap' => ['filter', []], |
|
42
|
|
|
'feFlood' => ['filter', []], |
|
43
|
|
|
'feFuncA' => ['filter', []], |
|
44
|
|
|
'feFuncB' => ['filter', []], |
|
45
|
|
|
'feFuncG' => ['filter', []], |
|
46
|
|
|
'feFuncR' => ['filter', []], |
|
47
|
|
|
'feGaussianBlur' => ['filter', []], |
|
48
|
|
|
'image' => ['filter', []], |
|
49
|
|
|
'feMerge' => ['filter', []], |
|
50
|
|
|
'feMergeNode' => ['filter', []], |
|
51
|
|
|
'feMorphology' => ['filter', []], |
|
52
|
|
|
'feOffset' => ['filter', []], |
|
53
|
|
|
'feSpecularLighting' => ['filter', []], |
|
54
|
|
|
'feTile' => ['filter', []], |
|
55
|
|
|
'feTurbulence' => ['filter', []], |
|
56
|
|
|
'font' => ['font', []], |
|
57
|
|
|
'font-face' => ['font', []], |
|
58
|
|
|
'font-face-format' => ['font', []], |
|
59
|
|
|
'font-face-name' => ['font', []], |
|
60
|
|
|
'font-face-src' => ['font', []], |
|
61
|
|
|
'font-face-uri' => ['font', []], |
|
62
|
|
|
'glyph' => ['font', []], |
|
63
|
|
|
'missing-glyph' => ['font', []], |
|
64
|
|
|
'hkern' => ['font', []], |
|
65
|
|
|
'vkern' => ['font', []], |
|
66
|
|
|
'linearGradient' => ['gradient', []], |
|
67
|
|
|
'radialGradient' => ['gradient', []], |
|
68
|
|
|
'stop' => ['gradient', []], |
|
69
|
|
|
'feDistantLight' => ['light', []], |
|
70
|
|
|
'fePointLight' => ['light', []], |
|
71
|
|
|
'feSpotLight' => ['light', []], |
|
72
|
|
|
'circle' => ['shape', ['cx', 'cy', 'r']], |
|
73
|
|
|
'ellipse' => ['shape', ['cx', 'cy', 'rx', 'ry']], |
|
74
|
|
|
'line' => ['shape', ['x1', 'y1', 'x2', 'y2']], |
|
75
|
|
|
'path' => ['shape', ['x', 'y']], |
|
76
|
|
|
'polygon' => ['shape', []], |
|
77
|
|
|
'polyline' => ['shape', []], |
|
78
|
|
|
'rect' => ['shape', ['height', 'width', 'x', 'y']], |
|
79
|
|
|
'altGlyph' => ['text', []], |
|
80
|
|
|
'altGlyphDef' => ['text', []], |
|
81
|
|
|
'altGlyphItem' => ['text', []], |
|
82
|
|
|
'glyphRef' => ['text', []], |
|
83
|
|
|
'text' => ['text', ['nodeValue']], |
|
84
|
|
|
'textPath' => ['text', []], |
|
85
|
|
|
'tref' => ['text', []], |
|
86
|
|
|
'tspan' => ['text', []], |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
3 |
|
public function fromString($svgString) |
|
90
|
|
|
{ |
|
91
|
3 |
|
$dom = new \DOMDocument('1.0', 'utf-8'); |
|
92
|
|
|
|
|
93
|
3 |
|
$dom->loadXML($svgString); |
|
94
|
|
|
|
|
95
|
3 |
|
$svgNode = $dom->getElementsByTagName('svg')->item(0); |
|
96
|
3 |
|
if ($svgNode === null) { |
|
97
|
1 |
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** @var SVG $svg */ |
|
101
|
2 |
|
$svg = $this->toObject($svgNode, null); |
|
|
|
|
|
|
102
|
2 |
|
$svg->removeChild($svg->getFirstChild()); |
|
103
|
|
|
|
|
104
|
2 |
|
$this->buildObjectTree($svgNode, $svg); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
2 |
|
return $svg; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param \DOMElement $element |
|
111
|
|
|
* |
|
112
|
|
|
* @param $parent |
|
113
|
|
|
* |
|
114
|
|
|
* @return ElementInterface |
|
115
|
|
|
*/ |
|
116
|
2 |
|
private function toObject($element, $parent) |
|
117
|
|
|
{ |
|
118
|
2 |
|
$map = $this->getObjectCtor($element); |
|
119
|
2 |
|
if ($map === null) { |
|
120
|
1 |
|
return null; |
|
121
|
|
|
} |
|
122
|
2 |
|
$args = [$parent]; |
|
123
|
|
|
|
|
124
|
2 |
|
$reflection = new ReflectionClass($map[0]); |
|
125
|
|
|
|
|
126
|
2 |
|
if (!empty($map[1])) { |
|
127
|
1 |
|
$args = array_merge($args, $this->getAttrs($element, $map[1])); |
|
|
|
|
|
|
128
|
1 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** @var ElementInterface $object */ |
|
131
|
2 |
|
$object = $reflection->newInstanceArgs($args); |
|
132
|
2 |
|
$object->apply(KeyValueWriter::allAttributes($element)); |
|
133
|
|
|
|
|
134
|
2 |
|
return $object; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
private function getObjectCtor(\DOMElement $element) |
|
138
|
|
|
{ |
|
139
|
2 |
|
if (isset(self::$map[$element->nodeName]) === false) { |
|
140
|
1 |
|
return null; |
|
141
|
|
|
} |
|
142
|
2 |
|
$map = self::$map[$element->nodeName]; |
|
143
|
2 |
|
$map[0] = 'nstdio\\svg\\' . $map[0] . '\\' . $element->nodeName; |
|
144
|
|
|
|
|
145
|
2 |
|
return $map; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param \DOMElement $element |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $attrs |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
1 |
|
private function getAttrs(\DOMElement $element, array $attrs) |
|
156
|
|
|
{ |
|
157
|
1 |
|
$ret = []; |
|
158
|
1 |
|
foreach ($element->attributes as $item) { |
|
159
|
1 |
|
if (in_array($item->nodeName, $attrs)) { |
|
160
|
1 |
|
$ret[$item->nodeName] = $item->nodeValue; |
|
161
|
1 |
|
} |
|
162
|
1 |
|
} |
|
163
|
1 |
|
if (in_array('nodeValue', $attrs)) { |
|
164
|
1 |
|
$ret['nodeValue'] = $element->nodeValue; |
|
165
|
1 |
|
} |
|
166
|
|
|
|
|
167
|
1 |
|
$diff = array_diff($attrs, array_keys($ret)); |
|
168
|
1 |
|
foreach ($diff as $item) { |
|
169
|
1 |
|
$ret[$item] = null; |
|
170
|
1 |
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
return array_values($ret); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
2 |
|
private function buildObjectTree(\DOMElement $element, ElementInterface $obj) |
|
176
|
|
|
{ |
|
177
|
|
|
/** @var \DOMElement $item */ |
|
178
|
2 |
|
foreach ($element->childNodes as $key => $item) { |
|
179
|
2 |
|
if ($item instanceof \DOMText) continue; |
|
180
|
2 |
|
$parent = $this->toObject($item, $obj); |
|
181
|
2 |
|
if ($item->hasChildNodes() && $parent !== null) { |
|
182
|
1 |
|
$this->buildObjectTree($item, $parent); |
|
183
|
1 |
|
} |
|
184
|
2 |
|
} |
|
185
|
2 |
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.