|
1
|
|
|
<?php |
|
2
|
|
|
namespace vakata\orm; |
|
3
|
|
|
|
|
4
|
|
|
use \vakata\collection\Collection; |
|
5
|
|
|
|
|
6
|
|
|
class CollectionRepository implements Repository |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var Collection |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $collection; |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Collection |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $original; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $id = false; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var callable |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $searching = null; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $consumed = false; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $modified = false; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(Collection $collection, $id, callable $searching = null) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->collection = $collection; |
|
36
|
|
|
$this->original = clone $collection; |
|
37
|
|
|
$this->id = is_array($id) ? $id : [$id]; |
|
38
|
|
|
$this->searching = $searching ?? function () { |
|
39
|
|
|
return true; |
|
40
|
|
|
}; |
|
41
|
|
|
} |
|
42
|
|
|
public function __clone() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->reset(); |
|
45
|
|
|
} |
|
46
|
|
|
public function find($key) |
|
47
|
|
|
{ |
|
48
|
|
|
$where = []; |
|
49
|
|
|
foreach ($this->id as $field) { |
|
50
|
|
|
$where[$field] = $key[$field] ?? array_shift($key) ?? null; |
|
51
|
|
|
} |
|
52
|
|
|
return $this->collection->where($where)[0]; |
|
53
|
|
|
} |
|
54
|
|
|
public function filter(string $column, $value) : Repository |
|
55
|
|
|
{ |
|
56
|
|
|
$where = []; |
|
57
|
|
|
$where[$column] = $value; |
|
58
|
|
|
$this->collection = $this->collection->where($where); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
public function reject(string $column, $value) : Repository |
|
62
|
|
|
{ |
|
63
|
|
|
$this->collection = $this->collection->reject(function ($v) use ($column, $value) { |
|
64
|
|
|
$strict = false; |
|
65
|
|
|
$vv = is_object($v) ? (isset($v->{$column}) ? $v->{$column} : null) : (isset($v[$column]) ? $v[$column] : null); |
|
66
|
|
|
if (!$vv || ($strict && $vv !== $value) || (!$strict && $vv != $value)) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
return true; |
|
70
|
|
|
}); |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
public function sort(string $column, bool $desc = false) : Repository |
|
74
|
|
|
{ |
|
75
|
|
|
$this->collection = $this->collection->sortBy(function ($a, $b) use ($column, $desc) { |
|
76
|
|
|
$v1 = is_object($a) ? |
|
77
|
|
|
(isset($a->{$column}) ? $a->{$column} : null) : |
|
78
|
|
|
(isset($a[$column]) ? $a[$column] : null); |
|
79
|
|
|
$v2 = is_object($b) ? |
|
80
|
|
|
(isset($b->{$column}) ? $b->{$column} : null) : |
|
81
|
|
|
(isset($b[$column]) ? $b[$column] : null); |
|
82
|
|
|
return $desc ? $v2 <=> $v1 : $v1 <=> $v2; |
|
83
|
|
|
}); |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
public function limit(int $limit, int $offset = 0) : Repository |
|
87
|
|
|
{ |
|
88
|
|
|
$this->collection = $this->collection->rest($offset)->first($limit); |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
public function count() : int |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->collection->count(); |
|
94
|
|
|
} |
|
95
|
|
|
public function reset() : Repository |
|
96
|
|
|
{ |
|
97
|
|
|
$this->collection = $this->original; |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
public function current() |
|
101
|
|
|
{ |
|
102
|
|
|
if(!($data = $this->collection->current())) { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
$this->consumed = true; |
|
106
|
|
|
return $data; |
|
107
|
|
|
} |
|
108
|
|
|
public function offsetGet($offset) |
|
109
|
|
|
{ |
|
110
|
|
|
if(!($data = $this->collection->offsetGet($offset))) { |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
$this->consumed = true; |
|
114
|
|
|
return $data; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function append($entity) : Repository |
|
118
|
|
|
{ |
|
119
|
|
|
$this->modified = true; |
|
120
|
|
|
$this->collection[] = $entity; |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
public function change($entity) : Repository |
|
124
|
|
|
{ |
|
125
|
|
|
$this->modified = true; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
public function remove($entity) : Repository |
|
129
|
|
|
{ |
|
130
|
|
|
$k = $this->collection->indexOf($entity); |
|
131
|
|
|
if ($k !== false) { |
|
132
|
|
|
$this->modified = true; |
|
133
|
|
|
unset($this->collection[$k]); |
|
134
|
|
|
} |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function key() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->collection->key(); |
|
141
|
|
|
} |
|
142
|
|
|
public function rewind() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->collection->rewind(); |
|
145
|
|
|
} |
|
146
|
|
|
public function next() |
|
147
|
|
|
{ |
|
148
|
|
|
$this->collection->next(); |
|
149
|
|
|
} |
|
150
|
|
|
public function valid() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->collection->valid(); |
|
153
|
|
|
} |
|
154
|
|
|
public function offsetExists($offset) |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->collection->offsetExists($offset); |
|
157
|
|
|
} |
|
158
|
|
|
public function offsetUnset($offset) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->collection->offsetUnset($offset); |
|
161
|
|
|
} |
|
162
|
|
|
public function offsetSet($offset, $value) |
|
163
|
|
|
{ |
|
164
|
|
|
if ($offset !== null) { |
|
165
|
|
|
throw new \BadMethodCallException(); |
|
166
|
|
|
} |
|
167
|
|
|
$this->append($value); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function isConsumed() : bool |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->consumed; |
|
173
|
|
|
} |
|
174
|
|
|
public function isModified() : bool |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->modified; |
|
177
|
|
|
} |
|
178
|
|
|
public function toArray($entity) : array |
|
179
|
|
|
{ |
|
180
|
|
|
return []; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function search(string $q) : Repository |
|
184
|
|
|
{ |
|
185
|
|
|
$this->collection = $this->collection->filter(function ($v) use ($q) { |
|
186
|
|
|
return call_user_func($this->searching, $v, $q); |
|
187
|
|
|
}); |
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function any(array $criteria) : Repository |
|
192
|
|
|
{ |
|
193
|
|
|
$where = []; |
|
194
|
|
|
foreach ($criteria as $row) { |
|
195
|
|
|
$temp = []; |
|
196
|
|
|
$temp[$row[0]] = $row[1]; |
|
197
|
|
|
$where[] = $temp; |
|
198
|
|
|
} |
|
199
|
|
|
$this->collection = $this->collection->whereAny($where); |
|
200
|
|
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
public function all(array $criteria) : Repository |
|
203
|
|
|
{ |
|
204
|
|
|
$where = []; |
|
205
|
|
|
foreach ($criteria as $row) { |
|
206
|
|
|
$temp = []; |
|
207
|
|
|
$temp[$row[0]] = $row[1]; |
|
208
|
|
|
$where[] = $temp; |
|
209
|
|
|
} |
|
210
|
|
|
$this->collection = $this->collection->whereAll($where); |
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|