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 PolderKnowledge\EntityService\Repository\Util; |
14
|
|
|
use UnexpectedValueException; |
15
|
|
|
|
16
|
|
|
class CollectionRepository implements |
17
|
|
|
EntityRepositoryInterface, |
18
|
|
|
DeletableInterface, |
19
|
|
|
ReadableInterface, |
20
|
|
|
WritableInterface |
21
|
|
|
{ |
22
|
|
|
private $allItems; |
23
|
|
|
|
24
|
|
|
private $persisted; |
25
|
|
|
|
26
|
18 |
|
public function __construct(array $initialItems = []) |
27
|
|
|
{ |
28
|
18 |
|
$this->allItems = new ArrayCollection($initialItems); |
29
|
18 |
|
$this->persisted = new ArrayCollection; |
30
|
18 |
|
} |
31
|
|
|
|
32
|
|
|
public function delete($entity) |
33
|
|
|
{ |
34
|
|
|
$this->allItems->removeElement($entity); |
35
|
|
|
$this->persisted->removeElement($entity); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function deleteBy($criteria) |
39
|
|
|
{ |
40
|
|
|
$criteria = Util::normalizeCriteria($criteria); |
41
|
|
|
|
42
|
|
|
$itemsToDelete = $this->allItems->matching($criteria); |
43
|
|
|
|
44
|
|
|
foreach ($itemsToDelete as $item) { |
45
|
|
|
$this->allItems->removeElement($item); |
46
|
|
|
$this->persisted->removeElement($item); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
public function countBy($criteria) |
51
|
|
|
{ |
52
|
6 |
|
$criteria = Util::normalizeCriteria($criteria); |
53
|
|
|
|
54
|
6 |
|
return $this->allItems->matching($criteria)->count(); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function find($id) |
58
|
|
|
{ |
59
|
3 |
|
return $this->allItems->filter( |
60
|
3 |
|
function ($entity) use ($id) { |
61
|
3 |
|
return $entity->getId() === $id; |
62
|
3 |
|
} |
63
|
3 |
|
)->first(); |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
public function findAll() |
67
|
|
|
{ |
68
|
9 |
|
return $this->allItems->toArray(); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public function findBy($criteria) |
72
|
|
|
{ |
73
|
3 |
|
$criteria = Util::normalizeCriteria($criteria); |
74
|
|
|
|
75
|
3 |
|
return $this->allItems->matching($criteria)->toArray(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function findOneBy($criteria) |
79
|
|
|
{ |
80
|
|
|
$criteria = Util::normalizeCriteria($criteria); |
81
|
|
|
|
82
|
|
|
return $this->allItems->matching($criteria)->first(); |
83
|
|
|
} |
84
|
|
|
|
85
|
12 |
|
public function persist($entity) |
86
|
|
|
{ |
87
|
12 |
|
if (!$this->allItems->contains($entity)) { |
88
|
9 |
|
$this->allItems->add($entity); |
89
|
|
|
} |
90
|
12 |
|
if (!$this->persisted->contains($entity)) { |
91
|
12 |
|
$this->persisted->add($entity); |
92
|
|
|
} |
93
|
12 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Method outside of the standard interface, for testing purposes |
97
|
|
|
*/ |
98
|
6 |
|
public function getPersisted(): ArrayCollection |
99
|
|
|
{ |
100
|
6 |
|
return clone $this->persisted; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|