1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Collections\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AccessMethodsTrait |
7
|
|
|
* @package Nip\Collections\Traits |
8
|
|
|
*/ |
9
|
|
|
trait AccessMethodsTrait |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param array $items |
14
|
|
|
*/ |
15
|
|
|
public function setItems($items) |
16
|
|
|
{ |
17
|
|
|
$this->items = $items; |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
27 |
|
public function add($element, $key = null) |
24
|
|
|
{ |
25
|
27 |
|
$this->set($key, $element); |
26
|
13 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $key |
30
|
|
|
* @param mixed $value |
31
|
|
|
*/ |
32
|
29 |
|
public function set($key, $value) |
33
|
|
|
{ |
34
|
29 |
|
$this->offsetSet($key, $value); |
|
|
|
|
35
|
15 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns a parameter by name. |
39
|
|
|
* |
40
|
|
|
* @param string $key The key |
41
|
|
|
* @param mixed $default The default value if the parameter key does not exist |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
2 |
|
public function get($key, $default = null) |
46
|
|
|
{ |
47
|
2 |
|
if ($this->offsetExists($key)) { |
|
|
|
|
48
|
2 |
|
return $this->offsetGet($key); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return value($default); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return boolean |
56
|
|
|
* @param string $key |
57
|
|
|
*/ |
58
|
1 |
|
public function has($key) |
59
|
|
|
{ |
60
|
1 |
|
$keys = is_array($key) ? $key : func_get_args(); |
61
|
|
|
|
62
|
1 |
|
foreach ($keys as $value) { |
63
|
1 |
|
if (!$this->offsetExists($value)) { |
64
|
1 |
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the parameters. |
73
|
|
|
* |
74
|
|
|
* @return array An array of parameters |
75
|
|
|
*/ |
76
|
3 |
|
public function all() |
77
|
|
|
{ |
78
|
3 |
|
return $this->items; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the parameter keys. |
83
|
|
|
* |
84
|
|
|
* @return array An array of parameter keys |
85
|
|
|
*/ |
86
|
2 |
|
public function keys() |
87
|
|
|
{ |
88
|
2 |
|
return array_keys($this->items); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the parameter values. |
93
|
|
|
* |
94
|
|
|
* @return array An array of parameter values |
95
|
|
|
*/ |
96
|
1 |
|
public function values() |
97
|
|
|
{ |
98
|
1 |
|
return array_values($this->items); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Remove an item from the collection by key. |
103
|
|
|
* |
104
|
|
|
* @param string|array $keys |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function forget($keys) |
108
|
|
|
{ |
109
|
|
|
foreach ((array)$keys as $key) { |
110
|
|
|
$this->offsetUnset($key); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $key |
118
|
|
|
* @return null |
119
|
|
|
*/ |
120
|
|
|
public function unset($key) |
121
|
|
|
{ |
122
|
|
|
$this->offsetUnset($key); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Alias of unshift |
127
|
|
|
* |
128
|
|
|
* @param mixed $value |
129
|
|
|
* @param mixed $key |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function prepend($value, $key = null) |
133
|
|
|
{ |
134
|
|
|
return $this->unshift($value, $key); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Push an item onto the beginning of the collection. |
139
|
|
|
* |
140
|
|
|
* @param $value |
141
|
|
|
* @param null $key |
|
|
|
|
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
14 |
|
public function unshift($value, $key = null) |
145
|
|
|
{ |
146
|
14 |
|
if (is_null($key)) { |
|
|
|
|
147
|
14 |
|
array_unshift($this->items, $value); |
148
|
|
|
} else { |
149
|
1 |
|
$this->items = [$key => $value] + $this->items; |
150
|
|
|
} |
151
|
|
|
|
152
|
14 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Push one or more items onto the end of the collection. |
157
|
|
|
* |
158
|
|
|
* @param mixed $values [optional] |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
27 |
|
public function push(...$values) |
162
|
|
|
{ |
163
|
27 |
|
foreach ($values as $value) { |
164
|
27 |
|
$this->add($value); |
165
|
|
|
} |
166
|
|
|
|
167
|
13 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|