1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg; |
3
|
|
|
|
4
|
|
|
use Doctrine\Instantiator\Instantiator; |
5
|
|
|
use nstdio\svg\attributes\Transformable; |
6
|
|
|
use nstdio\svg\container\ContainerInterface; |
7
|
|
|
use nstdio\svg\container\SVG; |
8
|
|
|
use nstdio\svg\traits\ChildTrait; |
9
|
|
|
use nstdio\svg\traits\ElementTrait; |
10
|
|
|
use nstdio\svg\util\Identifier; |
11
|
|
|
use nstdio\svg\util\Inflector; |
12
|
|
|
use nstdio\svg\util\KeyValueWriter; |
13
|
|
|
use nstdio\svg\util\Transform; |
14
|
|
|
use nstdio\svg\util\TransformInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class SVGElement |
18
|
|
|
* |
19
|
|
|
* @property string $id |
20
|
|
|
* @property string $fill The fill color. |
21
|
|
|
* @property float $height The height attribute of element. |
22
|
|
|
* @property float $width The width attribute of element. |
23
|
|
|
* |
24
|
|
|
* @package nstdio\svg |
25
|
|
|
* @author Edgar Asatryan <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
abstract class SVGElement implements ContainerInterface, ElementFactoryInterface |
28
|
|
|
{ |
29
|
|
|
use ElementTrait, ChildTrait; |
30
|
|
|
|
31
|
|
|
private static $notConvertable = ['patternContentUnits', 'patternTransform', 'patternUnits', 'diffuseConstant', 'pointsAtX', 'pointsAtY', 'pointsAtZ', 'limitingConeAngle', 'tableValues', 'filterUnits', 'gradientUnits', 'viewBox', 'repeatCount', 'attributeName', 'attributeType', 'stdDeviation']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var XMLDocumentInterface | ElementInterface | ElementFactoryInterface | ContainerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $root; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var XMLDocumentInterface | ElementInterface | ElementFactoryInterface | ContainerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $element; |
42
|
|
|
|
43
|
|
|
public function __construct(ElementInterface $parent) |
44
|
|
|
{ |
45
|
|
|
$this->child = new ElementStorage(); |
46
|
|
|
$this->root = $parent; |
|
|
|
|
47
|
|
|
$this->element = $this->createElement($this->getName()); |
48
|
|
|
$this->root->append($this); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function createElement($name, $value = null, $attributes = []) |
52
|
|
|
{ |
53
|
|
|
return $this->root->createElement($name, $value, $attributes); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
abstract public function getName(); |
57
|
|
|
|
58
|
|
|
final public function getRoot() |
59
|
|
|
{ |
60
|
|
|
return $this->root; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
final public function getElement() |
64
|
|
|
{ |
65
|
|
|
return $this->element; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function allAttributes(array $except = []) |
69
|
|
|
{ |
70
|
|
|
return $this->element->attributes($except); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __get($name) |
74
|
|
|
{ |
75
|
|
|
if ($name === 'filterUrl' || $name === 'fillUrl') { |
76
|
|
|
return $this->getIdFromUrl($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$name = $this->convertAttributeName($name); |
80
|
|
|
$value = $this->element->getAttribute($name); |
81
|
|
|
if ($value === '') { |
82
|
|
|
$value = $this->getXLinkAttribute($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $value === '' ? null : $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __set($name, $value) |
89
|
|
|
{ |
90
|
|
|
if ($name === 'id' && $value === null) { |
91
|
|
|
$this->element->setAttribute($name, Identifier::random('__' . $this->getName(), 5)); |
92
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($value === null || $value === false || $value === '') { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($name === 'filterUrl' || $name === 'fillUrl') { |
101
|
|
|
$this->handleUrlPostfixAttribute($name, $value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$name = $this->convertAttributeName($name); |
105
|
|
|
$this->element->setAttribute($name, $value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getXLinkAttribute($name) |
109
|
|
|
{ |
110
|
|
|
return $this->element->getAttributeNS('xlink', $name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $name |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
private function convertAttributeName($name) |
119
|
|
|
{ |
120
|
|
|
return !in_array($name, self::$notConvertable) ? Inflector::camel2dash($name) : $name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function apply(array $assoc) |
127
|
|
|
{ |
128
|
|
|
$filtered = []; |
129
|
|
|
foreach ($assoc as $attribute => $value) { |
130
|
|
|
$filtered[$this->convertAttributeName($attribute)] = $value; |
131
|
|
|
} |
132
|
|
|
KeyValueWriter::apply($this->element, $filtered); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function setAttribute($name, $value, $xLink = false) |
138
|
|
|
{ |
139
|
|
|
if ($xLink === true) { |
140
|
|
|
$this->element->setAttributeNS('xlink', $name, $value); |
141
|
|
|
} else { |
142
|
|
|
$this->element->setAttribute($name, $value); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function selfRemove() |
147
|
|
|
{ |
148
|
|
|
$this->getRoot()->removeChild($this); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function getSVG() |
152
|
|
|
{ |
153
|
|
|
if ($this->root instanceof SVG) { |
154
|
|
|
return $this->root; |
155
|
|
|
} |
156
|
|
|
$element = $this->root; |
157
|
|
|
|
158
|
|
|
do { |
159
|
|
|
$element = $element->getRoot(); |
160
|
|
|
} while (!($element instanceof SVG)); |
161
|
|
|
|
162
|
|
|
return $element; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
168
|
|
|
public function copy(array $apply = [], array $ignore = [], ContainerInterface $parent = null) |
169
|
|
|
{ |
170
|
|
|
/** @var SVGElement $instance */ |
171
|
|
|
$instance = (new Instantiator())->instantiate(get_class($this)); |
172
|
|
|
$instance->root = $parent === null ? $this->getRoot() : $parent; |
|
|
|
|
173
|
|
|
$instance->child = new ElementStorage(); |
174
|
|
|
$instance->element = $this->createElement($this->getName()); |
175
|
|
|
$instance->id = null; |
176
|
|
|
|
177
|
|
|
if ($instance instanceof TransformInterface && $this instanceof Transformable) { |
178
|
|
|
$instance->transformImpl = Transform::newInstance($this->getTransformAttribute()); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
$ignore[] = 'id'; |
181
|
|
|
$apply = array_merge($this->allAttributes($ignore), $apply); |
182
|
|
|
$instance->apply($apply); |
183
|
|
|
$parent === null ? $this->root->append($instance) : $parent->append($instance); |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $instance; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param $attribute |
190
|
|
|
* @param $value |
191
|
|
|
*/ |
192
|
|
|
private function handleUrlPostfixAttribute(&$attribute, &$value) |
193
|
|
|
{ |
194
|
|
|
$attribute = substr($attribute, 0, strrpos($attribute, 'U')); |
195
|
|
|
if (strpos($value, "url(#") !== 0) { |
196
|
|
|
$value = "url(#" . $value . ")"; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function getIdFromUrl($attribute) |
201
|
|
|
{ |
202
|
|
|
$attribute = substr($attribute, 0, strrpos($attribute, 'U')); |
203
|
|
|
return str_replace(['url(#', ')'], '', $this->element->getAttribute($attribute)); |
204
|
|
|
} |
205
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..