1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections\Traits; |
4
|
|
|
|
5
|
|
|
use Collections\Pair; |
6
|
|
|
|
7
|
|
|
trait MapLikeTrait |
8
|
|
|
{ |
9
|
|
|
use ConstMapLikeTrait, |
10
|
|
|
CommonMutableContainerTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* identical to at, implemented for ArrayAccess |
14
|
|
|
*/ |
15
|
|
|
public function offsetGet($offset) |
16
|
|
|
{ |
17
|
|
|
return $this->at($offset); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function offsetSet($offset, $value) |
21
|
|
|
{ |
22
|
|
|
if (is_null($offset)) { |
23
|
|
|
$this->add($value); |
24
|
|
|
} else { |
25
|
|
|
$this->set($offset, $value); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function offsetUnset($offset) |
30
|
|
|
{ |
31
|
|
|
$this->removeKey($offset); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function set($key, $value) |
38
|
|
|
{ |
39
|
|
|
$this->container[$key] = $value; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function setAll($items) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->validateTraversable($items); |
50
|
|
|
|
51
|
|
|
foreach ($items as $key => $item) { |
52
|
|
|
if (is_array($item)) { |
53
|
|
|
$item = new static($item); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
$this->set($key, $item); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function add($pair) |
65
|
|
|
{ |
66
|
|
|
if (!($pair instanceof Pair)) { |
67
|
|
|
throw new \InvalidArgumentException('Parameter must be an instance of Pair'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
list($key, $value) = $pair; |
71
|
|
|
|
72
|
|
|
$this->validateKeyExists($key); |
73
|
|
|
$this->set($key, $value); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function removeKey($key) |
82
|
|
|
{ |
83
|
|
|
$this->validateKeyDoesNotExists($key); |
84
|
|
|
|
85
|
|
|
unset($this->container[$key]); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function remove($element) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$key = array_search($element, $this->container); |
96
|
|
|
|
97
|
|
|
if (false === $key) { |
98
|
|
|
throw new \OutOfBoundsException('No element found in the collection'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->removeKey($key); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function each(callable $callable) |
111
|
|
|
{ |
112
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
113
|
|
|
$callable($v, $k); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.