|
24 | 24 | * @copyright 2010-2017 Horde LLC |
25 | 25 | * @license http://www.horde.org/licenses/bsd BSD |
26 | 26 | */ |
| 27 | + |
27 | 28 | class Horde_Argv_Values implements IteratorAggregate, ArrayAccess, Countable |
28 | 29 | { |
29 | | - public function __construct($defaults = array()) |
| 30 | + // Array to store the dynamic attributes |
| 31 | + private $data = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * Summary of __construct |
| 35 | + * @param mixed $defaults |
| 36 | + */ |
| 37 | + public function __construct($defaults = []) |
30 | 38 | { |
31 | 39 | foreach ($defaults as $attr => $val) { |
32 | | - $this->$attr = $val; |
| 40 | + $this->data[$attr] = $val; |
33 | 41 | } |
34 | 42 | } |
35 | 43 |
|
36 | | - public function __toString() |
| 44 | + /** |
| 45 | + * __set: Set a value |
| 46 | + * |
| 47 | + * @param string $attr The name of the attribute |
| 48 | + * @param mixed $value The content of the attribute |
| 49 | + */ |
| 50 | + public function __set($attr, $value): void |
| 51 | + { |
| 52 | + $this->data[$attr] = $value; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * __get: Returns a value of an attribute |
| 57 | + * |
| 58 | + * @param string $attr The name of the attribute |
| 59 | + * @return mixed The value of the attribute |
| 60 | + */ |
| 61 | + public function &__get($attr) |
| 62 | + { |
| 63 | + return $this->data[$attr]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * __isset: check if an attribute is set |
| 68 | + * |
| 69 | + * @param string $attr The name of the attribute |
| 70 | + * @return bool True, when the attribute exists/ is set else false |
| 71 | + */ |
| 72 | + public function __isset($attr): bool |
| 73 | + { |
| 74 | + return isset($this->data[$attr]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * __unset: removes a attribute |
| 79 | + * |
| 80 | + * @param string $attr The name of the attribute |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function __unset($attr): void |
| 84 | + { |
| 85 | + unset($this->data[$attr]); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * __toString: The whole content as a string |
| 90 | + * |
| 91 | + * @return string The content as a string |
| 92 | + */ |
| 93 | + public function __toString(): string |
37 | 94 | { |
38 | | - $str = array(); |
39 | | - foreach ($this as $attr => $val) { |
| 95 | + $str = []; |
| 96 | + foreach ($this->data as $attr => $val) { |
40 | 97 | $str[] = $attr . ': ' . (string)$val; |
41 | 98 | } |
42 | 99 | return implode(', ', $str); |
43 | 100 | } |
44 | 101 |
|
45 | | - public function offsetExists($attr) |
| 102 | + /** |
| 103 | + * Summary of offsetExists |
| 104 | + * @param mixed $attr |
| 105 | + * @return bool |
| 106 | + */ |
| 107 | + public function offsetExists($attr): bool |
46 | 108 | { |
47 | | - return isset($this->$attr) && !is_null($this->$attr); |
| 109 | + return isset($this->data[$attr]) && !$this->data[$attr] === null; |
48 | 110 | } |
49 | 111 |
|
| 112 | + /** |
| 113 | + * Summary of offsetGet |
| 114 | + * @param mixed $attr |
| 115 | + * @return mixed |
| 116 | + */ |
50 | 117 | public function offsetGet($attr) |
51 | 118 | { |
52 | | - return $this->$attr; |
| 119 | + return $this->data[$attr] ?? null; |
53 | 120 | } |
54 | 121 |
|
55 | | - public function offsetSet($attr, $val) |
| 122 | + /** |
| 123 | + * Summary of offsetSet |
| 124 | + * @param mixed $attr |
| 125 | + * @param mixed $val |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function offsetSet($attr, $val): void |
56 | 129 | { |
57 | | - $this->$attr = $val; |
| 130 | + $this->data[$attr] = $val; |
58 | 131 | } |
59 | 132 |
|
60 | | - public function offsetUnset($attr) |
| 133 | + /** |
| 134 | + * Summary of offsetUnset |
| 135 | + * @param mixed $attr |
| 136 | + * @return void |
| 137 | + */ |
| 138 | + public function offsetUnset($attr): void |
61 | 139 | { |
62 | | - unset($this->$attr); |
| 140 | + unset($this->data[$attr]); |
63 | 141 | } |
64 | 142 |
|
65 | | - public function getIterator() |
| 143 | + /** |
| 144 | + * Summary of getIterator |
| 145 | + * @return ArrayIterator |
| 146 | + */ |
| 147 | + public function getIterator(): ArrayIterator |
66 | 148 | { |
67 | | - return new ArrayIterator(get_object_vars($this)); |
| 149 | + return new ArrayIterator($this->data); |
68 | 150 | } |
69 | 151 |
|
70 | | - public function count() |
| 152 | + /** |
| 153 | + * Summary of count |
| 154 | + * @return int |
| 155 | + */ |
| 156 | + public function count(): int |
71 | 157 | { |
72 | | - return count(get_object_vars($this)); |
| 158 | + return count(get_object_vars($this->data)); |
73 | 159 | } |
74 | 160 |
|
| 161 | + /** |
| 162 | + * Summary of ensureValue |
| 163 | + * @param mixed $attr |
| 164 | + * @param mixed $value |
| 165 | + * @return mixed |
| 166 | + */ |
75 | 167 | public function ensureValue($attr, $value) |
76 | 168 | { |
77 | | - if (is_null($this->$attr)) { |
78 | | - $this->$attr = $value; |
| 169 | + if ($this->data[$attr] === null) { |
| 170 | + $this->data[$attr] = $value; |
79 | 171 | } |
80 | | - return $this->$attr; |
| 172 | + return $this->data[$attr]; |
81 | 173 | } |
82 | 174 |
|
83 | 175 | } |
0 commit comments