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
|
|
|
/** |
14
|
|
|
* @param array $items |
15
|
|
|
*/ |
16
|
|
|
public function setItems($items) |
17
|
|
|
{ |
18
|
|
|
$this->items = $items; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
2 |
|
public function add($element, $key = null) |
25
|
|
|
{ |
26
|
2 |
|
if ($key == null) { |
27
|
2 |
|
$this->items[] = $element; |
28
|
2 |
|
return; |
29
|
|
|
} |
30
|
|
|
$this->set($key, $element); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $id |
35
|
|
|
* @param mixed $value |
36
|
|
|
*/ |
37
|
3 |
|
public function set($id, $value) |
38
|
|
|
{ |
39
|
3 |
|
$this->items[$id] = $value; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a parameter by name. |
45
|
|
|
* |
46
|
|
|
* @param string $key The key |
47
|
|
|
* @param mixed $default The default value if the parameter key does not exist |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
1 |
|
public function get($key, $default = null) |
52
|
|
|
{ |
53
|
1 |
|
return array_key_exists($key, $this->items) ? $this->items[$key] : $default; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return boolean |
58
|
|
|
* @param string $key |
59
|
|
|
*/ |
60
|
|
|
public function has($key) |
61
|
|
|
{ |
62
|
|
|
return isset($this->items[$key]) || array_key_exists($key, $this->items); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $key |
67
|
|
|
* @return bool |
68
|
|
|
* @deprecated Use ->has($key) instead |
69
|
|
|
*/ |
70
|
|
|
public function exists($key) |
71
|
|
|
{ |
72
|
|
|
return $this->has($key); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the parameters. |
78
|
|
|
* |
79
|
|
|
* @return array An array of parameters |
80
|
|
|
*/ |
81
|
1 |
|
public function all() |
82
|
|
|
{ |
83
|
1 |
|
return $this->items; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the parameter keys. |
88
|
|
|
* |
89
|
|
|
* @return array An array of parameter keys |
90
|
|
|
*/ |
91
|
|
|
public function keys() |
92
|
|
|
{ |
93
|
|
|
return array_keys($this->items); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the parameter values. |
98
|
|
|
* |
99
|
|
|
* @return array An array of parameter values |
100
|
|
|
*/ |
101
|
|
|
public function values() |
102
|
|
|
{ |
103
|
|
|
return array_values($this->items); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $key |
109
|
|
|
* @return null |
110
|
|
|
*/ |
111
|
1 |
|
public function unset($key) |
112
|
|
|
{ |
113
|
1 |
|
if (!isset($this->items[$key]) && !array_key_exists($key, $this->items)) { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
1 |
|
$removed = $this->items[$key]; |
117
|
1 |
|
unset($this->items[$key]); |
118
|
1 |
|
return $removed; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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: