-
Notifications
You must be signed in to change notification settings - Fork 5
/
InMemoryRepository.php
264 lines (234 loc) · 7.1 KB
/
InMemoryRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 25/01/16
* Time: 19:19.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\InMemory;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter as FilterInterface;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Page;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\WriteRepository;
use NilPortugues\Foundation\Domain\Model\Repository\Page as ResultPage;
use NilPortugues\Foundation\Infrastructure\Model\Repository\InMemory\Filter as InMemoryFilter;
class InMemoryRepository implements ReadRepository, WriteRepository, PageRepository
{
/**
* @var Identity[]
*/
protected $data = [];
/**
* NameRepository constructor.
*
* @param array $data
*/
public function __construct(array $data = [])
{
foreach ($data as $item) {
$this->add($item);
}
}
/**
* Returns a Page of entities meeting the paging restriction provided in the Pageable object.
*
* @param Pageable $pageable
*
* @return Page
*/
public function findAll(Pageable $pageable = null): Page
{
if (null === $pageable) {
return new ResultPage($this->data, count($this->data), 1, 1);
}
$results = $this->findBy($pageable->filters(), $pageable->sortings());
if (0 !== count($pageable->distinctFields()->get())) {
$results = $this->resultsWithDistinctFieldsOnly($pageable->distinctFields(), $results);
}
$pageSize = ($pageable->pageSize()) ? $pageable->pageSize() : 1;
return new ResultPage(
array_slice($results, $pageable->offset() - $pageable->pageSize(), $pageable->pageSize()),
count($results),
$pageable->pageNumber(),
ceil(count($results) / $pageSize)
);
}
/**
* Returns the total amount of elements in the repository given the restrictions provided by the Filter object.
*
* @param FilterInterface|null $filter
*
* @return int
*/
public function count(FilterInterface $filter = null): int
{
if (null === $filter) {
return count($this->data);
}
return count($this->findBy($filter));
}
/**
* Returns whether an entity with the given id exists.
*
* @param Identity $id
*
* @return bool
*/
public function exists(Identity $id): bool
{
$id = (string) $id;
return array_key_exists($id, $this->data);
}
/**
* Adds a new entity to the storage.
*
* @param Identity $value
*
* @return mixed
*/
public function add(Identity $value)
{
$id = (string) $value->id();
$this->data[$id] = clone $value;
return clone $value;
}
/**
* Adds a collections of entities to the storage.
*
* @param array $values
*
* @return mixed
*/
public function addAll(array $values)
{
foreach ($values as $value) {
$this->add($value);
}
}
/**
* Removes the entity with the given id.
*
* @param Identity $id
*/
public function remove(Identity $id)
{
if ($this->exists($id)) {
unset($this->data[$id->id()]);
}
}
/**
* Removes all elements in the repository given the restrictions provided by the Filter object.
* If $filter is null, all the repository data will be removed.
*
* @param FilterInterface $filter
*/
public function removeAll(FilterInterface $filter = null)
{
if (null === $filter) {
$this->data = [];
}
/** @var Identity $value */
foreach ($this->findBy($filter) as $value) {
unset($this->data[$value->id()]);
}
}
/**
* Retrieves an entity by its id.
*
* @param Identity $id
* @param Fields|null $fields
*
* @return array
*/
public function find(Identity $id, Fields $fields = null)
{
if (false === $this->exists($id)) {
return;
}
return clone $this->data[(string) $id];
}
/**
* Returns all instances of the type.
*
* @param FilterInterface|null $filter
* @param Sort|null $sort
* @param Fields|null $fields
*
* @return array
*/
public function findBy(FilterInterface $filter = null, Sort $sort = null, Fields $fields = null): array
{
$results = $this->data;
if (null !== $filter && !$filter->isNull()) {
$results = InMemoryFilter::filter($results, $filter);
}
if (null !== $sort && !$filter->isNull()) {
$results = Sorter::sort($results, $sort);
}
return array_values($results);
}
/**
* Repository data is added or removed as a whole block.
* Must work or fail and rollback any persisted/erased data.
*
* @param callable $transaction
*
* @throws \Exception
*/
public function transactional(callable $transaction)
{
$copy = $this->data;
try {
$transaction();
} catch (\Exception $e) {
$this->data = $copy;
throw $e;
}
}
/**
* Returns all instances of the type meeting $distinctFields values.
*
* @param Fields $distinctFields
* @param FilterInterface|null $filter
* @param Sort|null $sort
*
* @return array
*/
public function findByDistinct(Fields $distinctFields, FilterInterface $filter = null, Sort $sort = null): array
{
$results = $this->findBy($filter, $sort, $distinctFields);
return $this->resultsWithDistinctFieldsOnly($distinctFields, $results);
}
/**
* @param Fields $distinctFields
* @param $results
*
* @return array
*
* @throws \Exception
*/
protected function resultsWithDistinctFieldsOnly(Fields $distinctFields, $results): array
{
$newResults = [];
$valueHash = [];
foreach ($results as $result) {
$distinctValues = [];
foreach ($distinctFields->get() as $field) {
$distinctValues[$field] = PropertyValue::get($result, $field);
}
$hash = md5(serialize($distinctValues));
if (false === in_array($hash, $valueHash)) {
$valueHash[] = $hash;
$newResults[] = $result;
}
}
return $newResults;
}
}