Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Config / Config.php
bloba1e1f7d65feac012e129832249bf1b33a9095003
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Config;
12 use ArrayAccess;
13 use Countable;
14 use Iterator;
16 /**
17 * Provides a property based interface to an array.
18 * The data are read-only unless $allowModifications is set to true
19 * on construction.
21 * Implements Countable, Iterator and ArrayAccess
22 * to facilitate easy access to the data.
24 class Config implements Countable, Iterator, ArrayAccess
26 /**
27 * Whether modifications to configuration data are allowed.
29 * @var bool
31 protected $allowModifications;
33 /**
34 * Number of elements in configuration data.
36 * @var int
38 protected $count;
40 /**
41 * Data within the configuration.
43 * @var array
45 protected $data = array();
47 /**
48 * Used when unsetting values during iteration to ensure we do not skip
49 * the next element.
51 * @var bool
53 protected $skipNextIteration;
55 /**
56 * Constructor.
58 * Data is read-only unless $allowModifications is set to true
59 * on construction.
61 * @param array $array
62 * @param bool $allowModifications
64 public function __construct(array $array, $allowModifications = false)
66 $this->allowModifications = (bool) $allowModifications;
68 foreach ($array as $key => $value) {
69 if (is_array($value)) {
70 $this->data[$key] = new static($value, $this->allowModifications);
71 } else {
72 $this->data[$key] = $value;
75 $this->count++;
79 /**
80 * Retrieve a value and return $default if there is no element set.
82 * @param string $name
83 * @param mixed $default
84 * @return mixed
86 public function get($name, $default = null)
88 if (array_key_exists($name, $this->data)) {
89 return $this->data[$name];
92 return $default;
95 /**
96 * Magic function so that $obj->value will work.
98 * @param string $name
99 * @return mixed
101 public function __get($name)
103 return $this->get($name);
107 * Set a value in the config.
109 * Only allow setting of a property if $allowModifications was set to true
110 * on construction. Otherwise, throw an exception.
112 * @param string $name
113 * @param mixed $value
114 * @return void
115 * @throws Exception\RuntimeException
117 public function __set($name, $value)
119 if ($this->allowModifications) {
121 if (is_array($value)) {
122 $value = new static($value, true);
125 if (null === $name) {
126 $this->data[] = $value;
127 } else {
128 $this->data[$name] = $value;
131 $this->count++;
132 } else {
133 throw new Exception\RuntimeException('Config is read only');
138 * Deep clone of this instance to ensure that nested Zend\Configs are also
139 * cloned.
141 * @return void
143 public function __clone()
145 $array = array();
147 foreach ($this->data as $key => $value) {
148 if ($value instanceof self) {
149 $array[$key] = clone $value;
150 } else {
151 $array[$key] = $value;
155 $this->data = $array;
159 * Return an associative array of the stored data.
161 * @return array
163 public function toArray()
165 $array = array();
166 $data = $this->data;
168 /** @var self $value */
169 foreach ($data as $key => $value) {
170 if ($value instanceof self) {
171 $array[$key] = $value->toArray();
172 } else {
173 $array[$key] = $value;
177 return $array;
181 * isset() overloading
183 * @param string $name
184 * @return bool
186 public function __isset($name)
188 return isset($this->data[$name]);
192 * unset() overloading
194 * @param string $name
195 * @return void
196 * @throws Exception\InvalidArgumentException
198 public function __unset($name)
200 if (!$this->allowModifications) {
201 throw new Exception\InvalidArgumentException('Config is read only');
202 } elseif (isset($this->data[$name])) {
203 unset($this->data[$name]);
204 $this->count--;
205 $this->skipNextIteration = true;
210 * count(): defined by Countable interface.
212 * @see Countable::count()
213 * @return int
215 public function count()
217 return $this->count;
221 * current(): defined by Iterator interface.
223 * @see Iterator::current()
224 * @return mixed
226 public function current()
228 $this->skipNextIteration = false;
229 return current($this->data);
233 * key(): defined by Iterator interface.
235 * @see Iterator::key()
236 * @return mixed
238 public function key()
240 return key($this->data);
244 * next(): defined by Iterator interface.
246 * @see Iterator::next()
247 * @return void
249 public function next()
251 if ($this->skipNextIteration) {
252 $this->skipNextIteration = false;
253 return;
256 next($this->data);
260 * rewind(): defined by Iterator interface.
262 * @see Iterator::rewind()
263 * @return void
265 public function rewind()
267 $this->skipNextIteration = false;
268 reset($this->data);
272 * valid(): defined by Iterator interface.
274 * @see Iterator::valid()
275 * @return bool
277 public function valid()
279 return ($this->key() !== null);
283 * offsetExists(): defined by ArrayAccess interface.
285 * @see ArrayAccess::offsetExists()
286 * @param mixed $offset
287 * @return bool
289 public function offsetExists($offset)
291 return $this->__isset($offset);
295 * offsetGet(): defined by ArrayAccess interface.
297 * @see ArrayAccess::offsetGet()
298 * @param mixed $offset
299 * @return mixed
301 public function offsetGet($offset)
303 return $this->__get($offset);
307 * offsetSet(): defined by ArrayAccess interface.
309 * @see ArrayAccess::offsetSet()
310 * @param mixed $offset
311 * @param mixed $value
312 * @return void
314 public function offsetSet($offset, $value)
316 $this->__set($offset, $value);
320 * offsetUnset(): defined by ArrayAccess interface.
322 * @see ArrayAccess::offsetUnset()
323 * @param mixed $offset
324 * @return void
326 public function offsetUnset($offset)
328 $this->__unset($offset);
332 * Merge another Config with this one.
334 * For duplicate keys, the following will be performed:
335 * - Nested Configs will be recursively merged.
336 * - Items in $merge with INTEGER keys will be appended.
337 * - Items in $merge with STRING keys will overwrite current values.
339 * @param Config $merge
340 * @return Config
342 public function merge(Config $merge)
344 /** @var Config $value */
345 foreach ($merge as $key => $value) {
346 if (array_key_exists($key, $this->data)) {
347 if (is_int($key)) {
348 $this->data[] = $value;
349 } elseif ($value instanceof self && $this->data[$key] instanceof self) {
350 $this->data[$key]->merge($value);
351 } else {
352 if ($value instanceof self) {
353 $this->data[$key] = new static($value->toArray(), $this->allowModifications);
354 } else {
355 $this->data[$key] = $value;
358 } else {
359 if ($value instanceof self) {
360 $this->data[$key] = new static($value->toArray(), $this->allowModifications);
361 } else {
362 $this->data[$key] = $value;
365 $this->count++;
369 return $this;
373 * Prevent any more modifications being made to this instance.
375 * Useful after merge() has been used to merge multiple Config objects
376 * into one object which should then not be modified again.
378 * @return void
380 public function setReadOnly()
382 $this->allowModifications = false;
384 /** @var Config $value */
385 foreach ($this->data as $value) {
386 if ($value instanceof self) {
387 $value->setReadOnly();
393 * Returns whether this Config object is read only or not.
395 * @return bool
397 public function isReadOnly()
399 return !$this->allowModifications;