1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Doctrine\Persistence\ObjectRepository; |
8
|
|
|
use PHPUnit\Framework\Assert; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @mixin EntityRepository |
12
|
|
|
* |
13
|
|
|
* @author Kevin Bond <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class RepositoryProxy implements ObjectRepository |
16
|
|
|
{ |
17
|
|
|
private ObjectRepository $repository; |
18
|
|
|
|
19
|
28 |
|
public function __construct(ObjectRepository $repository) |
20
|
|
|
{ |
21
|
28 |
|
$this->repository = $repository; |
22
|
28 |
|
} |
23
|
|
|
|
24
|
2 |
|
public function __call(string $method, array $arguments) |
25
|
|
|
{ |
26
|
2 |
|
return $this->proxyResult($this->repository->{$method}(...$arguments)); |
27
|
|
|
} |
28
|
|
|
|
29
|
10 |
|
public function getCount(): int |
30
|
|
|
{ |
31
|
10 |
|
if ($this->repository instanceof EntityRepository) { |
32
|
10 |
|
return $this->repository->count([]); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return \count($this->findAll()); |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
public function assertEmpty(): self |
39
|
|
|
{ |
40
|
4 |
|
return $this->assertCount(0); |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
public function assertCount(int $expectedCount): self |
44
|
|
|
{ |
45
|
|
|
// todo add message |
46
|
10 |
|
Assert::assertSame($expectedCount, $this->getCount()); |
47
|
|
|
|
48
|
10 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
public function assertCountGreaterThan(int $expected): self |
52
|
|
|
{ |
53
|
|
|
// todo add message |
54
|
2 |
|
Assert::assertGreaterThan($expected, $this->getCount()); |
55
|
|
|
|
56
|
2 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function assertCountGreaterThanOrEqual(int $expected): self |
60
|
|
|
{ |
61
|
|
|
// todo add message |
62
|
2 |
|
Assert::assertGreaterThanOrEqual($expected, $this->getCount()); |
63
|
|
|
|
64
|
2 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function assertCountLessThan(int $expected): self |
68
|
|
|
{ |
69
|
|
|
// todo add message |
70
|
2 |
|
Assert::assertLessThan($expected, $this->getCount()); |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function assertCountLessThanOrEqual(int $expected): self |
76
|
|
|
{ |
77
|
|
|
// todo add message |
78
|
2 |
|
Assert::assertLessThanOrEqual($expected, $this->getCount()); |
79
|
|
|
|
80
|
2 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param object|array|mixed $criteria |
85
|
|
|
*/ |
86
|
4 |
|
public function assertExists($criteria): self |
87
|
|
|
{ |
88
|
|
|
// todo add message |
89
|
4 |
|
Assert::assertNotNull($this->find($criteria)); |
90
|
|
|
|
91
|
4 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param object|array|mixed $criteria |
96
|
|
|
*/ |
97
|
2 |
|
public function assertNotExists($criteria): self |
98
|
|
|
{ |
99
|
|
|
// todo add message |
100
|
2 |
|
Assert::assertNull($this->find($criteria)); |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Proxy|object|null |
107
|
|
|
*/ |
108
|
2 |
|
public function first(): ?object |
109
|
|
|
{ |
110
|
2 |
|
return $this->findOneBy([]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Remove all rows. |
115
|
|
|
*/ |
116
|
2 |
|
public function truncate(): void |
117
|
|
|
{ |
118
|
2 |
|
$om = PersistenceManager::objectManagerFor($this->getClassName()); |
119
|
|
|
|
120
|
2 |
|
if (!$om instanceof EntityManagerInterface) { |
121
|
|
|
throw new \RuntimeException('This operation is only available when using doctrine/orm'); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$om->createQuery("DELETE {$this->getClassName()} e")->execute(); |
125
|
2 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param object|array|mixed $criteria |
129
|
|
|
* |
130
|
|
|
* @return Proxy|object|null |
131
|
|
|
*/ |
132
|
8 |
|
public function find($criteria): ?object |
133
|
|
|
{ |
134
|
8 |
|
if ($criteria instanceof Proxy) { |
135
|
2 |
|
$criteria = $criteria->object(); |
136
|
|
|
} |
137
|
|
|
|
138
|
8 |
|
if (!\is_array($criteria)) { |
139
|
2 |
|
return $this->proxyResult($this->repository->find($criteria)); |
140
|
|
|
} |
141
|
|
|
|
142
|
8 |
|
return $this->findOneBy($criteria); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return Proxy[]|object[] |
147
|
|
|
*/ |
148
|
2 |
|
public function findAll(): array |
149
|
|
|
{ |
150
|
2 |
|
return $this->proxyResult($this->repository->findAll()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return Proxy[]|object[] |
155
|
|
|
*/ |
156
|
2 |
|
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array |
157
|
|
|
{ |
158
|
2 |
|
return $this->proxyResult($this->repository->findBy(self::normalizeCriteria($criteria), $orderBy, $limit, $offset)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return Proxy|object|null |
163
|
|
|
*/ |
164
|
10 |
|
public function findOneBy(array $criteria): ?object |
165
|
|
|
{ |
166
|
10 |
|
return $this->proxyResult($this->repository->findOneBy(self::normalizeCriteria($criteria))); |
167
|
|
|
} |
168
|
|
|
|
169
|
12 |
|
public function getClassName(): string |
170
|
|
|
{ |
171
|
12 |
|
return $this->repository->getClassName(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param mixed $result |
176
|
|
|
* |
177
|
|
|
* @return Proxy|Proxy[]|object|object[]|mixed |
178
|
|
|
*/ |
179
|
12 |
|
private function proxyResult($result) |
180
|
|
|
{ |
181
|
12 |
|
if (\is_object($result) && $this->getClassName() === \get_class($result)) { |
182
|
10 |
|
return PersistenceManager::proxy($result); |
183
|
|
|
} |
184
|
|
|
|
185
|
8 |
|
if (\is_array($result)) { |
186
|
2 |
|
return \array_map([$this, 'proxyResult'], $result); |
187
|
|
|
} |
188
|
|
|
|
189
|
6 |
|
return $result; |
190
|
|
|
} |
191
|
|
|
|
192
|
10 |
|
private static function normalizeCriteria(array $criteria): array |
193
|
|
|
{ |
194
|
10 |
|
return \array_map( |
195
|
|
|
function($value) { |
196
|
8 |
|
return $value instanceof Proxy ? $value->object() : $value; |
197
|
10 |
|
}, |
198
|
10 |
|
$criteria |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|