1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Model\Collection; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Model\ModelInterface; |
8
|
|
|
use Chubbyphp\Model\ResolverInterface; |
9
|
|
|
use Chubbyphp\Model\ModelSortTrait; |
10
|
|
|
|
11
|
|
|
final class LazyModelCollection implements ModelCollectionInterface |
12
|
|
|
{ |
13
|
|
|
use ModelSortTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ResolverInterface |
17
|
|
|
*/ |
18
|
|
|
private $resolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $modelClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $foreignField; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $foreignId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array|null |
37
|
|
|
*/ |
38
|
|
|
private $orderBy; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $resolved = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ModelInterface[]|array |
47
|
|
|
*/ |
48
|
|
|
private $initialModels; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ModelInterface[]|array |
52
|
|
|
*/ |
53
|
|
|
private $models; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var \ReflectionProperty |
57
|
|
|
*/ |
58
|
|
|
private $propertyReflection; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ResolverInterface $resolver |
62
|
|
|
* @param string $modelClass |
63
|
|
|
* @param string $foreignField |
64
|
|
|
* @param string $foreignId |
65
|
|
|
* @param array|null $orderBy |
66
|
|
|
*/ |
67
|
11 |
View Code Duplication |
public function __construct( |
|
|
|
|
68
|
|
|
ResolverInterface $resolver, |
69
|
|
|
string $modelClass, |
70
|
|
|
string $foreignField, |
71
|
|
|
string $foreignId, |
72
|
|
|
array $orderBy = null |
73
|
|
|
) { |
74
|
11 |
|
$this->resolver = $resolver; |
75
|
11 |
|
$this->modelClass = $modelClass; |
76
|
11 |
|
$this->foreignField = $foreignField; |
77
|
11 |
|
$this->foreignId = $foreignId; |
78
|
11 |
|
$this->orderBy = $orderBy; |
79
|
|
|
|
80
|
11 |
|
$this->propertyReflection = new \ReflectionProperty($this->modelClass, $this->foreignField); |
81
|
11 |
|
$this->propertyReflection->setAccessible(true); |
82
|
11 |
|
} |
83
|
|
|
|
84
|
9 |
|
private function resolveModels() |
85
|
|
|
{ |
86
|
9 |
|
if ($this->resolved) { |
87
|
9 |
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
$this->resolved = true; |
91
|
|
|
|
92
|
9 |
|
$criteria = [$this->foreignField => $this->foreignId]; |
93
|
|
|
|
94
|
9 |
|
$models = []; |
95
|
9 |
|
foreach ($this->resolver->findBy($this->modelClass, $criteria, $this->orderBy) as $model) { |
96
|
5 |
|
$models[$model->getId()] = $model; |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
$this->initialModels = $models; |
100
|
9 |
|
$this->models = $models; |
101
|
9 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param ModelInterface $model |
105
|
|
|
* |
106
|
|
|
* @return ModelCollectionInterface |
107
|
|
|
*/ |
108
|
1 |
|
public function addModel(ModelInterface $model): ModelCollectionInterface |
109
|
|
|
{ |
110
|
1 |
|
$this->resolveModels(); |
111
|
|
|
|
112
|
1 |
|
$this->propertyReflection->setValue($model, $this->foreignId); |
113
|
|
|
|
114
|
1 |
|
$this->models[$model->getId()] = $model; |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param ModelInterface $model |
121
|
|
|
* |
122
|
|
|
* @return ModelCollectionInterface |
123
|
|
|
*/ |
124
|
1 |
View Code Duplication |
public function removeModel(ModelInterface $model): ModelCollectionInterface |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
$this->resolveModels(); |
127
|
|
|
|
128
|
1 |
|
if (isset($this->models[$model->getId()])) { |
129
|
1 |
|
$this->propertyReflection->setValue($model, null); |
130
|
|
|
|
131
|
1 |
|
unset($this->models[$model->getId()]); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param ModelInterface[]|array $models |
139
|
|
|
* |
140
|
|
|
* @return ModelCollectionInterface |
141
|
|
|
*/ |
142
|
1 |
View Code Duplication |
public function setModels(array $models): ModelCollectionInterface |
|
|
|
|
143
|
|
|
{ |
144
|
1 |
|
$this->resolveModels(); |
145
|
|
|
|
146
|
1 |
|
foreach ($this->models as $model) { |
147
|
|
|
$this->removeModel($model); |
148
|
|
|
} |
149
|
1 |
|
foreach ($models as $model) { |
150
|
1 |
|
$this->addModel($model); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return ModelInterface[]|array |
158
|
|
|
*/ |
159
|
2 |
|
public function getModels(): array |
160
|
|
|
{ |
161
|
2 |
|
$this->resolveModels(); |
162
|
|
|
|
163
|
2 |
|
return $this->sort($this->modelClass, array_values($this->models), $this->orderBy); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return ModelInterface[]|array |
168
|
|
|
*/ |
169
|
1 |
|
public function getInitialModels(): array |
170
|
|
|
{ |
171
|
1 |
|
$this->resolveModels(); |
172
|
|
|
|
173
|
1 |
|
return array_values($this->initialModels); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return \ArrayIterator |
178
|
|
|
*/ |
179
|
1 |
|
public function getIterator() |
180
|
|
|
{ |
181
|
1 |
|
$this->resolveModels(); |
182
|
|
|
|
183
|
1 |
|
return new \ArrayIterator($this->getModels()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return int |
188
|
|
|
*/ |
189
|
1 |
|
public function count() |
190
|
|
|
{ |
191
|
1 |
|
$this->resolveModels(); |
192
|
|
|
|
193
|
1 |
|
return count($this->getModels()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
1 |
View Code Duplication |
public function jsonSerialize(): array |
|
|
|
|
200
|
|
|
{ |
201
|
1 |
|
$this->jsonSerializableOrException(); |
202
|
|
|
|
203
|
1 |
|
$this->resolveModels(); |
204
|
|
|
|
205
|
1 |
|
$serializedModels = []; |
206
|
1 |
|
foreach ($this->getModels() as $model) { |
207
|
1 |
|
$serializedModels[] = $model->jsonSerialize(); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
return $serializedModels; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @throws \LogicException |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
private function jsonSerializableOrException() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
if (!in_array(\JsonSerializable::class, class_implements($this->modelClass), true)) { |
219
|
|
|
throw new \LogicException( |
220
|
|
|
sprintf('Model %s does not implement %s', $this->modelClass, \JsonSerializable::class) |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
1 |
|
public function getForeignField(): string |
229
|
|
|
{ |
230
|
1 |
|
return $this->foreignField; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
1 |
|
public function getForeignId(): string |
237
|
|
|
{ |
238
|
1 |
|
return $this->foreignId; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.