|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Domain\Repository\InMemory; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArraySet; |
|
14
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
|
15
|
|
|
use Cubiche\Core\Specification\Criteria; |
|
16
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
|
17
|
|
|
use Cubiche\Domain\Model\IdInterface; |
|
18
|
|
|
use Cubiche\Domain\Repository\QueryRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* InMemoryQueryRepository Class. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
24
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class InMemoryQueryRepository extends QueryRepository |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ArraySet |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $collection; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $entityName |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($entityName) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($entityName); |
|
39
|
|
|
|
|
40
|
|
|
$this->collection = new ArraySet(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function clear() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->collection->clear(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isEmpty() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->collection->isEmpty(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function toArray() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->collection->toArray(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function sorted(ComparatorInterface $criteria) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->collection->sorted($criteria); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function slice($offset, $length = null) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->collection->slice($offset, $length); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get(IdInterface $id) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->findOne(Criteria::method('id')->eq($id)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function find(SpecificationInterface $criteria) |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->collection->find($criteria); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function findOne(SpecificationInterface $criteria) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->collection->findOne($criteria); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getIterator() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->collection->getIterator(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function count() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->collection->count(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function persist($element) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->checkType($element); |
|
129
|
|
|
$this->collection->add($element); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function persistAll($elements) |
|
136
|
|
|
{ |
|
137
|
|
|
foreach ($elements as $element) { |
|
138
|
|
|
$this->persist($element); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function remove($element) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->checkType($element); |
|
148
|
|
|
$this->collection->remove($element); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|