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\Core\Collections\ArrayCollection; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Comparable\Comparator; |
13
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* SortedArraySet Class. |
17
|
|
|
* |
18
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class SortedArraySet extends ArraySet |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ComparatorInterface |
25
|
|
|
*/ |
26
|
|
|
protected $criteria; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SortedArraySet constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $elements |
32
|
|
|
* @param ComparatorInterface|null $criteria |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function __construct(array $elements = array(), ComparatorInterface $criteria = null) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
if ($criteria === null) { |
37
|
|
|
$criteria = new Comparator(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->criteria = $criteria; |
41
|
|
|
parent::__construct($elements); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function add($element) |
48
|
|
|
{ |
49
|
|
|
if (!$this->contains($element)) { |
50
|
|
|
$this->elements[] = $element; |
51
|
|
|
|
52
|
|
|
$this->sort(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function addAll($elements) |
60
|
|
|
{ |
61
|
|
|
$this->validateTraversable($elements); |
62
|
|
|
|
63
|
|
|
$changed = false; |
64
|
|
|
foreach ($elements as $element) { |
65
|
|
|
if (!$this->contains($element)) { |
66
|
|
|
$this->elements[] = $element; |
67
|
|
|
$changed = true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($changed) { |
72
|
|
|
$this->sort(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function remove($element) |
80
|
|
|
{ |
81
|
|
|
if (parent::remove($element)) { |
82
|
|
|
$this->sort(); |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function removeAll($elements) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->validateTraversable($elements); |
96
|
|
|
|
97
|
|
|
$changed = false; |
98
|
|
|
foreach ($elements as $element) { |
99
|
|
|
if (parent::remove($element)) { |
|
|
|
|
100
|
|
|
$changed = true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($changed) { |
105
|
|
|
$this->sort(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $changed; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function sort(ComparatorInterface $criteria = null) |
115
|
|
|
{ |
116
|
|
|
if ($criteria !== null) { |
117
|
|
|
$this->criteria = $criteria; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
parent::sort($this->criteria); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function offsetUnset($offset) |
127
|
|
|
{ |
128
|
|
|
parent::offsetUnset($offset); |
129
|
|
|
|
130
|
|
|
$this->sort(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.