1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cubiche\Core\Collections\ArrayCollection; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\DataSource\ArrayDataSource; |
14
|
|
|
use Cubiche\Core\Collections\DataSourceSet; |
15
|
|
|
use Cubiche\Core\Collections\Exception\InvalidKeyException; |
16
|
|
|
use Cubiche\Core\Comparable\Comparator; |
17
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
18
|
|
|
use Cubiche\Core\Specification\Criteria; |
19
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ArraySet Class. |
23
|
|
|
* |
24
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
25
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ArraySet extends ArrayCollection implements ArraySetInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* ArraySet constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $elements |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $elements = array()) |
35
|
|
|
{ |
36
|
|
|
$this->addAll($elements); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function add($element) |
43
|
|
|
{ |
44
|
|
|
$criteria = Criteria::eq($element); |
45
|
|
|
foreach ($this->elements as $key => $value) { |
46
|
|
|
if ($criteria->evaluate($value)) { |
47
|
|
|
$this->elements[$key] = $element; |
48
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->elements[] = $element; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function addAll($elements) |
60
|
|
|
{ |
61
|
|
|
$this->validateTraversable($elements); |
62
|
|
|
|
63
|
|
|
foreach ($elements as $element) { |
64
|
|
|
$this->add($element); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function contains($element) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$criteria = Criteria::eq($element); |
74
|
|
|
foreach ($this->elements as $key => $value) { |
75
|
|
|
if ($criteria->evaluate($value)) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function containsAll($elements) |
87
|
|
|
{ |
88
|
|
|
$this->validateTraversable($elements); |
89
|
|
|
|
90
|
|
|
foreach ($elements as $element) { |
91
|
|
|
if (!$this->contains($element)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function remove($element) |
103
|
|
|
{ |
104
|
|
|
$criteria = Criteria::eq($element); |
105
|
|
|
foreach ($this->elements as $key => $value) { |
106
|
|
|
if ($criteria->evaluate($value)) { |
107
|
|
|
unset($this->elements[$key]); |
108
|
|
|
$this->elements = array_values($this->elements); |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function removeAll($elements) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$this->validateTraversable($elements); |
123
|
|
|
|
124
|
|
|
$changed = false; |
125
|
|
|
foreach ($elements as $element) { |
126
|
|
|
if ($this->remove($element)) { |
127
|
|
|
$changed = true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $changed; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function sort(ComparatorInterface $criteria = null) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
if ($criteria === null) { |
140
|
|
|
$criteria = new Comparator(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
uasort($this->elements, function ($a, $b) use ($criteria) { |
144
|
|
|
return $criteria->compare($a, $b); |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function sorted(ComparatorInterface $criteria) |
152
|
|
|
{ |
153
|
|
|
return new DataSourceSet(new ArrayDataSource($this->elements, null, $criteria)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function find(SpecificationInterface $criteria) |
160
|
|
|
{ |
161
|
|
|
return new DataSourceSet(new ArrayDataSource($this->elements, $criteria)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function findOne(SpecificationInterface $criteria) |
168
|
|
|
{ |
169
|
|
|
return (new ArrayDataSource($this->elements, $criteria))->findOne(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function offsetSet($offset, $value) |
176
|
|
|
{ |
177
|
|
|
$this->add($value); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
protected function validateKey($key) |
184
|
|
|
{ |
185
|
|
|
if (!is_int($key)) { |
186
|
|
|
throw InvalidKeyException::forKey($key); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.