1 | <?php |
||
10 | class Query extends QueryHandler implements QueryInterface |
||
11 | { |
||
12 | /** @var string Class name for interacting with database */ |
||
13 | protected $class_name; |
||
14 | |||
15 | /** @var self[] Collection of query parameters objects */ |
||
16 | protected $parameters = array(); |
||
17 | |||
18 | /** @var array Collection of entity field names for sorting order */ |
||
19 | protected $sorting = array(); |
||
20 | |||
21 | /** @var array Collection of entity field names for grouping query results */ |
||
22 | protected $grouping = array(); |
||
23 | |||
24 | /** @var array Collection of query results limitations */ |
||
25 | protected $limitation = array(); |
||
26 | |||
27 | /** @var Condition Query base entity condition group */ |
||
28 | protected $own_condition; |
||
29 | |||
30 | /** @var Condition Query entity condition group */ |
||
31 | protected $cConditionGroup; |
||
32 | |||
33 | /** |
||
34 | * Query constructor. |
||
35 | * @param string|null $entity Entity identifier |
||
36 | * @throws EntityNotFound |
||
37 | */ |
||
38 | public function __construct($entity = null) |
||
43 | |||
44 | /** |
||
45 | * Reset all query parameters |
||
46 | * @return self Chaining |
||
47 | */ |
||
48 | public function flush() |
||
64 | |||
65 | /** |
||
66 | * Perform database request and get collection of database record objects |
||
67 | * @see \samson\activerecord\Query::execute() |
||
68 | * @param mixed $return External variable to store query results |
||
69 | * @return mixed If no arguments passed returns query results collection, otherwise query success status |
||
70 | */ |
||
71 | public function exec(& $return = null) |
||
76 | |||
77 | /** |
||
78 | * Perform database request and get first record from results collection |
||
79 | * @see \samson\activerecord\Query::execute() |
||
80 | * @param mixed $return External variable to store query results |
||
81 | * @return mixed If no arguments passed returns query results first database record object, |
||
82 | * otherwise query success status |
||
83 | */ |
||
84 | public function first(& $return = null) |
||
89 | |||
90 | /** |
||
91 | * Perform database request and get array of record field values |
||
92 | * @see \samson\activerecord\Query::execute() |
||
93 | * @param string $fieldName Record field name to get value from |
||
94 | * @param string $return External variable to store query results |
||
95 | * @return Ambigous <boolean, NULL, mixed> |
||
96 | */ |
||
97 | public function fields($fieldName, & $return = null) |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Perform database request and return different results depending on function arguments. |
||
118 | * @see \samson\activerecord\Record |
||
119 | * @param array $result External variable to store dabatase request results collection |
||
120 | * @param integer|bool $rType Amount of arguments passed to parent function |
||
121 | * @param integer $limit Quantity of records to return |
||
122 | * @param callable $handler External callable handler for results modification |
||
123 | * @param array $handlerArgs External callable handler arguments |
||
124 | * @return boolean/array Boolean if $r_type > 0, otherwise array of request results |
||
125 | */ |
||
126 | protected function &execute( |
||
177 | |||
178 | /** |
||
179 | * Set query entity to work with. |
||
180 | * |
||
181 | * @param string $entity Entity identifier |
||
182 | * @return Query|string Chaining or current entity identifier if nothing is passed |
||
183 | * @throws EntityNotFound |
||
184 | */ |
||
185 | public function entity($entity = null) |
||
206 | |||
207 | /** |
||
208 | * Get correct query condition depending on entity field name. |
||
209 | * If base entity has field with this name - use base entity condition |
||
210 | * group, otherwise default condition group. |
||
211 | * |
||
212 | * @param string $fieldName Entity field name |
||
213 | * @return Condition Correct query condition group |
||
214 | */ |
||
215 | protected function &getConditionGroup($fieldName) |
||
224 | |||
225 | /** |
||
226 | * Add condition to current query. |
||
227 | * |
||
228 | * @param string|Condition|Argument $fieldName Entity field name |
||
229 | * @param string $fieldValue Value |
||
230 | * @param string $relation Relation between field name and its value |
||
231 | * @return self Chaining |
||
232 | */ |
||
233 | public function where($fieldName, $fieldValue = null, $relation = '=') |
||
264 | |||
265 | /** |
||
266 | * Join entity to query. |
||
267 | * |
||
268 | * @param string $entityName Entity identifier |
||
269 | * @return self Chaining |
||
270 | */ |
||
271 | public function join($entityName) |
||
278 | |||
279 | /** |
||
280 | * Add query result grouping. |
||
281 | * |
||
282 | * @param string $fieldName Entity field identifier for grouping |
||
283 | * @return self Chaining |
||
284 | */ |
||
285 | public function groupBy($fieldName) |
||
292 | |||
293 | /** |
||
294 | * Add query result quantity limitation. |
||
295 | * |
||
296 | * @param int $offset Resulting offset |
||
297 | * @param null|int $quantity Amount of RecordInterface object to return |
||
298 | * @return self Chaining |
||
299 | */ |
||
300 | public function limit($offset, $quantity = null) |
||
307 | |||
308 | /** |
||
309 | * Add query result sorting. |
||
310 | * |
||
311 | * @param string $fieldName Entity field identifier for worting |
||
312 | * @param string $order Sorting order |
||
313 | * @return self Chaining |
||
314 | */ |
||
315 | public function orderBy($fieldName, $order = 'ASC') |
||
322 | |||
323 | /** |
||
324 | * Add condition by primary field |
||
325 | * |
||
326 | * @param string $value Primary field value |
||
327 | * @return self Chaining |
||
328 | */ |
||
329 | public function id($value) |
||
338 | |||
339 | /** |
||
340 | * Add condition to current query. |
||
341 | * |
||
342 | * @param string|Condition|Argument $fieldName Entity field name |
||
343 | * @param string $fieldValue Value |
||
344 | * @param string $relation Relation between field name and its value |
||
345 | * @deprecated @see self::where() |
||
346 | * @return self Chaining |
||
347 | */ |
||
348 | public function cond($fieldName, $fieldValue = null, $relation = '=') |
||
352 | } |
||
353 |
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.