1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections\Traits; |
4
|
|
|
|
5
|
|
|
use Collections\Enumerable; |
6
|
|
|
|
7
|
|
|
trait ConstMapLikeTrait |
8
|
|
|
{ |
9
|
|
|
use CommonContainerMethodsTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $container; |
15
|
|
|
|
16
|
|
View Code Duplication |
protected function init($it = null) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
if (null !== $it) { |
19
|
|
|
$this->validateTraversable($it); |
20
|
|
|
|
21
|
|
|
$coll = []; |
22
|
|
|
foreach ($it as $key => $value) { |
23
|
|
|
if (is_array($value)) { |
24
|
|
|
$value = new static($value); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
$coll[$key] = $value; |
27
|
|
|
} |
28
|
|
|
$this->container = $coll; |
29
|
|
|
} else { |
30
|
|
|
$this->container = []; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function isEmpty() |
38
|
|
|
{ |
39
|
|
|
return $this->count() === 0; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function count() |
46
|
|
|
{ |
47
|
|
|
return count($this->container); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function at($key) |
54
|
|
|
{ |
55
|
|
|
$this->validateKeyDoesNotExists($key); |
56
|
|
|
|
57
|
|
|
return $this->container[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function get($index) |
64
|
|
|
{ |
65
|
|
|
if ($this->containsKey($index)) { |
66
|
|
|
return $this->container[$index]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function containsKey($key) |
76
|
|
|
{ |
77
|
|
|
return array_key_exists($key, $this->container); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function contains($element) |
84
|
|
|
{ |
85
|
|
|
return in_array($element, $this->container, true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function offsetExists($offset) |
93
|
|
|
{ |
94
|
|
|
return $this->containsKey($offset) && $this->at($offset) !== null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns an array containing the values from this VectorLike. |
99
|
|
|
*/ |
100
|
|
|
public function toArray() |
101
|
|
|
{ |
102
|
|
|
$arr = []; |
103
|
|
|
foreach ($this as $key => $value) { |
|
|
|
|
104
|
|
|
if ($value instanceof Enumerable) { |
105
|
|
|
$arr[$key] = $value->toArray(); |
106
|
|
|
} else { |
107
|
|
|
$arr[$key] = $value; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $arr; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function each(callable $callable) |
119
|
|
|
{ |
120
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
121
|
|
|
$callable($v, $k); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.