PageRenderTime 86ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/nooku/libraries/koowa/object/array.php

https://github.com/bhar1red/anahita
PHP | 218 lines | 76 code | 20 blank | 122 comment | 4 complexity | 2fe856616940650ade0279a33531c52b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: array.php 4639 2012-05-13 16:43:53Z johanjanssens $
  4. * @package Koowa_Object
  5. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  6. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  7. * @link http://www.nooku.org
  8. */
  9. /**
  10. * An Object Array Class
  11. *
  12. * The KObjectArray class provides provides the main functionalities of array and at
  13. * the same time implement the features of KObject
  14. *
  15. * @author Johan Janssens <johan@nooku.org>
  16. * @category Koowa
  17. * @package Koowa_Object
  18. */
  19. class KObjectArray extends KObject implements IteratorAggregate, ArrayAccess, Serializable
  20. {
  21. /**
  22. * The data for each key in the array (key => value).
  23. *
  24. * @var array
  25. */
  26. protected $_data = array();
  27. /**
  28. * Constructor
  29. *
  30. * @param KConfig|null $config An optional KConfig object with configuration options
  31. * @return \KObjectArray
  32. */
  33. public function __construct(KConfig $config = null)
  34. {
  35. //If no config is passed create it
  36. if(!isset($config)) $config = new KConfig();
  37. parent::__construct($config);
  38. $this->_data = KConfig::unbox($config->data);
  39. }
  40. /**
  41. * Initializes the options for the object
  42. *
  43. * Called from {@link __construct()} as a first step of object instantiation.
  44. *
  45. * @param KConfig $object An optional KConfig object with configuration options
  46. * @return void
  47. */
  48. protected function _initialize(KConfig $config)
  49. {
  50. $config->append(array(
  51. 'data' => array(),
  52. ));
  53. parent::_initialize($config);
  54. }
  55. /**
  56. * Check if the offset exists
  57. *
  58. * Required by interface ArrayAccess
  59. *
  60. * @param int $offset
  61. * @return bool
  62. */
  63. public function offsetExists($offset)
  64. {
  65. return $this->__isset($offset);
  66. }
  67. /**
  68. * Get an item from the array by offset
  69. *
  70. * Required by interface ArrayAccess
  71. *
  72. * @param int $offset
  73. * @return mixed The item from the array
  74. */
  75. public function offsetGet($offset)
  76. {
  77. return $this->__get($offset);
  78. }
  79. /**
  80. * Set an item in the array
  81. *
  82. * Required by interface ArrayAccess
  83. *
  84. * @param int $offset
  85. * @param mixed $value
  86. * @return KObjectArray
  87. */
  88. public function offsetSet($offset, $value)
  89. {
  90. if (is_null($offset)) {
  91. $this->_data[] = $value;
  92. } else {
  93. $this->__set($offset, $value);
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Unset an item in the array
  99. *
  100. * All numerical array keys will be modified to start counting from zero while
  101. * literal keys won't be touched.
  102. *
  103. * Required by interface ArrayAccess
  104. *
  105. * @param int $offset
  106. * @return KObjectArray
  107. */
  108. public function offsetUnset($offset)
  109. {
  110. $this->__unset($offset);
  111. return $this;
  112. }
  113. /**
  114. * Get a new iterator
  115. *
  116. * @return ArrayIterator
  117. */
  118. public function getIterator()
  119. {
  120. return new ArrayIterator($this->_data);
  121. }
  122. /**
  123. * Serialize
  124. *
  125. * Required by interface Serializable
  126. *
  127. * @return string
  128. */
  129. public function serialize()
  130. {
  131. return serialize($this->_data);
  132. }
  133. /**
  134. * Unserialize
  135. *
  136. * Required by interface Serializable
  137. *
  138. * @param string $data
  139. */
  140. public function unserialize($data)
  141. {
  142. $this->_data = unserialize($data);
  143. }
  144. /**
  145. * Get a value by key
  146. *
  147. * @param string $key The key name.
  148. * @return string The corresponding value.
  149. */
  150. public function __get($key)
  151. {
  152. $result = null;
  153. if(isset($this->_data[$key])) {
  154. $result = $this->_data[$key];
  155. }
  156. return $result;
  157. }
  158. /**
  159. * Set a value by key
  160. *
  161. * @param string $key The key name
  162. * @param mixed $value The value for the key
  163. * @return void
  164. */
  165. public function __set($key, $value)
  166. {
  167. $this->_data[$key] = $value;
  168. }
  169. /**
  170. * Test existence of a key
  171. *
  172. * @param string $key The key name
  173. * @return boolean
  174. */
  175. public function __isset($key)
  176. {
  177. return array_key_exists($key, $this->_data);
  178. }
  179. /**
  180. * Unset a key
  181. *
  182. * @param string $key The key name
  183. * @return void
  184. */
  185. public function __unset($key)
  186. {
  187. unset($this->_data[$key]);
  188. }
  189. /**
  190. * Return an associative array of the data
  191. *
  192. * @return array
  193. */
  194. public function toArray()
  195. {
  196. return $this->_data;
  197. }
  198. }