|
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
|
54 |
|
public function __construct(array $data = []) { |
|
33
|
54 |
|
$this->values = $data; |
|
34
|
54 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
# Countable |
|
37
|
4 |
|
public function count() { |
|
38
|
4 |
|
return count($this->values); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
# ArrayAccess |
|
42
|
|
|
public function offsetGet($offset) { return $this->requireOffset($offset); } |
|
43
|
|
|
public function offsetExists($offset) { return isset($this->values[$offset]); } |
|
44
|
|
|
public function offsetSet($offset, $val) { $this->values[$offset] = $val; } |
|
45
|
|
|
public function offsetUnset($offset) { unset($this->values[$offset]); } |
|
46
|
|
|
|
|
47
|
|
|
# Iterator |
|
48
|
|
|
public function rewind() { return reset($this->values); } |
|
49
|
|
|
public function key() { return key($this->values); } |
|
50
|
|
|
public function current() { return current($this->values); } |
|
51
|
|
|
public function next() { return next($this->values); } |
|
52
|
|
|
public function valid() { return key($this->values) !== null; } |
|
53
|
|
|
|
|
54
|
|
|
# Magic Property Access |
|
55
|
|
|
public function __set($offset, $value) { $this->offsetSet($offset, $value); } |
|
56
|
|
|
public function __unset($offset) { $this->offsetUnset($offset); } |
|
57
|
|
|
public function __get($offset) { return $this->offsetGet($offset); } |
|
58
|
|
|
public function __isset($offset) { return $this->offsetExists($offset); } |
|
59
|
|
|
|
|
60
|
20 |
|
protected function requireOffset($offset) { |
|
61
|
20 |
|
if (!$this->offsetExists($offset)) |
|
62
|
2 |
|
throw new \OutOfBoundsException("offset '{$offset}' does not exist"); |
|
63
|
|
|
|
|
64
|
18 |
|
return $this->values[$offset]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
8 |
|
protected function rawValues() { |
|
68
|
8 |
|
return $this->values; |
|
69
|
|
|
} |
|
70
|
|
|
} |