1 | <?php |
||
37 | class TypedStruct implements \ArrayAccess, \IteratorAggregate, \Countable |
||
38 | { |
||
39 | /** |
||
40 | * @var TypeProperty[] Definition of the (string) key => (string) type |
||
41 | */ |
||
42 | protected $definitions = []; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $values = []; |
||
48 | |||
49 | /** |
||
50 | * TypedStruct constructor. |
||
51 | * @param string[] $definitions |
||
52 | * @param array $values |
||
53 | */ |
||
54 | 17 | public function __construct(array $definitions, array $values = []) |
|
72 | |||
73 | /** |
||
74 | * @return TypeProperty[] Get an array with all definitions |
||
75 | */ |
||
76 | 2 | public function getDefinitions() |
|
80 | |||
81 | /** |
||
82 | * Retrieve a definition object |
||
83 | * @param string $key |
||
84 | * @return TypeProperty |
||
85 | * @throws \InvalidArgumentException if key is not found |
||
86 | */ |
||
87 | 12 | public function getDefinition($key) |
|
92 | |||
93 | /** |
||
94 | * Set all the values from the array to the local object |
||
95 | * If the key is not found then it is just ignored |
||
96 | * |
||
97 | * @param array $values |
||
98 | */ |
||
99 | 15 | public function setValues(array $values) |
|
107 | |||
108 | /** |
||
109 | * Set the value for a key, this will check that the value is the same type of the definition |
||
110 | * |
||
111 | * @param string $key |
||
112 | * @param mixed $value |
||
113 | * @throws \InvalidArgumentException if key is not found |
||
114 | * @throws TypePropertyException if the value does not match the defined type |
||
115 | */ |
||
116 | 12 | public function set($key, $value) |
|
124 | |||
125 | /** |
||
126 | * Return TRUE if the key exists |
||
127 | * |
||
128 | * @param string $key |
||
129 | * @return bool |
||
130 | */ |
||
131 | 11 | public function exists($key) |
|
135 | |||
136 | /** |
||
137 | * Return the value of a key |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @return mixed |
||
141 | * @throws \InvalidArgumentException if key is not found |
||
142 | */ |
||
143 | 8 | public function get($key) |
|
148 | |||
149 | /** |
||
150 | * Return an array with the key and values |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | 1 | public function getValues() |
|
158 | |||
159 | /** |
||
160 | * Utility function that throws an exception if the key is not found |
||
161 | * @param string $key |
||
162 | */ |
||
163 | 14 | protected function checkDefinitionExists($key) |
|
169 | |||
170 | 3 | public function __get($name) |
|
174 | |||
175 | 1 | public function __set($name, $value) |
|
179 | |||
180 | 2 | public function __isset($name) |
|
184 | |||
185 | 1 | public function __unset($name) |
|
189 | |||
190 | 2 | public function offsetExists($offset) |
|
194 | |||
195 | 3 | public function offsetGet($offset) |
|
199 | |||
200 | 1 | public function offsetSet($offset, $value) |
|
204 | |||
205 | 1 | public function offsetUnset($offset) |
|
209 | |||
210 | 1 | public function getIterator() |
|
214 | |||
215 | 2 | public function count() |
|
219 | } |
||
220 |