1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Collection; |
8
|
|
|
|
9
|
|
|
use ArrayIterator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ArraySet |
13
|
|
|
* |
14
|
|
|
* @author Nate Brunette <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ArraySet extends AbstractSet |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The set elements |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $elements = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param array $elements |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct(array $elements = []) |
31
|
|
|
{ |
32
|
4 |
|
$this->addAllArray($elements); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns true if the collection contains element |
37
|
|
|
* |
38
|
|
|
* @param mixed $element |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
33 |
|
public function contains($element): bool |
42
|
|
|
{ |
43
|
33 |
|
return in_array($element, $this->elements, true); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Ensure the element exists in the collection |
48
|
|
|
* |
49
|
|
|
* Returns true if the collection can contain duplicates, |
50
|
|
|
* and false if it cannot. |
51
|
|
|
* |
52
|
|
|
* @param mixed $element |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
33 |
|
public function add($element): bool |
56
|
|
|
{ |
57
|
33 |
|
if ($this->contains($element)) { |
58
|
3 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
33 |
|
$this->elements[] = $element; |
62
|
|
|
|
63
|
33 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Removes all elements from a collection |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
1 |
|
public function clear(): void |
72
|
|
|
{ |
73
|
1 |
|
$this->elements = []; |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Removes object if it exists |
78
|
|
|
* |
79
|
|
|
* Returns true if the element was removed |
80
|
|
|
* |
81
|
|
|
* @param mixed $element |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
8 |
View Code Duplication |
public function remove($element): bool |
|
|
|
|
85
|
|
|
{ |
86
|
8 |
|
$index = array_search($element, $this->elements, true); |
87
|
|
|
|
88
|
8 |
|
if (false === $index) { |
89
|
2 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
array_splice($this->elements, $index, 1); |
93
|
|
|
|
94
|
7 |
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns an array of all elements in the collection |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
33 |
|
public function toArray(): array |
103
|
|
|
{ |
104
|
33 |
|
return $this->elements; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Retrieve an external iterator |
109
|
|
|
* |
110
|
|
|
* @return ArrayIterator |
111
|
|
|
*/ |
112
|
8 |
|
public function getIterator(): ArrayIterator |
113
|
|
|
{ |
114
|
8 |
|
return new ArrayIterator($this->toArray()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.