1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the GestPayWS library. |
4
|
|
|
* |
5
|
|
|
* (c) Manuel Dalla Lana <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace EndelWar\GestPayWS; |
12
|
|
|
|
13
|
|
|
trait ArrayAccessTrait |
14
|
|
|
{ |
15
|
|
|
protected $data = []; |
16
|
|
|
protected $parametersName = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $key |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @throws \InvalidArgumentException |
22
|
|
|
*/ |
23
|
|
|
public function set($key, $value) |
24
|
|
|
{ |
25
|
|
|
if (!in_array($key, $this->parametersName, true)) { |
26
|
|
|
throw new \InvalidArgumentException(sprintf('%s is not a valid parameter name.', $key)); |
27
|
|
|
} |
28
|
|
|
$this->data[$key] = $value; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $key |
33
|
|
|
* |
34
|
|
|
* @return mixed|null the value at the specified index or null |
35
|
|
|
*/ |
36
|
|
|
public function get($key) |
37
|
|
|
{ |
38
|
|
|
if (array_key_exists($key, $this->data)) { |
39
|
|
|
return $this->data[$key]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $data |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function fromArray($data) |
50
|
|
|
{ |
51
|
|
|
foreach ($data as $key => $value) { |
52
|
|
|
$this->set($key, $value); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function toArray() |
60
|
|
|
{ |
61
|
|
|
return $this->data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns whether the requested index exists |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function offsetExists($key) |
72
|
|
|
{ |
73
|
|
|
return array_key_exists($key, $this->data); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the value at the specified index |
78
|
|
|
* |
79
|
|
|
* @param string $key the index with the value |
80
|
|
|
* |
81
|
|
|
* @return mixed|null the value at the specified index or null |
82
|
|
|
*/ |
83
|
|
|
public function offsetGet($key) |
84
|
|
|
{ |
85
|
|
|
return $this->get($key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets the value at the specified index to $value |
90
|
|
|
* |
91
|
|
|
* @param string $key the index being set |
92
|
|
|
* @param mixed $value The new value for the index |
93
|
|
|
*/ |
94
|
|
|
public function offsetSet($key, $value) |
95
|
|
|
{ |
96
|
|
|
$this->set($key, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Unsets the value at the specified index |
101
|
|
|
* |
102
|
|
|
* @param string $key |
103
|
|
|
*/ |
104
|
|
|
public function offsetUnset($key) |
105
|
|
|
{ |
106
|
|
|
unset($this->data[$key]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Magic getter, calls getXXX if exists. |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function __get($key) |
117
|
|
|
{ |
118
|
|
|
$getter = 'get' . $this->classify($key); |
119
|
|
|
if (method_exists($this, $getter)) { |
120
|
|
|
return $this->$getter(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->get($key); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Magic setter, calls setXXX if exists. |
128
|
|
|
* @param $key |
129
|
|
|
* @param $value |
130
|
|
|
* |
131
|
|
|
* @throws \InvalidArgumentException |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function __set($key, $value) |
135
|
|
|
{ |
136
|
|
|
$setter = 'set' . $this->classify($key); |
137
|
|
|
if (method_exists($this, $setter)) { |
138
|
|
|
return $this->$setter($value); |
139
|
|
|
} |
140
|
|
|
$this->set($key, $value); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns whether the requested index exists |
145
|
|
|
* |
146
|
|
|
* @param string $key |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function __isset($key) |
151
|
|
|
{ |
152
|
|
|
return isset($this->data[$key]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Unsets the value at the specified index |
157
|
|
|
* |
158
|
|
|
* @param string $key |
159
|
|
|
*/ |
160
|
|
|
public function __unset($key) |
161
|
|
|
{ |
162
|
|
|
unset($this->data[$key]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Converts a string into a CamelCase word. |
167
|
|
|
* |
168
|
|
|
* @param string $string the string to classify |
169
|
|
|
* |
170
|
|
|
* @return string the classified word |
171
|
|
|
*/ |
172
|
|
|
private function classify($string) |
173
|
|
|
{ |
174
|
|
|
return str_replace(' ', '', ucwords(strtr($string, '_-', ' '))); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|