1 | <?php |
||
18 | class Indexable implements IndexableInterface |
||
19 | { |
||
20 | /** |
||
21 | * An array of raw configured callbacks for all types. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $callbacks = []; |
||
26 | |||
27 | /** |
||
28 | * An instance of ExpressionLanguage. |
||
29 | * |
||
30 | * @var ExpressionLanguage |
||
31 | */ |
||
32 | private $expressionLanguage; |
||
33 | |||
34 | /** |
||
35 | * An array of initialised callbacks. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private $initialisedCallbacks = []; |
||
40 | |||
41 | 15 | public function __construct(array $callbacks) |
|
45 | |||
46 | /** |
||
47 | * Return whether the object is indexable with respect to the callback. |
||
48 | * |
||
49 | * @param string $indexName |
||
50 | * @param string $typeName |
||
51 | * @param mixed $object |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | 15 | public function isObjectIndexable($indexName, $typeName, $object) |
|
74 | |||
75 | /** |
||
76 | * Builds and initialises a callback. |
||
77 | * |
||
78 | * @param string $type |
||
79 | * @param object $object |
||
80 | * |
||
81 | * @return callable|string|ExpressionLanguage|null |
||
82 | */ |
||
83 | 15 | private function buildCallback($type, $object) |
|
101 | |||
102 | /** |
||
103 | * Processes a string expression into an Expression. |
||
104 | * |
||
105 | * @param string $type |
||
106 | * @param mixed $object |
||
107 | * @param string $callback |
||
108 | * |
||
109 | * @return Expression |
||
110 | */ |
||
111 | 8 | private function buildExpressionCallback($type, $object, $callback) |
|
112 | { |
||
113 | 8 | $expression = $this->getExpressionLanguage(); |
|
114 | 8 | if (!$expression) { |
|
115 | throw new \RuntimeException('Unable to process an expression without the ExpressionLanguage component.'); |
||
116 | } |
||
117 | |||
118 | try { |
||
119 | 8 | $callback = new Expression($callback); |
|
120 | 8 | $expression->compile($callback, [ |
|
121 | 8 | 'object', $this->getExpressionVar($object), |
|
122 | ]); |
||
123 | |||
124 | 6 | return $callback; |
|
125 | 2 | } catch (SyntaxError $e) { |
|
126 | 2 | throw new \InvalidArgumentException(sprintf( |
|
127 | 2 | 'Callback for type "%s" is an invalid expression', |
|
128 | $type |
||
129 | 2 | ), $e->getCode(), $e); |
|
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Retreives a cached callback, or creates a new callback if one is not found. |
||
135 | * |
||
136 | * @param string $type |
||
137 | * @param object $object |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | 15 | private function getCallback($type, $object) |
|
149 | |||
150 | /** |
||
151 | * Returns the ExpressionLanguage class if it is available. |
||
152 | * |
||
153 | * @return ExpressionLanguage|null |
||
154 | */ |
||
155 | 8 | private function getExpressionLanguage() |
|
163 | |||
164 | /** |
||
165 | * Returns the variable name to be used to access the object when using the ExpressionLanguage |
||
166 | * component to parse and evaluate an expression. |
||
167 | * |
||
168 | * @param mixed $object |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 8 | private function getExpressionVar($object = null) |
|
182 | } |
||
183 |