1 | <?php |
||
18 | abstract class MultipleRelation extends AbstractRelation |
||
19 | { |
||
20 | use MatchTrait, PartialTrait; |
||
21 | |||
22 | /** |
||
23 | * Loaded list of records. SplObjectStorage? |
||
24 | * |
||
25 | * @var RecordInterface[] |
||
26 | */ |
||
27 | protected $instances = []; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | * |
||
32 | * We have to init relations right after initialization, this might be optimized in a future. |
||
33 | */ |
||
34 | public function withContext( |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function hasRelated(): bool |
||
57 | |||
58 | /** |
||
59 | * Such relations will represent themselves. |
||
60 | * |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function getRelated() |
||
67 | |||
68 | /** |
||
69 | * Iterate over instance set. |
||
70 | * |
||
71 | * @return \ArrayIterator |
||
72 | */ |
||
73 | public function getIterator() |
||
77 | |||
78 | /** |
||
79 | * @return int |
||
80 | */ |
||
81 | public function count() |
||
85 | |||
86 | /** |
||
87 | * Method will autoload data. |
||
88 | * |
||
89 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function has($query): bool |
||
97 | |||
98 | /** |
||
99 | * Fine one entity for a given query or return null. Method will autoload data. |
||
100 | * |
||
101 | * Example: ->matchOne(['value' => 'something', ...]); |
||
102 | * |
||
103 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
104 | * |
||
105 | * @return RecordInterface|null |
||
106 | */ |
||
107 | public function matchOne($query) |
||
117 | |||
118 | /** |
||
119 | * Return only instances matched given query, performed in memory! Only simple conditions are |
||
120 | * allowed. Not "find" due trademark violation. Method will autoload data. |
||
121 | * |
||
122 | * Example: ->matchMultiple(['value' => 'something', ...]); |
||
123 | * |
||
124 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
125 | * |
||
126 | * @return \ArrayIterator |
||
127 | */ |
||
128 | public function matchMultiple($query) |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | * |
||
143 | * @return self |
||
144 | * |
||
145 | * @throws SelectorException |
||
146 | * @throws QueryException (needs wrapping) |
||
147 | */ |
||
148 | protected function loadData(bool $autoload = true): self |
||
167 | |||
168 | /** |
||
169 | * Init pre-loaded data. |
||
170 | * |
||
171 | * @return HasManyRelation |
||
172 | */ |
||
173 | protected function initInstances(): self |
||
194 | |||
195 | /** |
||
196 | * Fetch relation data from database. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | abstract protected function loadRelated(): array; |
||
201 | } |