1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PolderKnowledge\EntityService\Repository\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use PolderKnowledge\EntityService\Repository\EntityRepositoryInterface; |
9
|
|
|
use PolderKnowledge\EntityService\Repository\Feature\DeletableInterface; |
10
|
|
|
use PolderKnowledge\EntityService\Repository\Feature\ReadableInterface; |
11
|
|
|
use PolderKnowledge\EntityService\Repository\Feature\TransactionAwareInterface; |
12
|
|
|
use PolderKnowledge\EntityService\Repository\Feature\WritableInterface; |
13
|
|
|
use UnexpectedValueException; |
14
|
|
|
|
15
|
|
|
class CollectionRepository implements |
16
|
|
|
EntityRepositoryInterface, |
17
|
|
|
DeletableInterface, |
18
|
|
|
ReadableInterface, |
19
|
|
|
WritableInterface |
20
|
|
|
{ |
21
|
|
|
private $allItems; |
22
|
|
|
|
23
|
|
|
private $persisted; |
24
|
|
|
|
25
|
15 |
|
public function __construct(array $initialItems = []) |
26
|
|
|
{ |
27
|
15 |
|
$this->allItems = new ArrayCollection($initialItems); |
28
|
15 |
|
$this->persisted = new ArrayCollection; |
29
|
15 |
|
} |
30
|
|
|
|
31
|
|
|
public function delete($entity) |
32
|
|
|
{ |
33
|
|
|
$this->allItems->removeElement($entity); |
34
|
|
|
$this->persisted->removeElement($entity); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function deleteBy($criteria) |
38
|
|
|
{ |
39
|
|
|
$itemsToDelete = $this->allItems->matching($criteria); |
|
|
|
|
40
|
|
|
|
41
|
|
|
foreach ($itemsToDelete as $item) { |
42
|
|
|
$this->allItems->removeElement($item); |
43
|
|
|
$this->persisted->removeElement($item); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
public function countBy($criteria) |
48
|
|
|
{ |
49
|
6 |
|
return $this->allItems->matching($criteria)->count(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
public function find($id) |
53
|
|
|
{ |
54
|
3 |
|
return $this->allItems->filter( |
55
|
3 |
|
function ($entity) use ($id) { |
56
|
3 |
|
return $entity->getId() === $id; |
57
|
3 |
|
} |
58
|
3 |
|
)->first(); |
59
|
|
|
} |
60
|
|
|
|
61
|
9 |
|
public function findAll() |
62
|
|
|
{ |
63
|
9 |
|
return $this->allItems->toArray(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function findBy($criteria) |
67
|
|
|
{ |
68
|
|
|
return $this->allItems->matching($criteria)->toArray(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function findOneBy($criteria) |
72
|
|
|
{ |
73
|
|
|
return $this->allItems->matching($criteria)->first(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
public function persist($entity) |
77
|
|
|
{ |
78
|
12 |
|
if (!$this->allItems->contains($entity)) { |
79
|
9 |
|
$this->allItems->add($entity); |
80
|
|
|
} |
81
|
12 |
|
if (!$this->persisted->contains($entity)) { |
82
|
12 |
|
$this->persisted->add($entity); |
83
|
|
|
} |
84
|
12 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Method outside of the standard interface, for testing purposes |
88
|
|
|
*/ |
89
|
6 |
|
public function getPersisted(): ArrayCollection |
90
|
|
|
{ |
91
|
6 |
|
return clone $this->persisted; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.