1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prozorov\Repositories; |
6
|
|
|
|
7
|
|
|
use Prozorov\Repositories\Contracts\RepositoryInterface; |
8
|
|
|
use Prozorov\Repositories\Traits\HasMeta; |
9
|
|
|
use Prozorov\Repositories\{Query, Result}; |
|
|
|
|
10
|
|
|
use Prozorov\Repositories\Exceptions\DataNotFound; |
11
|
|
|
use Prozorov\Repositories\Exceptions\NotImplemented; |
12
|
|
|
|
13
|
|
|
abstract class FakableRepository implements RepositoryInterface |
14
|
|
|
{ |
15
|
|
|
use HasMeta; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Creates data |
19
|
|
|
* |
20
|
|
|
* @var array $data |
21
|
|
|
*/ |
22
|
|
|
abstract protected function doCreate(array $data); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Updates the provided model with provided data |
26
|
|
|
* |
27
|
|
|
* @var mixed $model |
28
|
|
|
* @var array $data |
29
|
|
|
*/ |
30
|
|
|
abstract protected function doUpdate($model, array $data): void; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Deletes provided record |
34
|
|
|
* |
35
|
|
|
* @var int $id |
36
|
|
|
*/ |
37
|
|
|
abstract protected function doDelete(int $id): void; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns count of requested data |
41
|
|
|
* |
42
|
|
|
* @var array $filter |
43
|
|
|
*/ |
44
|
|
|
abstract protected function doCount(array $filter): int; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns true if requested data exists in the repository |
48
|
|
|
* |
49
|
|
|
* @var array $filter |
50
|
|
|
*/ |
51
|
|
|
abstract protected function doExists(array $filter): bool; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns iterable or null if nothing is found |
55
|
|
|
* |
56
|
|
|
* @var Query $params |
57
|
|
|
*/ |
58
|
|
|
abstract protected function doGet(Query $query); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function create(array $data) |
64
|
|
|
{ |
65
|
|
|
if ($this->isFake()) { |
66
|
|
|
return $this->fixtures()->create($data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->doCreate($filter); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
public function update($model, array $data): void |
76
|
|
|
{ |
77
|
|
|
if ($this->isFake()) { |
78
|
|
|
$this->fixtures()->update($model, $data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->doUpdate($model, $data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function delete(int $id): void |
88
|
|
|
{ |
89
|
|
|
if ($this->isFake()) { |
90
|
|
|
$this->fixtures()->delete($id); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->doDelete($id); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function exists(array $filter): bool |
100
|
|
|
{ |
101
|
|
|
if ($this->isFake()) { |
102
|
|
|
return $this->fixtures()->exists($filter); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->doExists($filter); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
|
|
public function count(array $filter = []): int |
112
|
|
|
{ |
113
|
|
|
if ($this->isFake()) { |
114
|
|
|
return $this->fixtures()->count($filter); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->doCount($filter); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
1 |
|
public function get(Query $query): Result |
124
|
|
|
{ |
125
|
1 |
|
if ($this->isFake()) { |
126
|
1 |
|
return $this->fixtures()->get($query); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return new Result( |
130
|
1 |
|
$this->getRaw($query), |
|
|
|
|
131
|
1 |
|
$this->getMeta($query) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function first(array $filter) |
139
|
|
|
{ |
140
|
|
|
$data = $this->getRaw((new Query())->where($filter)->limit(1)); |
141
|
|
|
|
142
|
|
|
if (empty($data[0])) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $data[0]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
1 |
|
public function getById(int $id, array $select = null) |
153
|
|
|
{ |
154
|
1 |
|
$data = $this->getRaw((new Query())->where(['id' => $id])->limit(1)->select($select)); |
155
|
|
|
|
156
|
1 |
|
if (empty($data[0])) { |
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
return $data[0]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritDoc |
165
|
|
|
*/ |
166
|
|
|
public function getByIdOrFail(int $id, array $select = null) |
167
|
|
|
{ |
168
|
|
|
$data = $this->getById($id, $select); |
169
|
|
|
|
170
|
|
|
if (empty($data)) { |
171
|
|
|
throw new DataNotFound(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $data; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns raw data |
179
|
|
|
* |
180
|
|
|
* @access protected |
181
|
|
|
* @param Query $query |
182
|
|
|
* @return iterable|null |
183
|
|
|
*/ |
184
|
1 |
|
protected function getRaw(Query $query) |
185
|
|
|
{ |
186
|
1 |
|
if ($this->isFake()) { |
187
|
1 |
|
return $this->fixtures()->get($query)->getData(); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
return $this->doGet($query); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* isFake. |
195
|
|
|
* |
196
|
|
|
* @access protected |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
1 |
|
protected function isFake(): bool |
200
|
|
|
{ |
201
|
|
|
// Fixtures mode is disabled by default. Please refer to the documentation |
202
|
1 |
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* getFixtures. |
207
|
|
|
* |
208
|
|
|
* @access protected |
209
|
|
|
* @return ArrayRepository |
210
|
|
|
*/ |
211
|
|
|
protected function fixtures(): ArrayRepository |
212
|
|
|
{ |
213
|
|
|
throw new RuntimeException('Fixtures mode is disabled. Please refer to the documentation'); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
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: