1 | <?php |
||
10 | abstract class TransitionHelper extends \Twig_Extension |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $options = array( |
||
16 | 'state' => array( |
||
17 | 'translation' => null, |
||
18 | 'default_color' => 'black', |
||
19 | 'negative_colors' => array('red'), |
||
20 | 'positive_colors' => array('green'), |
||
21 | ), |
||
22 | 'transition' => array( |
||
23 | 'translation' => null, |
||
24 | 'default_color' => 'black', |
||
25 | 'negative_colors' => array('red'), |
||
26 | 'positive_colors' => array('green'), |
||
27 | ), |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $colors = array(); |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $negativeColors = array(); |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $positiveColors = array(); |
||
44 | |||
45 | /** |
||
46 | * @var Factory |
||
47 | */ |
||
48 | protected $factory; |
||
49 | |||
50 | /** |
||
51 | * @var TranslatorInterface |
||
52 | */ |
||
53 | protected $translator; |
||
54 | |||
55 | /** |
||
56 | * @param array $options |
||
57 | */ |
||
58 | public function __construct(array $options = array()) |
||
59 | { |
||
60 | $this->options = array_replace_recursive($this->options, $options); |
||
61 | |||
62 | foreach ($this->options as $type => $options) { |
||
63 | if (!empty($options['colors'])) { |
||
64 | foreach ($options['colors'] as $color => $states) { |
||
65 | if (!is_array($states)) { |
||
66 | $states = (array) $states; |
||
67 | } |
||
68 | |||
69 | foreach ($states as $state) { |
||
70 | $this->colors[$type][$state] = $color; |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if (!empty($options['negative_colors'])) { |
||
76 | $this->negativeColors[$type] = $options['negative_colors']; |
||
77 | } |
||
78 | |||
79 | if (!empty($options['positive_colors'])) { |
||
80 | $this->positiveColors[$type] = $options['positive_colors']; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function getFunctions() |
||
89 | { |
||
90 | return array( |
||
91 | new \Twig_SimpleFunction('ts_is', array($this, 'isState')), |
||
|
|||
92 | new \Twig_SimpleFunction('ts_state', array($this, 'getState')), |
||
93 | new \Twig_SimpleFunction('ts_color', array($this, 'getStateColor')), |
||
94 | new \Twig_SimpleFunction('ts_color_t', array($this, 'getTransitionColor')), |
||
95 | new \Twig_SimpleFunction('ts_transitions', array($this, 'getPosibleTransitions')), |
||
96 | new \Twig_SimpleFunction('ts_trans_s', array($this, 'getTranslationState')), |
||
97 | new \Twig_SimpleFunction('ts_trans_t', array($this, 'getTranslationTransition')), |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param Factory $factory |
||
103 | */ |
||
104 | public function setFactory(Factory $factory) |
||
108 | |||
109 | /** |
||
110 | * @param TranslatorInterface $translator |
||
111 | */ |
||
112 | public function setTranslator(TranslatorInterface $translator) |
||
116 | |||
117 | /** |
||
118 | * @param string|StatableInterface $key |
||
119 | * @param string $type |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getTranslation($key, $type = 'state') |
||
123 | { |
||
124 | if (strtolower($type) === 'state') { |
||
125 | return $this->translator->trans($this->getState($key)); |
||
126 | } |
||
127 | |||
128 | return $this->translator->trans($this->getTransition($key)); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param string|StatableInterface $keyOrObject |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getTranslationState($keyOrObject) |
||
140 | |||
141 | /** |
||
142 | * @param $key |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getTranslationTransition($key) |
||
150 | |||
151 | /** |
||
152 | * Get transition translator key. |
||
153 | * |
||
154 | * @param string $transition |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getTransition($transition) |
||
162 | |||
163 | /** |
||
164 | * Get transition color. |
||
165 | * |
||
166 | * @param string $transition |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getTransitionColor($transition) |
||
171 | { |
||
172 | return empty($this->colors['transition'][$transition]) |
||
173 | ? $this->options['transition']['default_color'] |
||
174 | : $this->colors['transition'][$transition] |
||
175 | ; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Check color is negative? |
||
180 | * |
||
181 | * @param $color |
||
182 | * @param $type |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function isNegativeColor($color, $type) |
||
187 | { |
||
188 | if (empty($this->negativeColors[$type])) { |
||
189 | return false; |
||
190 | } |
||
191 | |||
192 | return in_array($color, $this->negativeColors[$type]); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Check color is positive? |
||
197 | * |
||
198 | * @param $color |
||
199 | * @param $type |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function isPositiveColor($color, $type) |
||
204 | { |
||
205 | if (empty($this->positiveColors[$type])) { |
||
206 | return false; |
||
207 | } |
||
208 | |||
209 | return in_array($color, $this->positiveColors[$type]); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get state translator key. |
||
214 | * |
||
215 | * @param string|object $state |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getState($state) |
||
220 | { |
||
221 | if ($state instanceof StatableInterface) { |
||
222 | $state = $state->getState(); |
||
223 | } |
||
224 | |||
225 | return $this->options['state']['translation'].'.'.$state; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get state color. |
||
230 | * |
||
231 | * @param string|object $state |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getStateColor($state) |
||
236 | { |
||
237 | if ($state instanceof StatableInterface) { |
||
238 | $state = $state->getState(); |
||
239 | } |
||
240 | |||
241 | return empty($this->colors['state'][$state]) |
||
242 | ? $this->options['state']['default_color'] |
||
243 | : $this->colors['state'][$state] |
||
244 | ; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param string $class Class name |
||
249 | * @param string $name Constant name |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | protected function getConstant($class, $name) |
||
254 | { |
||
255 | $cls = new \ReflectionClass($class); |
||
256 | |||
257 | return $cls->getConstant($name); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param $class |
||
262 | * @param string $prefix |
||
263 | * |
||
264 | * @return array |
||
265 | * @deprecated implement StatableInterface in client class. |
||
266 | */ |
||
267 | protected function getTransitions($class, $prefix = 'TS') |
||
268 | { |
||
269 | $cls = new \ReflectionClass($class); |
||
270 | |||
271 | $transitions = array(); |
||
272 | foreach ($cls->getConstants() as $const => $value) { |
||
273 | if (preg_match(sprintf('/^%s_.*/', $prefix), $const)) { |
||
274 | $transitions[$const] = $value; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | return $transitions; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Get all of posible transitions. |
||
283 | * |
||
284 | * @param StatableInterface $object |
||
285 | * @param string $objectIdentifier |
||
286 | * |
||
287 | * @return array|null |
||
288 | * |
||
289 | * @throws \SM\SMException |
||
290 | */ |
||
291 | public function getPosibleTransitions(StatableInterface $object = null, $objectIdentifier = 'id') |
||
292 | { |
||
293 | if (empty($object)) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | $sm = $this->factory->get($object, $object->getStateGraph()); |
||
298 | |||
299 | if (empty($sm)) { |
||
300 | return; |
||
301 | } |
||
302 | |||
303 | $tasks = array(); |
||
304 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
305 | |||
306 | foreach ($object->getStateTransitions() as $transition) { |
||
307 | if ($sm->can($transition)) { |
||
308 | |||
309 | $color = $this->getTransitionColor($transition); |
||
310 | $tasks[] = array( |
||
311 | 'id' => $accessor->getValue($object, $objectIdentifier), |
||
312 | 'name' => $transition, |
||
313 | 'color' => $color, |
||
314 | 'graph' => $object->getStateGraph(), |
||
315 | 'label' => $this->getTransition($transition), |
||
316 | 'negative' => $this->isNegativeColor($color, 'transition'), |
||
317 | 'positive' => $this->isPositiveColor($color, 'transition'), |
||
318 | ); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | return empty($tasks) ? null : $tasks; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Compare state. |
||
327 | * |
||
328 | * @param array|string $stateCompare State(s) to compare |
||
329 | * @param StatableInterface $object |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function isState($stateCompare, StatableInterface $object) |
||
337 | } |
||
338 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.