1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Orm\Repository; |
15
|
|
|
|
16
|
|
|
use Override; |
|
|
|
|
17
|
|
|
use Valkyrja\Orm\Contract\Manager; |
18
|
|
|
use Valkyrja\Orm\Data\Value; |
19
|
|
|
use Valkyrja\Orm\Data\Where; |
20
|
|
|
use Valkyrja\Orm\Entity\Contract\Entity; |
21
|
|
|
use Valkyrja\Orm\Repository\Contract\Repository as Contract; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Repository. |
25
|
|
|
* |
26
|
|
|
* @author Melech Mizrachi |
27
|
|
|
* |
28
|
|
|
* @template T of Entity |
29
|
|
|
* |
30
|
|
|
* @implements Contract<T> |
31
|
|
|
*/ |
32
|
|
|
class Repository implements Contract |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @param class-string<T> $entity |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
protected Manager $manager, |
39
|
|
|
protected string $entity, |
40
|
|
|
) { |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
* |
46
|
|
|
* @return T|null |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
#[Override] |
49
|
|
|
public function find(int|string $id): Entity|null |
50
|
|
|
{ |
51
|
|
|
/** @var class-string<T> $entity */ |
52
|
|
|
$entity = $this->entity; |
53
|
|
|
$where = new Where( |
54
|
|
|
value: new Value( |
55
|
|
|
name: $entity::getIdField(), |
56
|
|
|
value: $id |
57
|
|
|
), |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return $this->findBy($where); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritDoc |
65
|
|
|
* |
66
|
|
|
* @return T|null |
67
|
|
|
*/ |
68
|
|
|
#[Override] |
69
|
|
|
public function findBy(Where ...$where): Entity|null |
70
|
|
|
{ |
71
|
|
|
$table = $this->entity::getTableName(); |
72
|
|
|
$select = $this->manager->createQueryBuilder()->select($table); |
73
|
|
|
$select->withWhere(...$where); |
74
|
|
|
|
75
|
|
|
$statement = $this->manager->prepare((string) $select); |
76
|
|
|
|
77
|
|
|
$fetch = $statement->fetchAll($this->entity); |
78
|
|
|
|
79
|
|
|
return $fetch[0] ?? null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
* |
85
|
|
|
* @return T[] |
86
|
|
|
*/ |
87
|
|
|
#[Override] |
88
|
|
|
public function all(): array |
89
|
|
|
{ |
90
|
|
|
return $this->allBy(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
* |
96
|
|
|
* @return T[] |
97
|
|
|
*/ |
98
|
|
|
#[Override] |
99
|
|
|
public function allBy(Where ...$where): array |
100
|
|
|
{ |
101
|
|
|
$table = $this->entity::getTableName(); |
102
|
|
|
$select = $this->manager->createQueryBuilder()->select($table); |
103
|
|
|
$select->withWhere(...$where); |
104
|
|
|
|
105
|
|
|
$statement = $this->manager->prepare((string) $select); |
106
|
|
|
|
107
|
|
|
return $statement->fetchAll($this->entity); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
* |
113
|
|
|
* @param T $entity The entity |
114
|
|
|
*/ |
115
|
|
|
#[Override] |
116
|
|
|
public function create(Entity $entity): void |
117
|
|
|
{ |
118
|
|
|
$table = $entity::getTableName(); |
119
|
|
|
|
120
|
|
|
$set = []; |
121
|
|
|
|
122
|
|
|
foreach ($entity->asStorableArray() as $key => $value) { |
123
|
|
|
$set[] = new Value( |
124
|
|
|
name: $key, |
125
|
|
|
value: $value |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$create = $this->manager |
130
|
|
|
->createQueryBuilder() |
131
|
|
|
->insert($table) |
132
|
|
|
->withSet(...$set); |
133
|
|
|
|
134
|
|
|
$this->manager->prepare((string) $create); |
135
|
|
|
|
136
|
|
|
$id = $this->manager->lastInsertId($table, $entity::getIdField()); |
137
|
|
|
|
138
|
|
|
$entity->__set($entity::getIdField(), $id); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
* |
144
|
|
|
* @param T $entity The entity |
145
|
|
|
*/ |
146
|
|
|
#[Override] |
147
|
|
|
public function update(Entity $entity): void |
148
|
|
|
{ |
149
|
|
|
$table = $entity::getTableName(); |
150
|
|
|
|
151
|
|
|
$where = new Where( |
152
|
|
|
value: new Value( |
153
|
|
|
name: $entity::getIdField(), |
154
|
|
|
value: $entity->getIdValue() |
155
|
|
|
), |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$set = []; |
159
|
|
|
|
160
|
|
|
foreach ($entity->asStorableChangedArray() as $key => $value) { |
161
|
|
|
$set[] = new Value( |
162
|
|
|
name: $key, |
163
|
|
|
value: $value |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$update = $this->manager |
168
|
|
|
->createQueryBuilder() |
169
|
|
|
->update($table) |
170
|
|
|
->withWhere($where) |
171
|
|
|
->withSet(...$set); |
172
|
|
|
|
173
|
|
|
$this->manager->prepare((string) $update); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
* |
179
|
|
|
* @param T $entity The entity |
180
|
|
|
*/ |
181
|
|
|
#[Override] |
182
|
|
|
public function delete(Entity $entity): void |
183
|
|
|
{ |
184
|
|
|
$table = $entity::getTableName(); |
185
|
|
|
|
186
|
|
|
$where = new Where( |
187
|
|
|
value: new Value( |
188
|
|
|
name: $entity::getIdField(), |
189
|
|
|
value: $entity->getIdValue() |
190
|
|
|
), |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$delete = $this->manager |
194
|
|
|
->createQueryBuilder() |
195
|
|
|
->delete($table) |
196
|
|
|
->withWhere($where); |
197
|
|
|
|
198
|
|
|
$this->manager->prepare((string) $delete); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths