1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Logikos\Util; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This is a php version of \Phalcon\Registry |
7
|
|
|
* NOTICE: \Phalcon\Registry will be much faster than this class and you are encouraged to use it |
8
|
|
|
* |
9
|
|
|
*<code> |
10
|
|
|
* $registry = new \Phalcon\Registry(); |
11
|
|
|
* |
12
|
|
|
* // Set value |
13
|
|
|
* $registry->something = 'something'; |
14
|
|
|
* $registry['something'] = 'something'; |
15
|
|
|
* |
16
|
|
|
* // Get value |
17
|
|
|
* $value = $registry->something; |
18
|
|
|
* $value = $registry['something']; |
19
|
|
|
* |
20
|
|
|
* // Check if the key exists |
21
|
|
|
* $exists = isset($registry->something); |
22
|
|
|
* $exists = isset($registry['something']); |
23
|
|
|
* |
24
|
|
|
* // Unset |
25
|
|
|
* unset($registry->something); |
26
|
|
|
* unset($registry['something']); |
27
|
|
|
*</code> |
28
|
|
|
*/ |
29
|
|
|
class Registry implements \ArrayAccess, \Countable, \Iterator { |
30
|
|
|
private $values; |
31
|
|
|
|
32
|
75 |
|
public function __construct(array $data = []) { |
33
|
75 |
|
$this->values = $data; |
34
|
75 |
|
} |
35
|
|
|
|
36
|
|
|
# Countable |
37
|
5 |
|
public function count() { |
38
|
5 |
|
return count($this->values); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $offset |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
1 |
|
public function isset($offset) { |
46
|
1 |
|
return isset($this->values[$offset]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
# ArrayAccess |
50
|
|
|
public function offsetGet($offset) { return $this->requireOffset($offset); } |
51
|
|
|
public function offsetExists($offset) { return array_key_exists($offset, $this->values); } |
52
|
|
|
public function offsetSet($offset, $val) { $this->values[$offset] = $val; } |
53
|
|
|
public function offsetUnset($offset) { unset($this->values[$offset]); } |
54
|
|
|
|
55
|
|
|
# Iterator |
56
|
|
|
public function rewind() { return reset($this->values); } |
57
|
|
|
public function key() { return key($this->values); } |
58
|
|
|
public function current() { return current($this->values); } |
59
|
|
|
public function next() { return next($this->values); } |
60
|
|
|
public function valid() { return key($this->values) !== null; } |
61
|
|
|
|
62
|
|
|
# Magic Property Access |
63
|
|
|
public function __set($offset, $value) { $this->offsetSet($offset, $value); } |
64
|
|
|
public function __unset($offset) { $this->offsetUnset($offset); } |
65
|
|
|
public function __get($offset) { return $this->offsetGet($offset); } |
66
|
|
|
public function __isset($offset) { return $this->offsetExists($offset); } |
67
|
|
|
|
68
|
29 |
|
protected function requireOffset($offset) { |
69
|
29 |
|
if (!$this->offsetExists($offset)) |
70
|
3 |
|
throw new \OutOfBoundsException("offset '{$offset}' does not exist"); |
71
|
|
|
|
72
|
26 |
|
return $this->values[$offset]; |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
protected function rawValues() { |
76
|
9 |
|
return $this->values; |
77
|
|
|
} |
78
|
|
|
} |