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 registry with the ability to write and read data. |
12
|
|
|
*/ |
13
|
|
|
class Registry |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name of the registry. |
18
|
|
|
* |
19
|
|
|
* @var null|string |
20
|
|
|
*/ |
21
|
|
|
protected $registryName = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Registry. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $registry = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets the input data. |
32
|
|
|
* |
33
|
|
|
* @param null|string $registryName The name of the registry. |
34
|
|
|
*/ |
35
|
8 |
|
public function __construct($registryName = null) |
36
|
|
|
{ |
37
|
8 |
|
$this->setRegistryName($registryName); |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets the name of the registry. |
42
|
|
|
* Returns the current object. |
43
|
|
|
* |
44
|
|
|
* @param string $registryName The name of the registry. |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
8 |
|
private function setRegistryName($registryName) |
49
|
|
|
{ |
50
|
8 |
|
$this->registryName = $registryName; |
51
|
8 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the value in the registry by key. |
56
|
|
|
* Returns the current object. |
57
|
|
|
* |
58
|
|
|
* @param mixed $value Value in the registry. |
59
|
|
|
* @param mixed $key Key in the registry. (If you do not specify, something then PHP is assigned.) |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
6 |
|
public function set($value, $key = null) |
64
|
|
|
{ |
65
|
6 |
|
if (is_null($key)) { |
66
|
3 |
|
$this->registry[] = $value; |
67
|
|
|
} else { |
68
|
6 |
|
$this->registry[$key] = $value; |
69
|
|
|
} |
70
|
6 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the value by key from the registry. |
75
|
|
|
* |
76
|
|
|
* @param mixed $key Key in the registry. |
77
|
|
|
* @param mixed $default The default value. |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
4 |
|
public function get($key, $default = null) |
82
|
|
|
{ |
83
|
4 |
|
if ($this->has($key)) { |
84
|
3 |
|
return $this->registry[$key]; |
85
|
|
|
} |
86
|
1 |
|
return $default; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the status of record availability in the registry by key. |
91
|
|
|
* |
92
|
|
|
* @param mixed $key Key in the registry. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
5 |
|
public function has($key) |
97
|
|
|
{ |
98
|
5 |
|
return isset($this->registry[$key]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete an record from the registry by key. |
103
|
|
|
* Returns the delete status. |
104
|
|
|
* |
105
|
|
|
* @param mixed $key Key in the registry. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
1 |
|
public function delete($key) |
110
|
|
|
{ |
111
|
1 |
|
if ($this->has($key)) { |
112
|
1 |
|
unset($this->registry[$key]); |
113
|
1 |
|
return true; |
114
|
|
|
} |
115
|
1 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets the number of records in the registry. |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
1 |
|
public function count() |
124
|
|
|
{ |
125
|
1 |
|
return count($this->toArray()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Gets the registry as an array. |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
2 |
|
public function toArray() |
134
|
|
|
{ |
135
|
2 |
|
return $this->registry; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets the name of the registry. |
140
|
|
|
* |
141
|
|
|
* @return null|string |
142
|
|
|
*/ |
143
|
1 |
|
public function getRegistryName() |
144
|
|
|
{ |
145
|
1 |
|
return $this->registryName; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|