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
|
|
|
namespace Cubiche\Infrastructure\Collections\Doctrine\Common\Collections; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArraySetInterface; |
13
|
|
|
use Cubiche\Core\Collections\DataSourceSet; |
14
|
|
|
use Cubiche\Core\Comparable\Comparator; |
15
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
16
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
17
|
|
|
use Cubiche\Core\Collections\DataSource\IteratorDataSource; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PersistentArraySet Class. |
21
|
|
|
* |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class PersistentArraySet extends PersistentCollectionAdapter implements ArraySetInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function addAll($elements) |
31
|
|
|
{ |
32
|
|
|
foreach ($elements as $element) { |
33
|
|
|
$this->add($element); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function find(SpecificationInterface $criteria) |
41
|
|
|
{ |
42
|
|
|
return new DataSourceSet(new IteratorDataSource($this->getIterator(), $criteria)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function findOne(SpecificationInterface $criteria) |
49
|
|
|
{ |
50
|
|
|
return (new IteratorDataSource($this->getIterator(), $criteria))->findOne(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function sorted(ComparatorInterface $criteria) |
57
|
|
|
{ |
58
|
|
|
return new DataSourceSet(new IteratorDataSource($this->getIterator(), null, $criteria)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function sort(ComparatorInterface $criteria = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$elements = $this->collection->toArray(); |
67
|
|
|
if ($criteria === null) { |
68
|
|
|
$criteria = new Comparator(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
usort($elements, function ($a, $b) use ($criteria) { |
72
|
|
|
return $criteria->compare($a, $b); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->clear(); |
76
|
|
|
foreach ($elements as $element) { |
77
|
|
|
$this->add($element); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function removeAll($elements) |
85
|
|
|
{ |
86
|
|
|
foreach ($elements as $element) { |
87
|
|
|
$this->removeElement($element); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function containsAll($elements) |
95
|
|
|
{ |
96
|
|
|
foreach ($elements as $element) { |
97
|
|
|
if (!$this->contains($element)) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.