1 | <?php |
||
22 | class Indexable implements IndexableInterface, ContainerAwareInterface |
||
23 | { |
||
24 | use ContainerAwareTrait; |
||
25 | |||
26 | /** |
||
27 | * An array of raw configured callbacks for all types. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $callbacks = array(); |
||
32 | |||
33 | /** |
||
34 | * An instance of ExpressionLanguage. |
||
35 | * |
||
36 | * @var ExpressionLanguage |
||
37 | */ |
||
38 | private $expressionLanguage; |
||
39 | |||
40 | /** |
||
41 | * An array of initialised callbacks. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $initialisedCallbacks = array(); |
||
46 | |||
47 | /** |
||
48 | * PropertyAccessor instance. |
||
49 | * |
||
50 | * @var PropertyAccessorInterface |
||
51 | */ |
||
52 | private $propertyAccessor; |
||
53 | |||
54 | /** |
||
55 | * @param array $callbacks |
||
56 | */ |
||
57 | 19 | public function __construct(array $callbacks) |
|
62 | |||
63 | /** |
||
64 | * Return whether the object is indexable with respect to the callback. |
||
65 | * |
||
66 | * @param string $indexName |
||
67 | * @param string $typeName |
||
68 | * @param mixed $object |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | 16 | public function isObjectIndexable($indexName, $typeName, $object) |
|
73 | { |
||
74 | 16 | $type = sprintf('%s/%s', $indexName, $typeName); |
|
75 | 16 | $callback = $this->getCallback($type, $object); |
|
76 | 11 | if (!$callback) { |
|
77 | 1 | return true; |
|
78 | } |
||
79 | |||
80 | 10 | if ($callback instanceof Expression) { |
|
81 | 5 | return (bool) $this->getExpressionLanguage()->evaluate($callback, array( |
|
82 | 5 | 'object' => $object, |
|
83 | 5 | $this->getExpressionVar($object) => $object, |
|
84 | 5 | )); |
|
85 | } |
||
86 | |||
87 | 5 | return is_string($callback) |
|
88 | 5 | ? call_user_func(array($object, $callback)) |
|
89 | 5 | : call_user_func($callback, $object); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Builds and initialises a callback. |
||
94 | * |
||
95 | * @param string $type |
||
96 | * @param object $object |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 16 | private function buildCallback($type, $object) |
|
122 | |||
123 | /** |
||
124 | * Processes a string expression into an Expression. |
||
125 | * |
||
126 | * @param string $type |
||
127 | * @param mixed $object |
||
128 | * @param string $callback |
||
129 | * |
||
130 | * @return Expression |
||
131 | */ |
||
132 | 7 | private function buildExpressionCallback($type, $object, $callback) |
|
133 | { |
||
134 | 7 | $expression = $this->getExpressionLanguage(); |
|
135 | 7 | if (!$expression) { |
|
136 | throw new \RuntimeException('Unable to process an expression without the ExpressionLanguage component.'); |
||
137 | } |
||
138 | |||
139 | try { |
||
140 | 7 | $callback = new Expression($callback); |
|
141 | 7 | $expression->compile($callback, array( |
|
142 | 7 | 'object', $this->getExpressionVar($object) |
|
143 | 7 | )); |
|
144 | |||
145 | 5 | return $callback; |
|
146 | 2 | } catch (SyntaxError $e) { |
|
147 | 2 | throw new \InvalidArgumentException(sprintf( |
|
148 | 2 | 'Callback for type "%s" is an invalid expression', |
|
149 | $type |
||
150 | 2 | ), $e->getCode(), $e); |
|
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Retreives a cached callback, or creates a new callback if one is not found. |
||
156 | * |
||
157 | * @param string $type |
||
158 | * @param object $object |
||
159 | * |
||
160 | * @return mixed |
||
161 | */ |
||
162 | 16 | private function getCallback($type, $object) |
|
163 | { |
||
164 | 16 | if (!array_key_exists($type, $this->initialisedCallbacks)) { |
|
165 | 16 | $this->initialisedCallbacks[$type] = $this->buildCallback($type, $object); |
|
166 | 11 | } |
|
167 | |||
168 | 11 | return $this->initialisedCallbacks[$type]; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Returns the ExpressionLanguage class if it is available. |
||
173 | * |
||
174 | * @return ExpressionLanguage|null |
||
175 | */ |
||
176 | 7 | private function getExpressionLanguage() |
|
177 | { |
||
178 | 7 | if (null === $this->expressionLanguage) { |
|
179 | 7 | $this->expressionLanguage = new ExpressionLanguage(); |
|
180 | 7 | } |
|
181 | |||
182 | 7 | return $this->expressionLanguage; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns the variable name to be used to access the object when using the ExpressionLanguage |
||
187 | * component to parse and evaluate an expression. |
||
188 | * |
||
189 | * @param mixed $object |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 7 | private function getExpressionVar($object = null) |
|
203 | |||
204 | /** |
||
205 | * Processes an array into a callback. Replaces the first element with a service if |
||
206 | * it begins with an @. |
||
207 | * |
||
208 | * @param string $type |
||
209 | * @param array $callback |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | 3 | private function processArrayToCallback($type, array $callback) |
|
237 | } |
||
238 |