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 Illuminate\Support\Collection; |
12
|
|
|
|
13
|
|
|
class ArrayRepository implements RepositoryInterface |
14
|
|
|
{ |
15
|
|
|
use HasMeta; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Collection $data |
19
|
|
|
*/ |
20
|
|
|
protected $data; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string $idField |
24
|
|
|
*/ |
25
|
|
|
protected $idField; |
26
|
|
|
|
27
|
15 |
|
public function __construct(array $data, string $idField = 'id') |
28
|
|
|
{ |
29
|
15 |
|
$this->data = (new Collection($data))->keyBy('id'); |
30
|
15 |
|
$this->idField = $idField; |
31
|
15 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
1 |
|
public function create(array $data) |
37
|
|
|
{ |
38
|
1 |
|
$this->data->put($data[$this->idField], $data); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
1 |
|
public function update($model, array $data): void |
45
|
|
|
{ |
46
|
1 |
|
if (is_int($model)) { |
47
|
1 |
|
$model = $this->data->get($model); |
48
|
|
|
|
49
|
1 |
|
$this->data->put($model[$this->idField], array_merge($model, $data)); |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
1 |
|
public function delete(int $id): void |
57
|
|
|
{ |
58
|
1 |
|
$this->data->forget($id); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
1 |
|
public function exists(array $filter): bool |
65
|
|
|
{ |
66
|
1 |
|
$data = $this->getRaw((new Query())->where($filter)); |
67
|
|
|
|
68
|
1 |
|
return ! empty($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
5 |
|
public function count(array $filter = []): int |
75
|
|
|
{ |
76
|
5 |
|
$data = $this->getRaw((new Query())->where($filter)); |
77
|
|
|
|
78
|
5 |
|
return count($data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
*/ |
84
|
2 |
|
public function get(Query $query): Result |
85
|
|
|
{ |
86
|
2 |
|
return new Result( |
87
|
2 |
|
$this->getRaw($query), |
|
|
|
|
88
|
2 |
|
$this->getMeta($query) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
|
|
public function first(array $filter) |
96
|
|
|
{ |
97
|
|
|
$data = $this->getRaw((new Query())->where($filter)->limit(1)); |
98
|
|
|
|
99
|
|
|
if (empty($data[0])) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $data[0]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
6 |
|
public function getById(int $id, array $select = null) |
110
|
|
|
{ |
111
|
6 |
|
return $this->data->get($id); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
1 |
|
public function getByIdOrFail(int $id, array $select = null) |
118
|
|
|
{ |
119
|
1 |
|
$data = $this->getById($id, $select); |
|
|
|
|
120
|
|
|
|
121
|
1 |
|
if (empty($data)) { |
122
|
1 |
|
throw new DataNotFound(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns raw data |
130
|
|
|
* |
131
|
|
|
* @access protected |
132
|
|
|
* @param Query $query |
133
|
|
|
* @return iterable|null |
134
|
|
|
*/ |
135
|
7 |
|
protected function getRaw(Query $query) |
136
|
|
|
{ |
137
|
7 |
|
$data = $this->data; |
138
|
|
|
|
139
|
7 |
|
if (! empty($query->getWhere())) { |
140
|
3 |
|
foreach ($query->getWhere() as $key => $value) { |
141
|
3 |
|
$data = $this->data->where($key, $value); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
return $data->values()->toArray(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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: