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
|
|
|
* @param \Nip\Records\AbstractModels\Record $element |
23
|
|
|
*/ |
24
|
2 |
|
public function add($element, $key = null) |
25
|
|
|
{ |
26
|
2 |
|
if ($key == null) { |
27
|
2 |
|
$this->items[] = $element; |
28
|
|
|
|
29
|
2 |
|
return; |
30
|
|
|
} |
31
|
|
|
$this->set($key, $element); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $id |
36
|
|
|
* @param mixed $value |
37
|
|
|
*/ |
38
|
4 |
|
public function set($id, $value) |
39
|
|
|
{ |
40
|
4 |
|
$this->items[$id] = $value; |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns a parameter by name. |
46
|
|
|
* |
47
|
|
|
* @param string $key The key |
48
|
|
|
* @param mixed $default The default value if the parameter key does not exist |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
2 |
|
public function get($key, $default = null) |
53
|
|
|
{ |
54
|
2 |
|
return array_key_exists($key, $this->items) ? $this->items[$key] : $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return boolean |
59
|
|
|
* @param string $key |
60
|
|
|
*/ |
61
|
|
|
public function has($key) |
62
|
|
|
{ |
63
|
|
|
return isset($this->items[$key]) || array_key_exists($key, $this->items); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $key |
68
|
|
|
* @return bool |
69
|
|
|
* @deprecated Use ->has($key) instead |
70
|
|
|
*/ |
71
|
|
|
public function exists($key) |
72
|
|
|
{ |
73
|
|
|
return $this->has($key); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the parameters. |
79
|
|
|
* |
80
|
|
|
* @return array An array of parameters |
81
|
|
|
*/ |
82
|
|
|
public function all() |
83
|
|
|
{ |
84
|
|
|
return $this->items; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the parameter keys. |
89
|
|
|
* |
90
|
|
|
* @return array An array of parameter keys |
91
|
|
|
*/ |
92
|
1 |
|
public function keys() |
93
|
|
|
{ |
94
|
1 |
|
return array_keys($this->items); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the parameter values. |
99
|
|
|
* |
100
|
|
|
* @return array An array of parameter values |
101
|
|
|
*/ |
102
|
|
|
public function values() |
103
|
|
|
{ |
104
|
|
|
return array_values($this->items); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $key |
110
|
|
|
* @return null |
111
|
|
|
*/ |
112
|
1 |
|
public function unset($key) |
113
|
|
|
{ |
114
|
1 |
|
if (!isset($this->items[$key]) && !array_key_exists($key, $this->items)) { |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
1 |
|
$removed = $this->items[$key]; |
118
|
1 |
|
unset($this->items[$key]); |
119
|
|
|
|
120
|
1 |
|
return $removed; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Alias of unshift |
125
|
|
|
* |
126
|
|
|
* @param mixed $value |
127
|
|
|
* @param mixed $key |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function prepend($value, $key = null) |
131
|
|
|
{ |
132
|
|
|
return $this->unshift($value, $key); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Push an item onto the beginning of the collection. |
137
|
|
|
* |
138
|
|
|
* @param $value |
139
|
|
|
* @param null $key |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function unshift($value, $key = null) |
143
|
|
|
{ |
144
|
|
|
if (is_null($key)) { |
145
|
|
|
array_unshift($this->items, $value); |
146
|
|
|
} else { |
147
|
|
|
$this->items = [$key => $value] + $this->items; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: