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