1
|
|
|
<?php namespace GenericCollections\Abstracts; |
2
|
|
|
|
3
|
|
|
use GenericCollections\Interfaces\CollectionInterface; |
4
|
|
|
use GenericCollections\Internal\DataArray; |
5
|
|
|
use GenericCollections\Traits\CollectionMethods; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is a partial implementation of CollectionInterface |
9
|
|
|
* The main Collection object extends this class but types methods |
10
|
|
|
* |
11
|
|
|
* Use this class to implement your own collection class. |
12
|
|
|
* You will need to implement the getElementType method on your concrete case. |
13
|
|
|
* |
14
|
|
|
* @package GenericCollections\Abstracts |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractCollection extends DataArray implements CollectionInterface |
17
|
|
|
{ |
18
|
|
|
use CollectionMethods; |
19
|
|
|
|
20
|
72 |
|
public function add($element) |
21
|
|
|
{ |
22
|
72 |
|
if ($this->optionUniqueValues() && $this->contains($element)) { |
23
|
3 |
|
return false; |
24
|
|
|
} |
25
|
72 |
View Code Duplication |
if (! $this->checkElementType($element)) { |
|
|
|
|
26
|
3 |
|
throw new \InvalidArgumentException( |
27
|
|
|
'Invalid element type;' |
28
|
3 |
|
. ' the collection ' . get_class($this) |
29
|
3 |
|
. ' was expecting a ' . $this->getElementType() . ' type' |
30
|
3 |
|
); |
31
|
|
|
} |
32
|
69 |
|
$this->data[] = $element; |
33
|
69 |
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
21 |
|
public function contains($element) |
37
|
|
|
{ |
38
|
21 |
|
return in_array($element, $this->data, $this->optionComparisonIsIdentical()); |
39
|
|
|
} |
40
|
|
|
|
41
|
18 |
|
public function remove($element) |
42
|
|
|
{ |
43
|
18 |
|
$index = array_search($element, $this->data, $this->optionComparisonIsIdentical()); |
44
|
18 |
|
if (false === $index) { |
45
|
12 |
|
return false; |
46
|
|
|
} |
47
|
18 |
|
unset($this->data[$index]); |
48
|
18 |
|
if ($index < count($this->data)) { |
49
|
18 |
|
$this->data = array_values($this->data); |
50
|
18 |
|
} |
51
|
18 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
public function retainAll(array $elements) |
55
|
|
|
{ |
56
|
6 |
|
$changed = false; |
57
|
6 |
|
foreach ($this->data as $index => $element) { |
58
|
6 |
|
if (! in_array($element, $elements, $this->optionComparisonIsIdentical())) { |
59
|
6 |
|
unset($this->data[$index]); |
60
|
6 |
|
$changed = true; |
61
|
6 |
|
} |
62
|
6 |
|
} |
63
|
6 |
|
if ($changed) { |
64
|
6 |
|
$this->data = array_values($this->data); |
65
|
6 |
|
} |
66
|
6 |
|
return $changed; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
public function removeAll(array $elements) |
70
|
|
|
{ |
71
|
6 |
|
$changed = false; |
72
|
6 |
|
foreach ($elements as $element) { |
73
|
|
|
do { |
74
|
6 |
|
$removed = $this->remove($element); |
75
|
6 |
|
if ($removed) { |
76
|
6 |
|
$changed = true; |
77
|
6 |
|
} |
78
|
6 |
|
} while ($removed); |
79
|
6 |
|
} |
80
|
6 |
|
return $changed; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function removeIf(callable $callable) |
84
|
|
|
{ |
85
|
3 |
|
$changed = false; |
86
|
3 |
|
foreach ($this->data as $element) { |
87
|
3 |
|
if (true === call_user_func($callable, $element)) { |
88
|
3 |
|
if ($this->remove($element)) { |
89
|
3 |
|
$changed = true; |
90
|
3 |
|
} |
91
|
3 |
|
} |
92
|
3 |
|
} |
93
|
3 |
|
return $changed; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.