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