1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/Util |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license https://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\Util; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Storage of data in the form of a list. |
12
|
|
|
*/ |
13
|
|
|
class StorageList |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Data to initialize the list. |
18
|
|
|
* |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
private $initData; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* List. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $list; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets incoming values. |
32
|
|
|
* |
33
|
|
|
* @param mixed $initData Data to initialize the list. |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct($initData = array()) |
36
|
|
|
{ |
37
|
2 |
|
$this->setInitData($initData); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets data to initialize the list. |
42
|
|
|
* |
43
|
|
|
* @param mixed $initData Data for initialization. |
44
|
|
|
* |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
2 |
|
private function setInitData($initData) |
48
|
|
|
{ |
49
|
2 |
|
$this->initData = $initData; |
50
|
2 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets data to initialize the list. |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
2 |
|
protected function getInitData() |
59
|
|
|
{ |
60
|
2 |
|
return $this->initData; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets value of by key name. |
65
|
|
|
* |
66
|
|
|
* @param string $keyName Key name. |
67
|
|
|
* @param mixed $defaultValue Default value. |
68
|
|
|
* |
69
|
|
|
* @return string|mixed |
70
|
|
|
*/ |
71
|
1 |
|
public function get($keyName, $defaultValue = null) |
72
|
|
|
{ |
73
|
1 |
|
$this->initList(); |
74
|
1 |
|
$keyName = strtolower($keyName); |
75
|
1 |
|
if (empty($this->list[$keyName])) { |
76
|
1 |
|
return $defaultValue; |
77
|
|
|
} |
78
|
1 |
|
return $this->list[$keyName]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the container as an array. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
public function toArray() |
87
|
|
|
{ |
88
|
1 |
|
$this->initList(); |
89
|
1 |
|
return $this->list; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the list. |
94
|
|
|
* |
95
|
|
|
* @param array $list List. |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
2 |
|
protected function setList(array $list) |
100
|
|
|
{ |
101
|
2 |
|
$this->list = $list; |
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Initializes and sets the list. |
107
|
|
|
*/ |
108
|
2 |
|
protected function initList() |
109
|
|
|
{ |
110
|
2 |
|
if (empty($this->list)) { |
111
|
2 |
|
$this->setList($this->getInitData()); |
112
|
|
|
} |
113
|
2 |
|
} |
114
|
|
|
} |
115
|
|
|
|