1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Collections; |
4
|
|
|
|
5
|
|
|
use Nip\Collections\Collection as AbstractCollection; |
6
|
|
|
use Nip\HelperBroker; |
7
|
|
|
use Nip\Records\AbstractModels\Record as Record; |
8
|
|
|
use Nip\Records\AbstractModels\RecordManager as Records; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Collection |
12
|
|
|
* @package Nip\Records\Collections |
13
|
|
|
*/ |
14
|
|
|
class Collection extends AbstractCollection |
15
|
|
|
{ |
16
|
|
|
protected $_indexKey = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Records |
20
|
|
|
*/ |
21
|
|
|
protected $_manager = null; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $relations |
26
|
|
|
*/ |
27
|
|
|
public function loadRelations($relations) |
28
|
|
|
{ |
29
|
|
|
if (is_string($relations)) { |
30
|
|
|
$relations = func_get_args(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
foreach ($relations as $relation) { |
34
|
|
|
$this->loadRelation($relation); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $name |
40
|
|
|
* @return Collection |
41
|
|
|
*/ |
42
|
|
|
public function loadRelation($name) |
43
|
|
|
{ |
44
|
|
|
$relation = $this->getRelation($name); |
45
|
|
|
$results = $relation->getEagerResults($this); |
46
|
|
|
$relation->match($this, $results); |
47
|
|
|
return $results; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $name |
52
|
|
|
* @return \Nip\Records\Relations\Relation|null |
53
|
|
|
*/ |
54
|
|
|
public function getRelation($name) |
55
|
|
|
{ |
56
|
|
|
return $this->getManager()->getRelation($name); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Records |
61
|
|
|
*/ |
62
|
1 |
|
public function getManager() |
63
|
|
|
{ |
64
|
1 |
|
if ($this->_manager == null) { |
65
|
|
|
$this->initManager(); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this->_manager; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Records $manager |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
2 |
|
public function setManager(Records $manager) |
76
|
|
|
{ |
77
|
2 |
|
$this->_manager = $manager; |
78
|
|
|
|
79
|
2 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function initManager() |
83
|
|
|
{ |
84
|
|
|
$manager = $this->rewind()->getManager(); |
|
|
|
|
85
|
|
|
$this->setManager($manager); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function toJSON() |
92
|
|
|
{ |
93
|
|
|
$return = []; |
94
|
|
|
foreach ($this as $item) { |
95
|
|
|
$return = $item->toArray(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return json_encode($return); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function save() |
102
|
|
|
{ |
103
|
|
|
if (count($this) > 0) { |
104
|
|
|
foreach ($this as $item) { |
105
|
|
|
$item->save(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Record $record |
112
|
|
|
* @param string $index |
113
|
|
|
*/ |
114
|
4 |
|
public function add($record, $index = null) |
115
|
|
|
{ |
116
|
4 |
|
$index = $this->getRecordKey($record, $index); |
|
|
|
|
117
|
4 |
|
parent::add($record, $index); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Record $record |
122
|
|
|
* @param null $index |
|
|
|
|
123
|
|
|
* @return string|null |
124
|
|
|
*/ |
125
|
7 |
|
public function getRecordKey(Record $record, $index = null) |
126
|
|
|
{ |
127
|
7 |
|
if ($index) { |
|
|
|
|
128
|
2 |
|
return $this->generateRecordKeyByIndex($record, $index); |
129
|
|
|
} |
130
|
|
|
|
131
|
5 |
|
$index = $this->getIndexKey(); |
132
|
5 |
|
if ($index) { |
133
|
|
|
return $this->generateRecordKeyByIndex($record, $index); |
134
|
|
|
} |
135
|
|
|
|
136
|
5 |
|
$index = $record->getPrimaryKey(); |
137
|
5 |
|
if (is_array($index)) { |
138
|
|
|
return implode('-', $index); |
139
|
|
|
} |
140
|
5 |
|
return $index; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Record $record |
145
|
|
|
* @param $index |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
2 |
|
protected function generateRecordKeyByIndex(Record $record, $index) |
149
|
|
|
{ |
150
|
2 |
|
if (is_array($index)) { |
151
|
1 |
|
return $record->implodeFields($index); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return $record->{$index}; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
5 |
|
public function getIndexKey() |
161
|
|
|
{ |
162
|
5 |
|
return $this->_indexKey; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $key |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
|
|
public function setIndexKey($key) |
170
|
|
|
{ |
171
|
|
|
return $this->_indexKey = $key; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param Record $record |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function has($record) |
179
|
|
|
{ |
180
|
|
|
if ($record instanceof Record) { |
|
|
|
|
181
|
|
|
return $this->hasRecord($record); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return parent::has($record); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param Record $record |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function hasRecord(Record $record) |
192
|
|
|
{ |
193
|
|
|
$index = $this->getRecordKey($record); |
194
|
|
|
|
195
|
|
|
return parent::has($index) && $this->get($index) == $record; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $record |
200
|
|
|
*/ |
201
|
|
|
public function remove($record) |
202
|
|
|
{ |
203
|
|
|
foreach ($this as $key => $item) { |
204
|
|
|
if ($item == $record) { |
205
|
|
|
unset($this[$key]); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* When $each is true, each record gets it's delete() method called. |
212
|
|
|
* Otherwise, a delete query is built for the entire collection |
213
|
|
|
* |
214
|
|
|
* @param bool $each |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
|
|
public function delete($each = false) |
218
|
|
|
{ |
219
|
|
|
if (count($this) > 0) { |
220
|
|
|
if ($each) { |
221
|
|
|
foreach ($this as $item) { |
222
|
|
|
$item->delete(); |
223
|
|
|
} |
224
|
|
|
} else { |
225
|
|
|
$primaryKey = $this->getManager()->getPrimaryKey(); |
226
|
|
|
$pk_list = HelperBroker::get('Arrays')->pluck($this, $primaryKey); |
|
|
|
|
227
|
|
|
|
228
|
|
|
$query = $this->getManager()->newQuery("delete"); |
229
|
|
|
$query->where("$primaryKey IN ?", $pk_list); |
230
|
|
|
$query->execute(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->clear(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|