|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Collections\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Collections\Exception\InvalidOperationException; |
|
6
|
|
|
|
|
7
|
|
|
trait VectorLikeTrait |
|
8
|
|
|
{ |
|
9
|
|
|
use ConstVectorLikeTrait, |
|
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
|
|
|
throw InvalidOperationException::unsupportedUnset($this); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Stores a value into the Vector with the specified key, overwriting the |
|
36
|
|
|
* previous value associated with the key. If the key is not present, |
|
37
|
|
|
* an exception is thrown. "$vec->set($k,$v)" is semantically equivalent |
|
38
|
|
|
* to "$vec[$k] = $v" (except that set() returns the Vector). |
|
39
|
|
|
* |
|
40
|
|
|
* @param $key |
|
41
|
|
|
* @param $value |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function set($key, $value) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->validateKeyType($key); |
|
47
|
|
|
$this->container[$key] = $value; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function setAll($items) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$this->validateTraversable($items); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($items as $key => $item) { |
|
60
|
|
|
if (is_array($item)) { |
|
61
|
|
|
$item = new static($item); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
$this->set($key, $item); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function add($item) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->container[] = $item; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function removeKey($key) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->validateKeyType($key); |
|
85
|
|
|
$this->validateKeyBounds($key); |
|
86
|
|
|
|
|
87
|
|
|
array_splice($this->container, $key, 1); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function splice($offset, $length = null) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!is_int($offset)) { |
|
98
|
|
|
throw new \InvalidArgumentException('Parameter offset must be an integer'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (!is_null($length) && !is_int($length)) { |
|
102
|
|
|
throw new \InvalidArgumentException('Parameter len must be null or an integer'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$removed = is_null($length) ? array_splice($this->container, $offset) : |
|
|
|
|
|
|
106
|
|
|
array_splice($this->container, $offset, $len); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
// if (count($removed) > 0) { |
|
|
|
|
|
|
109
|
|
|
// $this->hacklib_expireAllIterators(); |
|
110
|
|
|
// } |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.