1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections; |
4
|
|
|
|
5
|
|
|
use Collections\Exception\ElementAlreadyExists; |
6
|
|
|
use Collections\Exception\UnsuportedException; |
7
|
|
|
use Collections\Iterator\SetIterator; |
8
|
|
|
use Collections\Traits\GuardTrait; |
9
|
|
|
use Collections\Traits\StrictKeyedIterableTrait; |
10
|
|
|
|
11
|
|
|
class Set extends AbstractConstCollectionArray implements SetInterface, \ArrayAccess |
12
|
|
|
{ |
13
|
|
|
use GuardTrait, |
14
|
|
|
StrictKeyedIterableTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
19
|
|
|
public function contains($item) |
20
|
|
|
{ |
21
|
|
|
return in_array($item, $this->container, true); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
*/ |
27
|
|
|
public function addAll($items) |
28
|
|
|
{ |
29
|
|
|
$this->validateTraversable($items); |
30
|
|
|
|
31
|
|
|
foreach ($items as $value) { |
32
|
|
|
$this->add($value); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function remove($element) |
40
|
|
|
{ |
41
|
|
|
$key = array_search($element, $this->container, true); |
42
|
|
|
unset($this->container[$key]); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function getIterator() |
51
|
|
|
{ |
52
|
|
|
return new SetIterator($this->container); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
|
|
public function add($item) |
59
|
|
|
{ |
60
|
|
|
if ($this->contains($item)) { |
61
|
|
|
throw ElementAlreadyExists::duplicatedElement($item); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->container[] = $item; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function removeAll(Iterable $iterable) |
73
|
|
|
{ |
74
|
|
|
$iterable->each(function ($item) { |
75
|
|
|
if ($this->contains($item)) { |
76
|
|
|
$this->remove($item); |
77
|
|
|
} |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function offsetExists($offset) |
87
|
|
|
{ |
88
|
|
|
return $this->getIterator()->offsetExists($offset); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function offsetGet($offset) |
95
|
|
|
{ |
96
|
|
|
throw UnsuportedException::unsupportedGet($this); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function offsetSet($offset, $value) |
103
|
|
|
{ |
104
|
|
|
if (!is_null($offset)) { |
105
|
|
|
throw UnsuportedException::unsupportedSetKey($this); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->add($value); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function offsetUnset($offset) |
115
|
|
|
{ |
116
|
|
|
throw UnsuportedException::unsupportedUnset($this); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function concat($iterable) |
124
|
|
|
{ |
125
|
|
|
$this->validateTraversable($iterable); |
126
|
|
|
|
127
|
|
|
$concatenated = $this->concatRecurse($this, $iterable); |
128
|
|
|
$this->clear(); |
129
|
|
|
$this->addAll($concatenated); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|