|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Prozorov\Repositories; |
|
6
|
|
|
|
|
7
|
|
|
use Prozorov\Repositories\Contracts\RepositoryInterface; |
|
8
|
|
|
use Prozorov\Repositories\Query; |
|
9
|
|
|
use Prozorov\Repositories\Exceptions\{DataNotFound, RepositoryException}; |
|
10
|
|
|
use Prozorov\Repositories\Helpers\DuckTyper; |
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
|
|
13
|
|
|
class ArrayRepository implements RepositoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Collection $data |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $data; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string $idField |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $idField; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Collection $dataCopy |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $dataCopy; |
|
29
|
|
|
|
|
30
|
16 |
|
public function __construct(array $data, string $idField = 'id') |
|
31
|
|
|
{ |
|
32
|
16 |
|
$this->data = (new Collection($data))->keyBy('id'); |
|
33
|
16 |
|
$this->idField = $idField; |
|
34
|
16 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritDoc |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function create(array $data) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->data->put($data[$this->idField], $data); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritDoc |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function update($model, array $data): void |
|
48
|
|
|
{ |
|
49
|
3 |
|
$id = is_int($model) ? $model : DuckTyper::getId($model, $this->idField); |
|
50
|
|
|
|
|
51
|
3 |
|
$model = $this->data->get($id); |
|
52
|
|
|
|
|
53
|
3 |
|
$this->data->put($model[$this->idField], array_merge($model, $data)); |
|
54
|
3 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function delete($model): void |
|
60
|
|
|
{ |
|
61
|
1 |
|
$id = is_int($model) ? $model : DuckTyper::getId($model, $this->idField); |
|
62
|
|
|
|
|
63
|
1 |
|
$this->data->forget($id); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function exists(array $filter): bool |
|
70
|
|
|
{ |
|
71
|
1 |
|
$data = $this->getRaw((new Query())->where($filter)); |
|
72
|
|
|
|
|
73
|
1 |
|
return ! empty($data); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function count(array $filter = []): int |
|
80
|
|
|
{ |
|
81
|
4 |
|
$data = $this->getRaw((new Query())->where($filter)); |
|
82
|
|
|
|
|
83
|
4 |
|
return count($data); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function get($params): ?iterable |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->getRaw($params); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritDoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function first(array $filter) |
|
98
|
|
|
{ |
|
99
|
|
|
$data = $this->getRaw((new Query())->where($filter)->limit(1)); |
|
100
|
|
|
|
|
101
|
|
|
if (empty($data[0])) { |
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $data[0]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
9 |
|
public function getById(int $id, array $select = null) |
|
112
|
|
|
{ |
|
113
|
9 |
|
return $this->data->get($id); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritDoc |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function getByIdOrFail(int $id, array $select = null) |
|
120
|
|
|
{ |
|
121
|
1 |
|
$data = $this->getById($id, $select); |
|
122
|
|
|
|
|
123
|
1 |
|
if (empty($data)) { |
|
124
|
1 |
|
throw new DataNotFound(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $data; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritDoc} |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function openTransaction(): void |
|
134
|
|
|
{ |
|
135
|
2 |
|
if ($this->dataCopy !== null) { |
|
136
|
|
|
throw new RepositoryException('Array repository does not support nested transactions'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
2 |
|
$this->dataCopy = clone $this->data; |
|
140
|
2 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritDoc} |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function commitTransaction(): void |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->assertTransactionIsOpened(); |
|
148
|
|
|
|
|
149
|
1 |
|
unset($this->dataCopy); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritDoc} |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function rollbackTransaction(): void |
|
156
|
|
|
{ |
|
157
|
1 |
|
$this->assertTransactionIsOpened(); |
|
158
|
|
|
|
|
159
|
1 |
|
$this->data = $this->dataCopy; |
|
160
|
|
|
|
|
161
|
1 |
|
unset($this->dataCopy); |
|
162
|
1 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns raw data |
|
166
|
|
|
* |
|
167
|
|
|
* @access protected |
|
168
|
|
|
* @param Query $query |
|
169
|
|
|
* @return iterable|null |
|
170
|
|
|
*/ |
|
171
|
6 |
|
protected function getRaw(Query $query) |
|
172
|
|
|
{ |
|
173
|
6 |
|
$data = $this->data; |
|
174
|
|
|
|
|
175
|
6 |
|
if (! empty($query->getWhere())) { |
|
176
|
2 |
|
foreach ($query->getWhere() as $key => $value) { |
|
177
|
2 |
|
$data = $this->data->where($key, $value); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
6 |
|
return $data->values()->toArray(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* assertTransactionIsOpened. |
|
186
|
|
|
* |
|
187
|
|
|
* @access protected |
|
188
|
|
|
* @return void |
|
189
|
|
|
*/ |
|
190
|
2 |
|
protected function assertTransactionIsOpened(): void |
|
191
|
|
|
{ |
|
192
|
2 |
|
if ($this->dataCopy === null) { |
|
193
|
|
|
throw new RepositoryException('No transaction has been opened'); |
|
194
|
|
|
} |
|
195
|
2 |
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|