1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Model\Collection; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Model\ModelInterface; |
8
|
|
|
|
9
|
|
|
final class LazyModelCollection implements ModelCollectionInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Closure; |
13
|
|
|
*/ |
14
|
|
|
private $resolver; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ModelInterface[]|array |
18
|
|
|
*/ |
19
|
|
|
private $initialModels; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ModelInterface[]|array |
23
|
|
|
*/ |
24
|
|
|
private $models; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \Closure $resolver |
28
|
|
|
*/ |
29
|
|
|
public function __construct(\Closure $resolver) |
30
|
|
|
{ |
31
|
|
|
$this->resolver = $resolver; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function resolveModels() |
35
|
|
|
{ |
36
|
|
|
if (null === $this->resolver) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$resolver = $this->resolver; |
41
|
|
|
|
42
|
|
|
$this->resolver = null; |
43
|
|
|
|
44
|
|
|
$this->setModels($resolver()); |
45
|
|
|
$this->initialModels = $this->models; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ModelInterface $model |
50
|
|
|
* @return ModelCollectionInterface |
51
|
|
|
*/ |
52
|
|
|
public function addModel(ModelInterface $model): ModelCollectionInterface |
53
|
|
|
{ |
54
|
|
|
$this->resolveModels(); |
55
|
|
|
|
56
|
|
|
$this->models[$model->getId()] = $model; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ModelInterface $model |
63
|
|
|
* @return ModelCollectionInterface |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function removeModel(ModelInterface $model): ModelCollectionInterface |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->resolveModels(); |
68
|
|
|
|
69
|
|
|
if (isset($this->models[$model->getId()])) { |
70
|
|
|
unset($this->models[$model->getId()]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ModelInterface[]|array $models |
78
|
|
|
* @return ModelCollectionInterface |
79
|
|
|
*/ |
80
|
|
|
public function setModels(array $models): ModelCollectionInterface |
81
|
|
|
{ |
82
|
|
|
$this->resolveModels(); |
83
|
|
|
|
84
|
|
|
$this->models = []; |
85
|
|
|
foreach ($models as $model) { |
86
|
|
|
$this->addModel($model); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return ModelInterface[]|array |
94
|
|
|
*/ |
95
|
|
|
public function getModels(): array |
96
|
|
|
{ |
97
|
|
|
$this->resolveModels(); |
98
|
|
|
|
99
|
|
|
return $this->models; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return ModelInterface[]|array |
104
|
|
|
*/ |
105
|
|
|
public function getInitialModels(): array |
106
|
|
|
{ |
107
|
|
|
$this->resolveModels(); |
108
|
|
|
|
109
|
|
|
return $this->initialModels; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function jsonSerialize(): array |
116
|
|
|
{ |
117
|
|
|
$this->resolveModels(); |
118
|
|
|
|
119
|
|
|
$serializedModels = []; |
120
|
|
|
foreach ($this->models as $model) { |
121
|
|
|
$serializedModels[] = $model->jsonSerialize(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $serializedModels; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.