1 | <?php |
||
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 | 2 | public function fromString($svgString) |
|
108 | |||
109 | /** |
||
110 | * @param \DOMElement $element |
||
111 | * |
||
112 | * @param $parent |
||
113 | * |
||
114 | * @return ElementInterface |
||
115 | */ |
||
116 | 1 | private function toObject($element, $parent) |
|
117 | { |
||
118 | 1 | $map = $this->getObjectCtor($element); |
|
119 | 1 | if ($map === null) { |
|
120 | return null; |
||
121 | } |
||
122 | 1 | $args = [$parent]; |
|
123 | |||
124 | 1 | $reflection = new ReflectionClass($map[0]); |
|
125 | |||
126 | 1 | if (!empty($map[1])) { |
|
127 | $args = array_merge($args, $this->getAttrs($element, $map[1])); |
||
128 | } |
||
129 | |||
130 | /** @var ElementInterface $object */ |
||
131 | 1 | $object = $reflection->newInstanceArgs($args); |
|
132 | 1 | $object->apply(KeyValueWriter::allAttributes($element)); |
|
133 | |||
134 | 1 | return $object; |
|
135 | } |
||
136 | |||
137 | 1 | private function getObjectCtor(\DOMElement $element) |
|
147 | |||
148 | /** |
||
149 | * @param \DOMElement $element |
||
150 | * |
||
151 | * @param array $attrs |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | private function getAttrs(\DOMElement $element, array $attrs) |
||
156 | { |
||
157 | $ret = []; |
||
158 | foreach ($element->attributes as $item) { |
||
159 | if (in_array($item->nodeName, $attrs)) { |
||
160 | $ret[$item->nodeName] = $item->nodeValue; |
||
161 | } |
||
162 | } |
||
163 | if (in_array('nodeValue', $attrs)) { |
||
164 | $ret['nodeValue'] = $element->nodeValue; |
||
165 | } |
||
166 | |||
167 | $diff = array_diff($attrs, array_keys($ret)); |
||
168 | foreach ($diff as $item) { |
||
169 | $ret[$item] = null; |
||
170 | } |
||
171 | |||
172 | return array_values($ret); |
||
173 | } |
||
174 | |||
175 | 1 | private function buildObjectTree(\DOMElement $element, ElementInterface $obj) |
|
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.