|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoDraw\Draw\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayAccess; |
|
14
|
|
|
use WebinoDraw\Cache\DrawCache; |
|
15
|
|
|
use WebinoDraw\Dom\NodeList; |
|
16
|
|
|
use WebinoDraw\Event\DrawEvent; |
|
17
|
|
|
use WebinoDraw\Exception; |
|
18
|
|
|
use WebinoDraw\Manipulator\Manipulator; |
|
19
|
|
|
use WebinoDraw\VarTranslator\Translation; |
|
20
|
|
|
use WebinoDraw\VarTranslator\VarTranslator; |
|
21
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
|
22
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class AbstractHelper |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractHelper implements |
|
28
|
|
|
HelperInterface, |
|
29
|
|
|
EventManagerAwareInterface |
|
30
|
|
|
{ |
|
31
|
|
|
use EventManagerAwareTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $eventIdentifier = 'WebinoDraw'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var DrawCache |
|
40
|
|
|
*/ |
|
41
|
|
|
private $cache; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var DrawEvent |
|
45
|
|
|
*/ |
|
46
|
|
|
private $event; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Manipulator |
|
50
|
|
|
*/ |
|
51
|
|
|
private $manipulator; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $vars = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var VarTranslator |
|
60
|
|
|
*/ |
|
61
|
|
|
private $varTranslator; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var Translation |
|
65
|
|
|
*/ |
|
66
|
|
|
private $varTranslation; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param NodeList $nodes |
|
70
|
|
|
* @param array $spec |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __invoke(NodeList $nodes, array $spec) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->setVarTranslation(null); |
|
75
|
|
|
$this->drawNodes($nodes, $spec); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return DrawCache |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getCache() |
|
82
|
|
|
{ |
|
83
|
|
|
if (null === $this->cache) { |
|
84
|
|
|
throw new Exception\RuntimeException('Expected injected DrawCache'); |
|
85
|
|
|
} |
|
86
|
|
|
return $this->cache; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param DrawCache $cache |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setCache(DrawCache $cache) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->cache = $cache; |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return DrawEvent |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getEvent() |
|
103
|
|
|
{ |
|
104
|
|
|
if (null === $this->event) { |
|
105
|
|
|
$this->setEvent(new DrawEvent); |
|
106
|
|
|
} |
|
107
|
|
|
return $this->event; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param DrawEvent $event |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setEvent(DrawEvent $event) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->event = $event; |
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return Manipulator |
|
122
|
|
|
* @throws Exception\RuntimeException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getManipulator() |
|
125
|
|
|
{ |
|
126
|
|
|
if (null === $this->manipulator) { |
|
127
|
|
|
throw new Exception\RuntimeException('Expected injected Manipulator'); |
|
128
|
|
|
} |
|
129
|
|
|
return $this->manipulator; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param Manipulator $manipulator |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setManipulator(Manipulator $manipulator) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->manipulator = $manipulator; |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getVars() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->vars; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param array $vars |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setVars(array $vars) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->vars = $vars; |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param VarTranslator $varTranslator |
|
162
|
|
|
* @return $this |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setVarTranslator(VarTranslator $varTranslator) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->varTranslator = $varTranslator; |
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return VarTranslator |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getVarTranslator() |
|
174
|
|
|
{ |
|
175
|
|
|
if (null === $this->varTranslator) { |
|
176
|
|
|
throw new Exception\RuntimeException('Expected injected VarTranslator'); |
|
177
|
|
|
} |
|
178
|
|
|
return $this->varTranslator; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param array $triggers |
|
183
|
|
|
* @param DrawEvent $event |
|
184
|
|
|
* @return $this |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function trigger(array $triggers, DrawEvent $event) |
|
187
|
|
|
{ |
|
188
|
|
|
$events = $this->getEventManager(); |
|
189
|
|
|
foreach ($triggers as $eventName) { |
|
190
|
|
|
$events->trigger($eventName, $event); |
|
191
|
|
|
} |
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return Translation ArrayAccess |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getVarTranslation() |
|
199
|
|
|
{ |
|
200
|
|
|
if ($this->varTranslation === null) { |
|
201
|
|
|
$this->setVarTranslation( |
|
202
|
|
|
$this->getVarTranslator()->createTranslation($this->getVars())->makeVarKeys() |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
return $this->varTranslation; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param ArrayAccess|null $varTranslation |
|
210
|
|
|
* @return $this |
|
211
|
|
|
*/ |
|
212
|
|
|
public function setVarTranslation(ArrayAccess $varTranslation = null) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->varTranslation = $varTranslation; |
|
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string|array $subject |
|
220
|
|
|
* @return mixed |
|
221
|
|
|
*/ |
|
222
|
|
|
public function translate(&$subject) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->getVarTranslation()->translate($subject); |
|
225
|
|
|
return $subject; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param string $value |
|
230
|
|
|
* @param Translation $varTranslation |
|
231
|
|
|
* @param array $spec |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function translateValue($value, Translation $varTranslation, array $spec) |
|
235
|
|
|
{ |
|
236
|
|
|
return $varTranslation->translateString($value); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param NodeList $nodes |
|
241
|
|
|
* @param array $spec |
|
242
|
|
|
* @return $this |
|
243
|
|
|
*/ |
|
244
|
|
|
public function drawNodes(NodeList $nodes, array $spec) |
|
245
|
|
|
{ |
|
246
|
|
|
$this->manipulateNodes($nodes, $spec, $this->getVarTranslator()->createTranslation($this->getVars())); |
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Manipulate nodes |
|
252
|
|
|
* |
|
253
|
|
|
* @param NodeList $nodes |
|
254
|
|
|
* @param array $spec |
|
255
|
|
|
* @param ArrayAccess $translation |
|
256
|
|
|
* @return $this |
|
257
|
|
|
*/ |
|
258
|
|
|
public function manipulateNodes(NodeList $nodes, array $spec, ArrayAccess $translation) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->getManipulator()->manipulate([ |
|
261
|
|
|
'helper' => $this, |
|
262
|
|
|
'nodes' => $nodes, |
|
263
|
|
|
'spec' => $spec, |
|
264
|
|
|
'translation' => $translation, |
|
265
|
|
|
]); |
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.