1 | <?php |
||
37 | abstract class QueryLoader implements LoaderInterface |
||
38 | { |
||
39 | use ColumnsTrait; |
||
40 | |||
41 | /** |
||
42 | * Loading methods for data loaders. |
||
43 | */ |
||
44 | const INLOAD = 1; |
||
45 | const POSTLOAD = 2; |
||
46 | const JOIN = 3; |
||
47 | const LEFT_JOIN = 4; |
||
48 | |||
49 | /** |
||
50 | * Nested loaders. |
||
51 | * |
||
52 | * @var LoaderInterface[] |
||
53 | */ |
||
54 | protected $loaders = []; |
||
55 | |||
56 | /** |
||
57 | * Set of loaders with ability to JOIN it's data into parent SelectQuery. |
||
58 | * |
||
59 | * @var QueryLoader[] |
||
60 | */ |
||
61 | protected $joiners = []; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $class; |
||
67 | |||
68 | /** |
||
69 | * @invisible |
||
70 | * @var ORMInterface |
||
71 | */ |
||
72 | protected $orm; |
||
73 | |||
74 | /** |
||
75 | * Parent loader if any. |
||
76 | * |
||
77 | * @invisible |
||
78 | * @var QueryLoader |
||
79 | */ |
||
80 | protected $parent; |
||
81 | |||
82 | /** |
||
83 | * Loader options, can be altered on RecordSelector level. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $options = []; |
||
88 | |||
89 | /** |
||
90 | * Relation schema. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $schema = []; |
||
95 | |||
96 | /** |
||
97 | * @param string $class |
||
98 | * @param array $schema Relation schema. |
||
99 | * @param ORMInterface $orm |
||
100 | */ |
||
101 | public function __construct(string $class, array $schema, ORMInterface $orm) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function withContext(LoaderInterface $parent, array $options = []): LoaderInterface |
||
139 | |||
140 | /** |
||
141 | * Pre-load data on inner relation or relation chain. Method automatically called by Selector, |
||
142 | * see load() method. |
||
143 | * |
||
144 | * Method support chain initiation via dot notation. Method will return already exists loader if |
||
145 | * such presented. |
||
146 | * |
||
147 | * @see RecordSelector::load() |
||
148 | * |
||
149 | * @param string $relation Relation name, or chain of relations separated by. |
||
150 | * @param array $options Loader options (to be applied to last chain element only). |
||
151 | * @param bool $join When set to true loaders will be forced into JOIN mode. |
||
152 | * |
||
153 | * @return LoaderInterface Must return loader for a requested relation. |
||
154 | * |
||
155 | * @throws LoaderException |
||
156 | */ |
||
157 | final public function loadRelation( |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | final public function createNode(): AbstractNode |
||
212 | |||
213 | public function loadData(AbstractNode $node) |
||
230 | |||
231 | /** |
||
232 | * Ensure state of every nested loader. |
||
233 | */ |
||
234 | final public function __clone() |
||
246 | |||
247 | /** |
||
248 | * Destruct loader. |
||
249 | */ |
||
250 | final public function __destruct() |
||
255 | |||
256 | /** |
||
257 | * @param SelectQuery $query |
||
258 | * |
||
259 | * @return SelectQuery |
||
260 | */ |
||
261 | protected function configureQuery(SelectQuery $query): SelectQuery |
||
275 | |||
276 | /** |
||
277 | * Get database name associated with relation |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | protected function getDatabase(): string |
||
285 | |||
286 | /** |
||
287 | * Get table name associated with relation |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function getTable(): string |
||
295 | |||
296 | /** |
||
297 | * @return AbstractNode |
||
298 | */ |
||
299 | abstract protected function initNode(): AbstractNode; |
||
300 | |||
301 | /** |
||
302 | * Joined table alias. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | abstract protected function getAlias(): string; |
||
307 | |||
308 | /** |
||
309 | * list of columns to be loaded. |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | abstract protected function getColumns(): array; |
||
314 | |||
315 | /** |
||
316 | * Check if given relation is actually chain of relations. |
||
317 | * |
||
318 | * @param string $relation |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | private function isChain(string $relation): bool |
||
326 | |||
327 | /** |
||
328 | * @see loadRelation() |
||
329 | * @see joinRelation() |
||
330 | * |
||
331 | * @param string $chain |
||
332 | * @param array $options Final loader options. |
||
333 | * @param bool $join See loadRelation(). |
||
334 | * |
||
335 | * @return LoaderInterface |
||
336 | * |
||
337 | * @throws LoaderException When one of chain elements is not actually chainable (let's say ODM |
||
338 | * loader). |
||
339 | */ |
||
340 | private function loadChain(string $chain, array $options, bool $join): LoaderInterface |
||
365 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: