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