1 | <?php |
||
31 | class Indexable implements IndexableInterface, ContainerAwareInterface |
||
32 | { |
||
33 | use ContainerAwareTrait; |
||
34 | |||
35 | /** |
||
36 | * An array of raw configured callbacks for all types. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $callbacks = []; |
||
41 | |||
42 | /** |
||
43 | * An instance of ExpressionLanguage. |
||
44 | * |
||
45 | * @var ExpressionLanguage |
||
46 | */ |
||
47 | private $expressionLanguage; |
||
48 | |||
49 | /** |
||
50 | * An array of initialised callbacks. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $initialisedCallbacks = []; |
||
55 | |||
56 | /** |
||
57 | * PropertyAccessor instance. |
||
58 | * |
||
59 | * @var PropertyAccessorInterface |
||
60 | */ |
||
61 | private $propertyAccessor; |
||
62 | |||
63 | /** |
||
64 | * @param array $callbacks |
||
65 | */ |
||
66 | 25 | public function __construct(array $callbacks) |
|
71 | |||
72 | /** |
||
73 | * Return whether the object is indexable with respect to the callback. |
||
74 | * |
||
75 | * @param string $indexName |
||
76 | * @param string $typeName |
||
77 | * @param mixed $object |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | 17 | public function isObjectIndexable($indexName, $typeName, $object) |
|
82 | { |
||
83 | 17 | $type = sprintf('%s/%s', $indexName, $typeName); |
|
84 | 17 | $callback = $this->getCallback($type, $object); |
|
85 | 12 | if (!$callback) { |
|
86 | 1 | return true; |
|
87 | } |
||
88 | |||
89 | 11 | if ($callback instanceof Expression) { |
|
90 | 6 | return (bool) $this->getExpressionLanguage()->evaluate($callback, [ |
|
91 | 6 | 'object' => $object, |
|
92 | 6 | $this->getExpressionVar($object) => $object, |
|
93 | 6 | ]); |
|
94 | } |
||
95 | |||
96 | 6 | return is_string($callback) |
|
97 | 6 | ? call_user_func([$object, $callback]) |
|
98 | 6 | : call_user_func($callback, $object); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Builds and initialises a callback. |
||
103 | * |
||
104 | * @param string $type |
||
105 | * @param object $object |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | 17 | private function buildCallback($type, $object) |
|
131 | |||
132 | /** |
||
133 | * Processes a string expression into an Expression. |
||
134 | * |
||
135 | * @param string $type |
||
136 | * @param mixed $object |
||
137 | * @param string $callback |
||
138 | * |
||
139 | * @return Expression |
||
140 | */ |
||
141 | 8 | private function buildExpressionCallback($type, $object, $callback) |
|
142 | { |
||
143 | 8 | $expression = $this->getExpressionLanguage(); |
|
144 | 8 | if (!$expression) { |
|
145 | throw new \RuntimeException('Unable to process an expression without the ExpressionLanguage component.'); |
||
146 | } |
||
147 | |||
148 | try { |
||
149 | 8 | $callback = new Expression($callback); |
|
150 | 8 | $expression->compile($callback, [ |
|
151 | 8 | 'object', $this->getExpressionVar($object), |
|
152 | 8 | ]); |
|
153 | |||
154 | 6 | return $callback; |
|
155 | 2 | } catch (SyntaxError $e) { |
|
156 | 2 | throw new \InvalidArgumentException(sprintf( |
|
157 | 2 | 'Callback for type "%s" is an invalid expression', |
|
158 | $type |
||
159 | 2 | ), $e->getCode(), $e); |
|
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Retreives a cached callback, or creates a new callback if one is not found. |
||
165 | * |
||
166 | * @param string $type |
||
167 | * @param object $object |
||
168 | * |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 17 | private function getCallback($type, $object) |
|
172 | { |
||
173 | 17 | if (!array_key_exists($type, $this->initialisedCallbacks)) { |
|
174 | 17 | $this->initialisedCallbacks[$type] = $this->buildCallback($type, $object); |
|
175 | 12 | } |
|
176 | |||
177 | 12 | return $this->initialisedCallbacks[$type]; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns the ExpressionLanguage class if it is available. |
||
182 | * |
||
183 | * @return ExpressionLanguage|null |
||
184 | */ |
||
185 | 8 | private function getExpressionLanguage() |
|
186 | { |
||
187 | 8 | if (null === $this->expressionLanguage) { |
|
188 | 8 | $this->expressionLanguage = new ExpressionLanguage(); |
|
189 | 8 | } |
|
190 | |||
191 | 8 | return $this->expressionLanguage; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Returns the variable name to be used to access the object when using the ExpressionLanguage |
||
196 | * component to parse and evaluate an expression. |
||
197 | * |
||
198 | * @param mixed $object |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 8 | private function getExpressionVar($object = null) |
|
212 | |||
213 | /** |
||
214 | * Processes an array into a callback. Replaces the first element with a service if |
||
215 | * it begins with an @. |
||
216 | * |
||
217 | * @param string $type |
||
218 | * @param array $callback |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | 4 | private function processArrayToCallback($type, array $callback) |
|
246 | } |
||
247 |