|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @category Brownie/Util |
|
4
|
|
|
* @author Brownie <[email protected]> |
|
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Brownie\Util; |
|
9
|
|
|
|
|
10
|
|
|
use Brownie\Util\Exception\UndefinedMethodException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Data storage in the form of an associative array. |
|
14
|
|
|
*/ |
|
15
|
|
|
class StorageArray |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* List of supported fields. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $fields = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sets the incoming values. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $fields Set values list |
|
29
|
|
|
* @param bool $isReinitialize Reinitialization flag. |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function __construct($fields = array(), $isReinitialize = false) |
|
32
|
|
|
{ |
|
33
|
6 |
|
if ($isReinitialize) { |
|
34
|
4 |
|
$this->initFields($fields); |
|
35
|
|
|
} else { |
|
36
|
2 |
|
foreach ($fields as $key => $value) { |
|
37
|
1 |
|
$method = 'set' . ucfirst($key); |
|
38
|
1 |
|
$this->$method($value); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Call processing methods Set and Get. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name Name. |
|
47
|
|
|
* @param mixed $values Value. |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
* |
|
51
|
|
|
* @throws UndefinedMethodException |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public function __call($name, $values) |
|
54
|
|
|
{ |
|
55
|
4 |
|
$method = substr($name, 0, 3); |
|
56
|
4 |
|
$nameField = lcfirst(substr($name, 3)); |
|
57
|
4 |
|
if (!array_key_exists($nameField, $this->toArray())) { |
|
58
|
3 |
|
throw new UndefinedMethodException('Call to undefined method ' . $name); |
|
59
|
|
|
} |
|
60
|
1 |
|
return $this->$method($nameField, $values); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Initialization of supported fields. |
|
65
|
|
|
* Returns the current object. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $fields List of supported fields. |
|
68
|
|
|
* |
|
69
|
|
|
* @return self |
|
70
|
|
|
*/ |
|
71
|
4 |
|
private function initFields(array $fields) |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->fields = $fields; |
|
74
|
4 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets values in the storage. |
|
79
|
|
|
* Returns the current object. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $name Name. |
|
82
|
|
|
* @param mixed $values Value. |
|
83
|
|
|
* |
|
84
|
|
|
* @return self |
|
85
|
|
|
*/ |
|
86
|
1 |
|
private function set($name, $values) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->fields[$name] = $values[0]; |
|
89
|
1 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Gets the values from the storage. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $name Name |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
1 |
|
private function get($name) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->fields[$name]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns the storage as an array. |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
6 |
|
public function toArray() |
|
110
|
|
|
{ |
|
111
|
6 |
|
return $this->fields; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|