Config.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config;
  10. use ArrayAccess;
  11. use Countable;
  12. use Iterator;
  13. /**
  14. * Provides a property based interface to an array.
  15. * The data are read-only unless $allowModifications is set to true
  16. * on construction.
  17. *
  18. * Implements Countable, Iterator and ArrayAccess
  19. * to facilitate easy access to the data.
  20. */
  21. class Config implements Countable, Iterator, ArrayAccess
  22. {
  23. /**
  24. * Whether modifications to configuration data are allowed.
  25. *
  26. * @var bool
  27. */
  28. protected $allowModifications;
  29. /**
  30. * Number of elements in configuration data.
  31. *
  32. * @var int
  33. */
  34. protected $count;
  35. /**
  36. * Data within the configuration.
  37. *
  38. * @var array
  39. */
  40. protected $data = array();
  41. /**
  42. * Used when unsetting values during iteration to ensure we do not skip
  43. * the next element.
  44. *
  45. * @var bool
  46. */
  47. protected $skipNextIteration;
  48. /**
  49. * Constructor.
  50. *
  51. * Data is read-only unless $allowModifications is set to true
  52. * on construction.
  53. *
  54. * @param array $array
  55. * @param bool $allowModifications
  56. */
  57. public function __construct(array $array, $allowModifications = false)
  58. {
  59. $this->allowModifications = (bool) $allowModifications;
  60. foreach ($array as $key => $value) {
  61. if (is_array($value)) {
  62. $this->data[$key] = new static($value, $this->allowModifications);
  63. } else {
  64. $this->data[$key] = $value;
  65. }
  66. $this->count++;
  67. }
  68. }
  69. /**
  70. * Retrieve a value and return $default if there is no element set.
  71. *
  72. * @param string $name
  73. * @param mixed $default
  74. * @return mixed
  75. */
  76. public function get($name, $default = null)
  77. {
  78. if (array_key_exists($name, $this->data)) {
  79. return $this->data[$name];
  80. }
  81. return $default;
  82. }
  83. /**
  84. * Magic function so that $obj->value will work.
  85. *
  86. * @param string $name
  87. * @return mixed
  88. */
  89. public function __get($name)
  90. {
  91. return $this->get($name);
  92. }
  93. /**
  94. * Set a value in the config.
  95. *
  96. * Only allow setting of a property if $allowModifications was set to true
  97. * on construction. Otherwise, throw an exception.
  98. *
  99. * @param string $name
  100. * @param mixed $value
  101. * @return void
  102. * @throws Exception\RuntimeException
  103. */
  104. public function __set($name, $value)
  105. {
  106. if ($this->allowModifications) {
  107. if (is_array($value)) {
  108. $value = new static($value, true);
  109. }
  110. if (null === $name) {
  111. $this->data[] = $value;
  112. } else {
  113. $this->data[$name] = $value;
  114. }
  115. $this->count++;
  116. } else {
  117. throw new Exception\RuntimeException('Config is read only');
  118. }
  119. }
  120. /**
  121. * Deep clone of this instance to ensure that nested Zend\Configs are also
  122. * cloned.
  123. *
  124. * @return void
  125. */
  126. public function __clone()
  127. {
  128. $array = array();
  129. foreach ($this->data as $key => $value) {
  130. if ($value instanceof self) {
  131. $array[$key] = clone $value;
  132. } else {
  133. $array[$key] = $value;
  134. }
  135. }
  136. $this->data = $array;
  137. }
  138. /**
  139. * Return an associative array of the stored data.
  140. *
  141. * @return array
  142. */
  143. public function toArray()
  144. {
  145. $array = array();
  146. $data = $this->data;
  147. /** @var self $value */
  148. foreach ($data as $key => $value) {
  149. if ($value instanceof self) {
  150. $array[$key] = $value->toArray();
  151. } else {
  152. $array[$key] = $value;
  153. }
  154. }
  155. return $array;
  156. }
  157. /**
  158. * isset() overloading
  159. *
  160. * @param string $name
  161. * @return bool
  162. */
  163. public function __isset($name)
  164. {
  165. return isset($this->data[$name]);
  166. }
  167. /**
  168. * unset() overloading
  169. *
  170. * @param string $name
  171. * @return void
  172. * @throws Exception\InvalidArgumentException
  173. */
  174. public function __unset($name)
  175. {
  176. if (!$this->allowModifications) {
  177. throw new Exception\InvalidArgumentException('Config is read only');
  178. } elseif (isset($this->data[$name])) {
  179. unset($this->data[$name]);
  180. $this->count--;
  181. $this->skipNextIteration = true;
  182. }
  183. }
  184. /**
  185. * count(): defined by Countable interface.
  186. *
  187. * @see Countable::count()
  188. * @return int
  189. */
  190. public function count()
  191. {
  192. return $this->count;
  193. }
  194. /**
  195. * current(): defined by Iterator interface.
  196. *
  197. * @see Iterator::current()
  198. * @return mixed
  199. */
  200. public function current()
  201. {
  202. $this->skipNextIteration = false;
  203. return current($this->data);
  204. }
  205. /**
  206. * key(): defined by Iterator interface.
  207. *
  208. * @see Iterator::key()
  209. * @return mixed
  210. */
  211. public function key()
  212. {
  213. return key($this->data);
  214. }
  215. /**
  216. * next(): defined by Iterator interface.
  217. *
  218. * @see Iterator::next()
  219. * @return void
  220. */
  221. public function next()
  222. {
  223. if ($this->skipNextIteration) {
  224. $this->skipNextIteration = false;
  225. return;
  226. }
  227. next($this->data);
  228. }
  229. /**
  230. * rewind(): defined by Iterator interface.
  231. *
  232. * @see Iterator::rewind()
  233. * @return void
  234. */
  235. public function rewind()
  236. {
  237. $this->skipNextIteration = false;
  238. reset($this->data);
  239. }
  240. /**
  241. * valid(): defined by Iterator interface.
  242. *
  243. * @see Iterator::valid()
  244. * @return bool
  245. */
  246. public function valid()
  247. {
  248. return ($this->key() !== null);
  249. }
  250. /**
  251. * offsetExists(): defined by ArrayAccess interface.
  252. *
  253. * @see ArrayAccess::offsetExists()
  254. * @param mixed $offset
  255. * @return bool
  256. */
  257. public function offsetExists($offset)
  258. {
  259. return $this->__isset($offset);
  260. }
  261. /**
  262. * offsetGet(): defined by ArrayAccess interface.
  263. *
  264. * @see ArrayAccess::offsetGet()
  265. * @param mixed $offset
  266. * @return mixed
  267. */
  268. public function offsetGet($offset)
  269. {
  270. return $this->__get($offset);
  271. }
  272. /**
  273. * offsetSet(): defined by ArrayAccess interface.
  274. *
  275. * @see ArrayAccess::offsetSet()
  276. * @param mixed $offset
  277. * @param mixed $value
  278. * @return void
  279. */
  280. public function offsetSet($offset, $value)
  281. {
  282. $this->__set($offset, $value);
  283. }
  284. /**
  285. * offsetUnset(): defined by ArrayAccess interface.
  286. *
  287. * @see ArrayAccess::offsetUnset()
  288. * @param mixed $offset
  289. * @return void
  290. */
  291. public function offsetUnset($offset)
  292. {
  293. $this->__unset($offset);
  294. }
  295. /**
  296. * Merge another Config with this one.
  297. *
  298. * For duplicate keys, the following will be performed:
  299. * - Nested Configs will be recursively merged.
  300. * - Items in $merge with INTEGER keys will be appended.
  301. * - Items in $merge with STRING keys will overwrite current values.
  302. *
  303. * @param Config $merge
  304. * @return Config
  305. */
  306. public function merge(Config $merge)
  307. {
  308. /** @var Config $value */
  309. foreach ($merge as $key => $value) {
  310. if (array_key_exists($key, $this->data)) {
  311. if (is_int($key)) {
  312. $this->data[] = $value;
  313. } elseif ($value instanceof self && $this->data[$key] instanceof self) {
  314. $this->data[$key]->merge($value);
  315. } else {
  316. if ($value instanceof self) {
  317. $this->data[$key] = new static($value->toArray(), $this->allowModifications);
  318. } else {
  319. $this->data[$key] = $value;
  320. }
  321. }
  322. } else {
  323. if ($value instanceof self) {
  324. $this->data[$key] = new static($value->toArray(), $this->allowModifications);
  325. } else {
  326. $this->data[$key] = $value;
  327. }
  328. $this->count++;
  329. }
  330. }
  331. return $this;
  332. }
  333. /**
  334. * Prevent any more modifications being made to this instance.
  335. *
  336. * Useful after merge() has been used to merge multiple Config objects
  337. * into one object which should then not be modified again.
  338. *
  339. * @return void
  340. */
  341. public function setReadOnly()
  342. {
  343. $this->allowModifications = false;
  344. /** @var Config $value */
  345. foreach ($this->data as $value) {
  346. if ($value instanceof self) {
  347. $value->setReadOnly();
  348. }
  349. }
  350. }
  351. /**
  352. * Returns whether this Config object is read only or not.
  353. *
  354. * @return bool
  355. */
  356. public function isReadOnly()
  357. {
  358. return !$this->allowModifications;
  359. }
  360. }