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, Result}; |
|
|
|
|
9
|
|
|
use Prozorov\Repositories\Exceptions\DataNotFound; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
class ArrayRepository implements RepositoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Collection $data |
16
|
|
|
*/ |
17
|
|
|
protected $data; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string $idField |
21
|
|
|
*/ |
22
|
|
|
protected $idField; |
23
|
|
|
|
24
|
15 |
|
public function __construct(array $data, string $idField = 'id') |
25
|
|
|
{ |
26
|
15 |
|
$this->data = (new Collection($data))->keyBy('id'); |
27
|
15 |
|
$this->idField = $idField; |
28
|
15 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritDoc |
32
|
|
|
*/ |
33
|
1 |
|
public function create(array $data) |
34
|
|
|
{ |
35
|
1 |
|
$this->data->put($data[$this->idField], $data); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
1 |
|
public function update($model, array $data): void |
42
|
|
|
{ |
43
|
1 |
|
if (is_int($model)) { |
44
|
1 |
|
$model = $this->data->get($model); |
45
|
|
|
|
46
|
1 |
|
$this->data->put($model[$this->idField], array_merge($model, $data)); |
47
|
|
|
} |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
1 |
|
public function delete(int $id): void |
54
|
|
|
{ |
55
|
1 |
|
$this->data->forget($id); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
1 |
|
public function exists(array $filter): bool |
62
|
|
|
{ |
63
|
1 |
|
$data = $this->getRaw((new Query())->where($filter)); |
64
|
|
|
|
65
|
1 |
|
return ! empty($data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
5 |
|
public function count(array $filter = []): int |
72
|
|
|
{ |
73
|
5 |
|
$data = $this->getRaw((new Query())->where($filter)); |
74
|
|
|
|
75
|
5 |
|
return count($data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
2 |
|
public function get(Query $query): Result |
82
|
|
|
{ |
83
|
2 |
|
return new Result( |
84
|
2 |
|
$this->getRaw($query), |
|
|
|
|
85
|
2 |
|
$this->getMeta($query) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function first(array $filter) |
93
|
|
|
{ |
94
|
|
|
$data = $this->getRaw((new Query())->where($filter)->limit(1)); |
95
|
|
|
|
96
|
|
|
if (empty($data[0])) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $data[0]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
6 |
|
public function getById(int $id, array $select = null) |
107
|
|
|
{ |
108
|
6 |
|
return $this->data->get($id); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
1 |
|
public function getByIdOrFail(int $id, array $select = null) |
115
|
|
|
{ |
116
|
1 |
|
$data = $this->getById($id, $select); |
|
|
|
|
117
|
|
|
|
118
|
1 |
|
if (empty($data)) { |
119
|
1 |
|
throw new DataNotFound(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns raw data |
127
|
|
|
* |
128
|
|
|
* @access protected |
129
|
|
|
* @param Query $query |
130
|
|
|
* @return iterable|null |
131
|
|
|
*/ |
132
|
7 |
|
protected function getRaw(Query $query) |
133
|
|
|
{ |
134
|
7 |
|
$data = $this->data; |
135
|
|
|
|
136
|
7 |
|
if (! empty($query->getWhere())) { |
137
|
3 |
|
foreach ($query->getWhere() as $key => $value) { |
138
|
3 |
|
$data = $this->data->where($key, $value); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
7 |
|
return $data->values()->toArray(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Calculate meta information |
147
|
|
|
* |
148
|
|
|
* @access protected |
149
|
|
|
* @param Query $query |
150
|
|
|
* @return array|null |
151
|
|
|
*/ |
152
|
2 |
|
protected function getMeta(Query $query): ?array |
153
|
|
|
{ |
154
|
2 |
|
if ($query->isWithMeta()) { |
155
|
|
|
$meta = [ |
156
|
1 |
|
'offset' => $query->getOffset(), |
157
|
1 |
|
'limit' => $query->getLimit(), |
158
|
|
|
]; |
159
|
|
|
|
160
|
1 |
|
if ($query->isCountTotal()) { |
161
|
1 |
|
$meta['total'] = $this->count($query->getWhere() ?? []); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
return $meta ?? null; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: