1 | <?php |
||
13 | class LaravelExpressionProvider implements IExpressionProvider |
||
14 | { |
||
15 | const ADD = '+'; |
||
16 | const CLOSE_BRACKET = ')'; |
||
17 | const COMMA = ','; |
||
18 | const DIVIDE = '/'; |
||
19 | const SUBTRACT = '-'; |
||
20 | const EQUAL = '=='; |
||
21 | const GREATER_THAN = '>'; |
||
22 | const GREATER_THAN_OR_EQUAL = '>='; |
||
23 | const LESS_THAN = '<'; |
||
24 | const LESS_THAN_OR_EQUAL = '<='; |
||
25 | const LOGICAL_AND = '&&'; |
||
26 | const LOGICAL_NOT = '!'; |
||
27 | const LOGICAL_OR = '||'; |
||
28 | const MEMBER_ACCESS = '->'; |
||
29 | const MODULO = '%'; |
||
30 | const MULTIPLY = '*'; |
||
31 | const NEGATE = '-'; |
||
32 | const NOT_EQUAL = '!='; |
||
33 | const OPEN_BRACKET = '('; |
||
34 | const TYPE_NAMESPACE = 'POData\\Providers\\Metadata\\Type\\'; |
||
35 | |||
36 | private $functionDescriptionParsers; |
||
37 | |||
38 | /** |
||
39 | * The name of iterator. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $iteratorName; |
||
44 | /** |
||
45 | * The type of the resource pointed by the resource path segment. |
||
46 | * |
||
47 | * @var ResourceType |
||
48 | */ |
||
49 | 52 | private $resourceType; |
|
50 | /** |
||
51 | 52 | */ |
|
52 | public function __construct() |
||
131 | 1 | /** |
|
132 | 6 | * Get the name of the iterator. |
|
133 | 1 | * |
|
134 | 5 | * @return string |
|
135 | 1 | */ |
|
136 | 4 | public function getIteratorName() |
|
140 | 2 | ||
141 | 1 | /** |
|
142 | 1 | * Get the resource type |
|
143 | 1 | * |
|
144 | 1 | * @return object|null |
|
145 | */ |
||
146 | public function getResourceType() |
||
150 | |||
151 | /** |
||
152 | * call-back for setting the resource type. |
||
153 | * |
||
154 | 3 | * @param ResourceType $resourceType The resource type on which the filter |
|
155 | * is going to be applied |
||
156 | */ |
||
157 | 3 | public function setResourceType(ResourceType $resourceType) |
|
162 | 1 | /** |
|
163 | 1 | * Call-back for logical expression. |
|
164 | * |
||
165 | * @param ExpressionType $expressionType The type of logical expression |
||
166 | * @param string $left The left expression |
||
167 | * @param string $right The left expression |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function onLogicalExpression($expressionType, $left, $right) |
||
182 | /** |
||
183 | * Call-back for arithmetic expression. |
||
184 | * |
||
185 | * @param ExpressionType $expressionType The type of arithmetic expression |
||
186 | * @param string $left The left expression |
||
187 | * @param string $right The left expression |
||
188 | * |
||
189 | 1 | * @return string |
|
190 | */ |
||
191 | 1 | public function onArithmeticExpression($expressionType, $left, $right) |
|
208 | /** |
||
209 | 28 | * Call-back for relational expression. |
|
210 | * |
||
211 | 28 | * @param ExpressionType $expressionType The type of relation expression |
|
212 | 1 | * @param string $left The left expression |
|
213 | * @param string $right The left expression |
||
214 | 27 | * |
|
215 | 27 | * @return string |
|
216 | 1 | */ |
|
217 | 26 | public function onRelationalExpression($expressionType, $left, $right) |
|
236 | 16 | /** |
|
237 | 1 | * Call-back for unary expression. |
|
238 | 15 | * |
|
239 | 1 | * @param ExpressionType $expressionType The type of unary expression |
|
240 | 14 | * @param string $child The child expression |
|
241 | 1 | * |
|
242 | 13 | * @return string |
|
243 | 1 | */ |
|
244 | 12 | public function onUnaryExpression($expressionType, $child) |
|
255 | 1 | /** |
|
256 | 6 | * Call-back for constant expression. |
|
257 | 1 | * |
|
258 | 5 | * @param IType $type The type of constant |
|
259 | 1 | * @param mixed $value The value of the constant |
|
260 | 4 | * |
|
261 | 1 | * @return string |
|
262 | 3 | */ |
|
263 | 1 | public function onConstantExpression(IType $type, $value) |
|
272 | /** |
||
273 | * Call-back for property access expression. |
||
274 | * |
||
275 | * @param PropertyAccessExpression $expression The property access expression |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 13 | public function onPropertyAccessExpression($expression) |
|
291 | /** |
||
292 | 2 | * Call-back for function call expression. |
|
293 | * |
||
294 | 2 | * @param \POData\UriProcessor\QueryProcessor\FunctionDescription $functionDescription Description of the function |
|
295 | * @param array<string> $params Parameters to the function |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | public function onFunctionCallExpression($functionDescription, $params) |
||
309 | /** |
||
310 | * To format binary expression. |
||
311 | * |
||
312 | * @param string $operator The binary operator |
||
313 | * @param string $left The left operand |
||
314 | * @param string $right The right operand |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | private function _prepareBinaryExpression($operator, $left, $right) |
||
323 | /** |
||
324 | * To format unary expression. |
||
325 | * |
||
326 | * @param string $operator The unary operator |
||
327 | * @param string $child The operand |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | private function _prepareUnaryExpression($operator, $child) |
||
335 | } |
||
336 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.