1 | <?php |
||
16 | abstract class AbstractNode |
||
17 | { |
||
18 | /** |
||
19 | * Indicates that node data is joined to parent row. |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $joined = false; |
||
24 | |||
25 | /** |
||
26 | * Column names to be used to hydrate based on given query rows. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private $columns = []; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | private $countColumns = 0; |
||
36 | |||
37 | /** |
||
38 | * Set of keys to be aggregated by Parser while parsing results. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $trackReferences = []; |
||
43 | |||
44 | /** |
||
45 | * Tree parts associated with reference keys and key values. |
||
46 | * |
||
47 | * $this->collectedReferences[id][ID_VALUE] = [ITEM1, ITEM2, ...]. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $references = []; |
||
52 | |||
53 | /** |
||
54 | * Node location in a tree. Set when node is registered. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $container; |
||
59 | |||
60 | /** |
||
61 | * Declared column which must be aggregated in a parent node. i.e. Parent Key |
||
62 | * |
||
63 | * @var null|string |
||
64 | */ |
||
65 | protected $referenceKey = null; |
||
66 | |||
67 | /** |
||
68 | * @invisible |
||
69 | * @var AbstractNode |
||
70 | */ |
||
71 | protected $parent; |
||
72 | |||
73 | /** |
||
74 | * @var AbstractNode[] |
||
75 | */ |
||
76 | protected $nodes = []; |
||
77 | |||
78 | /** |
||
79 | * @param array $columns |
||
80 | * @param string|null $referenceKey Defines column name in parent Node to be aggregated. |
||
81 | * @param array $columns |
||
82 | * @param string|null $referenceKey |
||
83 | */ |
||
84 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * Convert node into joined form (node will automatically parse parent row). |
||
95 | * |
||
96 | * @param bool $joined |
||
97 | * |
||
98 | * @return AbstractNode |
||
99 | */ |
||
100 | public function asJoined(bool $joined = true) |
||
107 | |||
108 | /** |
||
109 | * Get list of reference key values aggregated by parent. |
||
110 | * |
||
111 | * @return array |
||
112 | * |
||
113 | * @throws LoaderException |
||
114 | */ |
||
115 | public function getReferences(): array |
||
127 | |||
128 | /** |
||
129 | * Register new node into NodeTree. Nodes used to convert flat results into tree representation |
||
130 | * using reference aggregations. |
||
131 | * |
||
132 | * @param string $container |
||
133 | * @param AbstractNode $node |
||
134 | * |
||
135 | * @throws NodeException |
||
136 | */ |
||
137 | final public function registerNode(string $container, AbstractNode $node) |
||
148 | |||
149 | /** |
||
150 | * Fetch sub node. |
||
151 | * |
||
152 | * @param string $container |
||
153 | * |
||
154 | * @return AbstractNode |
||
155 | * |
||
156 | * @throws NodeException |
||
157 | */ |
||
158 | final public function fetchNode(string $container): AbstractNode |
||
166 | |||
167 | /** |
||
168 | * Parser result work, fetch data and mount it into parent tree. |
||
169 | * |
||
170 | * @param int $dataOffset |
||
171 | * @param array $row |
||
172 | */ |
||
173 | final public function parseRow(int $dataOffset, array $row) |
||
195 | |||
196 | /** |
||
197 | * In many cases (for example if you have inload of HAS_MANY relation) record data can be |
||
198 | * replicated by many result rows (duplicated). To prevent wrong data linking we have to |
||
199 | * deduplicate such records. This is only internal loader functionality and required due data |
||
200 | * tree are built using php references. |
||
201 | * |
||
202 | * Method will return true if data is unique handled before and false in opposite case. |
||
203 | * Provided data array will be automatically linked with it's unique state using references. |
||
204 | * |
||
205 | * @param array $data Reference to parsed record data, reference will be pointed to valid and |
||
206 | * existed data segment if such data was already parsed. |
||
207 | * |
||
208 | * @return bool Must return TRUE what data is unique in this selection. |
||
209 | */ |
||
210 | abstract protected function deduplicate(array &$data): bool; |
||
211 | |||
212 | /** |
||
213 | * Register data result. |
||
214 | * |
||
215 | * @param array $data |
||
216 | */ |
||
217 | abstract protected function registerData(array &$data); |
||
218 | |||
219 | /** |
||
220 | * Mount record data into internal data storage under specified container using reference key |
||
221 | * (inner key) and reference criteria (outer key value). |
||
222 | * |
||
223 | * Example (default ORM Loaders): |
||
224 | * $this->parent->mount('profile', 'id', 1, [ |
||
225 | * 'id' => 100, |
||
226 | * 'user_id' => 1, |
||
227 | * ... |
||
228 | * ]); |
||
229 | * |
||
230 | * In this example "id" argument is inner key of "user" record and it's linked to outer key |
||
231 | * "user_id" in "profile" record, which defines reference criteria as 1. |
||
232 | * |
||
233 | * Attention, data WILL be referenced to new memory location! |
||
234 | * |
||
235 | * @param string $container |
||
236 | * @param string $key |
||
237 | * @param mixed $criteria |
||
238 | * @param array $data Data must be referenced to existed set if it was registered |
||
239 | * previously. |
||
240 | * |
||
241 | * @throws LoaderException |
||
242 | */ |
||
243 | final protected function mount( |
||
260 | |||
261 | /** |
||
262 | * Mount record data into internal data storage under specified container using reference key |
||
263 | * (inner key) and reference criteria (outer key value). |
||
264 | * |
||
265 | * Example (default ORM Loaders): |
||
266 | * $this->parent->mountArray('comments', 'id', 1, [ |
||
267 | * 'id' => 100, |
||
268 | * 'user_id' => 1, |
||
269 | * ... |
||
270 | * ]); |
||
271 | * |
||
272 | * In this example "id" argument is inner key of "user" record and it's linked to outer key |
||
273 | * "user_id" in "profile" record, which defines reference criteria as 1. |
||
274 | * |
||
275 | * Add added records will be added as array items. |
||
276 | * |
||
277 | * @param string $container |
||
278 | * @param string $key |
||
279 | * @param mixed $criteria |
||
280 | * @param array $data Data must be referenced to existed set if it was registered |
||
281 | * previously. |
||
282 | * |
||
283 | * @throws LoaderException |
||
284 | */ |
||
285 | final protected function mountArray( |
||
300 | |||
301 | /** |
||
302 | * Fetch record columns from query row, must use data offset to slice required part of query. |
||
303 | * |
||
304 | * @param int $dataOffset |
||
305 | * @param array $line |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | protected function fetchData(int $dataOffset, array $line): array |
||
321 | |||
322 | /** |
||
323 | * Create internal references cache based on requested keys. For example, if we have request for |
||
324 | * "id" as reference key, every record will create following structure: |
||
325 | * $this->references[id][ID_VALUE] = ITEM. |
||
326 | * |
||
327 | * Only deduplicated data must be collected! |
||
328 | * |
||
329 | * @see deduplicate() |
||
330 | * |
||
331 | * @param array $data |
||
332 | */ |
||
333 | private function collectReferences(array &$data) |
||
340 | |||
341 | /** |
||
342 | * Create placeholders for each of sub nodes. |
||
343 | * |
||
344 | * @param array $data |
||
345 | */ |
||
346 | private function ensurePlaceholders(array &$data) |
||
353 | |||
354 | /** |
||
355 | * Add key to be tracked |
||
356 | * |
||
357 | * @param string $key |
||
358 | * |
||
359 | * @throws NodeException |
||
360 | */ |
||
361 | private function trackReference(string $key) |
||
372 | } |