1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finite\Extension\Twig; |
4
|
|
|
|
5
|
|
|
use Finite\Context; |
6
|
|
|
use Twig\TwigFunction; |
7
|
|
|
|
8
|
5 |
|
if (!class_exists('Twig_Extension')) { |
9
|
|
|
class_alias('Twig\Extension\AbstractExtension', 'Twig_Extension'); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The Finite Twig extension. |
14
|
|
|
* |
15
|
|
|
* @author Yohan Giarelli <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class FiniteExtension extends \Twig_Extension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Context |
21
|
|
|
*/ |
22
|
|
|
protected $context; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Context $context |
26
|
|
|
*/ |
27
|
25 |
|
public function __construct(Context $context) |
28
|
|
|
{ |
29
|
25 |
|
$this->context = $context; |
30
|
25 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
25 |
|
public function getFunctions() |
36
|
|
|
{ |
37
|
25 |
|
if (class_exists('Twig_SimpleFunction')) { |
38
|
|
|
return [ |
39
|
25 |
|
new \Twig_SimpleFunction('finite_state', [$this, 'getFiniteState']), |
40
|
25 |
|
new \Twig_SimpleFunction('finite_transitions', [$this, 'getFiniteTransitions']), |
41
|
25 |
|
new \Twig_SimpleFunction('finite_properties', [$this, 'getFiniteProperties']), |
42
|
25 |
|
new \Twig_SimpleFunction('finite_has', [$this, 'hasFiniteProperty']), |
43
|
25 |
|
new \Twig_SimpleFunction('finite_can', [$this, 'canFiniteTransition']), |
44
|
15 |
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return [ |
48
|
|
|
new TwigFunction('finite_state', [$this, 'getFiniteState']), |
49
|
|
|
new TwigFunction('finite_transitions', [$this, 'getFiniteTransitions']), |
50
|
|
|
new TwigFunction('finite_properties', [$this, 'getFiniteProperties']), |
51
|
|
|
new TwigFunction('finite_has', [$this, 'hasFiniteProperty']), |
52
|
|
|
new TwigFunction('finite_can', [$this, 'canFiniteTransition']), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param object $object |
58
|
|
|
* @param string $graph |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
5 |
|
public function getFiniteState($object, $graph = 'default') |
63
|
|
|
{ |
64
|
5 |
|
return $this->context->getState($object, $graph); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param object $object |
69
|
|
|
* @param string $graph |
70
|
|
|
* @param bool $as_object |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
5 |
|
public function getFiniteTransitions($object, $graph = 'default', $as_object = false) |
|
|
|
|
75
|
|
|
{ |
76
|
5 |
|
return $this->context->getTransitions($object, $graph, $as_object); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param object $object |
81
|
|
|
* @param string $graph |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
5 |
|
public function getFiniteProperties($object, $graph = 'default') |
86
|
|
|
{ |
87
|
5 |
|
return $this->context->getProperties($object, $graph); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param object $object |
92
|
|
|
* @param string $property |
93
|
|
|
* @param string $graph |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
5 |
|
public function hasFiniteProperty($object, $property, $graph = 'default') |
98
|
|
|
{ |
99
|
5 |
|
return $this->context->hasProperty($object, $property, $graph); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param object $object |
104
|
|
|
* @param string $transition |
105
|
|
|
* @param string $graph |
106
|
|
|
* |
107
|
|
|
* @return bool|mixed |
108
|
|
|
*/ |
109
|
5 |
|
public function canFiniteTransition($object, $transition, $graph = 'default') |
|
|
|
|
110
|
|
|
{ |
111
|
5 |
|
return $this->context->getStateMachine($object, $graph)->can($transition); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
20 |
|
public function getName() |
118
|
|
|
{ |
119
|
20 |
|
return 'finite'; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.