PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-stdlib/src/ArrayObject.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 432 lines | 203 code | 49 blank | 180 comment | 26 complexity | 764415762b6dbb29654f904802ab9c60 MD5 | raw file
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib;
  10. use ArrayAccess;
  11. use Countable;
  12. use IteratorAggregate;
  13. use Serializable;
  14. /**
  15. * Custom framework ArrayObject implementation
  16. *
  17. * Extends version-specific "abstract" implementation.
  18. */
  19. class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
  20. {
  21. /**
  22. * Properties of the object have their normal functionality
  23. * when accessed as list (var_dump, foreach, etc.).
  24. */
  25. const STD_PROP_LIST = 1;
  26. /**
  27. * Entries can be accessed as properties (read and write).
  28. */
  29. const ARRAY_AS_PROPS = 2;
  30. /**
  31. * @var array
  32. */
  33. protected $storage;
  34. /**
  35. * @var int
  36. */
  37. protected $flag;
  38. /**
  39. * @var string
  40. */
  41. protected $iteratorClass;
  42. /**
  43. * @var array
  44. */
  45. protected $protectedProperties;
  46. /**
  47. * Constructor
  48. *
  49. * @param array $input
  50. * @param int $flags
  51. * @param string $iteratorClass
  52. */
  53. public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator')
  54. {
  55. $this->setFlags($flags);
  56. $this->storage = $input;
  57. $this->setIteratorClass($iteratorClass);
  58. $this->protectedProperties = array_keys(get_object_vars($this));
  59. }
  60. /**
  61. * Returns whether the requested key exists
  62. *
  63. * @param mixed $key
  64. * @return bool
  65. */
  66. public function __isset($key)
  67. {
  68. if ($this->flag == self::ARRAY_AS_PROPS) {
  69. return $this->offsetExists($key);
  70. }
  71. if (in_array($key, $this->protectedProperties)) {
  72. throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
  73. }
  74. return isset($this->$key);
  75. }
  76. /**
  77. * Sets the value at the specified key to value
  78. *
  79. * @param mixed $key
  80. * @param mixed $value
  81. * @return void
  82. */
  83. public function __set($key, $value)
  84. {
  85. if ($this->flag == self::ARRAY_AS_PROPS) {
  86. return $this->offsetSet($key, $value);
  87. }
  88. if (in_array($key, $this->protectedProperties)) {
  89. throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
  90. }
  91. $this->$key = $value;
  92. }
  93. /**
  94. * Unsets the value at the specified key
  95. *
  96. * @param mixed $key
  97. * @return void
  98. */
  99. public function __unset($key)
  100. {
  101. if ($this->flag == self::ARRAY_AS_PROPS) {
  102. return $this->offsetUnset($key);
  103. }
  104. if (in_array($key, $this->protectedProperties)) {
  105. throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
  106. }
  107. unset($this->$key);
  108. }
  109. /**
  110. * Returns the value at the specified key by reference
  111. *
  112. * @param mixed $key
  113. * @return mixed
  114. */
  115. public function &__get($key)
  116. {
  117. $ret = null;
  118. if ($this->flag == self::ARRAY_AS_PROPS) {
  119. $ret =& $this->offsetGet($key);
  120. return $ret;
  121. }
  122. if (in_array($key, $this->protectedProperties)) {
  123. throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
  124. }
  125. return $this->$key;
  126. }
  127. /**
  128. * Appends the value
  129. *
  130. * @param mixed $value
  131. * @return void
  132. */
  133. public function append($value)
  134. {
  135. $this->storage[] = $value;
  136. }
  137. /**
  138. * Sort the entries by value
  139. *
  140. * @return void
  141. */
  142. public function asort()
  143. {
  144. asort($this->storage);
  145. }
  146. /**
  147. * Get the number of public properties in the ArrayObject
  148. *
  149. * @return int
  150. */
  151. public function count()
  152. {
  153. return count($this->storage);
  154. }
  155. /**
  156. * Exchange the array for another one.
  157. *
  158. * @param array|ArrayObject $data
  159. * @return array
  160. */
  161. public function exchangeArray($data)
  162. {
  163. if (!is_array($data) && !is_object($data)) {
  164. throw new Exception\InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
  165. }
  166. if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
  167. $data = $data->getArrayCopy();
  168. }
  169. if (!is_array($data)) {
  170. $data = (array) $data;
  171. }
  172. $storage = $this->storage;
  173. $this->storage = $data;
  174. return $storage;
  175. }
  176. /**
  177. * Creates a copy of the ArrayObject.
  178. *
  179. * @return array
  180. */
  181. public function getArrayCopy()
  182. {
  183. return $this->storage;
  184. }
  185. /**
  186. * Gets the behavior flags.
  187. *
  188. * @return int
  189. */
  190. public function getFlags()
  191. {
  192. return $this->flag;
  193. }
  194. /**
  195. * Create a new iterator from an ArrayObject instance
  196. *
  197. * @return \Iterator
  198. */
  199. public function getIterator()
  200. {
  201. $class = $this->iteratorClass;
  202. return new $class($this->storage);
  203. }
  204. /**
  205. * Gets the iterator classname for the ArrayObject.
  206. *
  207. * @return string
  208. */
  209. public function getIteratorClass()
  210. {
  211. return $this->iteratorClass;
  212. }
  213. /**
  214. * Sort the entries by key
  215. *
  216. * @return void
  217. */
  218. public function ksort()
  219. {
  220. ksort($this->storage);
  221. }
  222. /**
  223. * Sort an array using a case insensitive "natural order" algorithm
  224. *
  225. * @return void
  226. */
  227. public function natcasesort()
  228. {
  229. natcasesort($this->storage);
  230. }
  231. /**
  232. * Sort entries using a "natural order" algorithm
  233. *
  234. * @return void
  235. */
  236. public function natsort()
  237. {
  238. natsort($this->storage);
  239. }
  240. /**
  241. * Returns whether the requested key exists
  242. *
  243. * @param mixed $key
  244. * @return bool
  245. */
  246. public function offsetExists($key)
  247. {
  248. return isset($this->storage[$key]);
  249. }
  250. /**
  251. * Returns the value at the specified key
  252. *
  253. * @param mixed $key
  254. * @return mixed
  255. */
  256. public function &offsetGet($key)
  257. {
  258. $ret = null;
  259. if (!$this->offsetExists($key)) {
  260. return $ret;
  261. }
  262. $ret =& $this->storage[$key];
  263. return $ret;
  264. }
  265. /**
  266. * Sets the value at the specified key to value
  267. *
  268. * @param mixed $key
  269. * @param mixed $value
  270. * @return void
  271. */
  272. public function offsetSet($key, $value)
  273. {
  274. $this->storage[$key] = $value;
  275. }
  276. /**
  277. * Unsets the value at the specified key
  278. *
  279. * @param mixed $key
  280. * @return void
  281. */
  282. public function offsetUnset($key)
  283. {
  284. if ($this->offsetExists($key)) {
  285. unset($this->storage[$key]);
  286. }
  287. }
  288. /**
  289. * Serialize an ArrayObject
  290. *
  291. * @return string
  292. */
  293. public function serialize()
  294. {
  295. return serialize(get_object_vars($this));
  296. }
  297. /**
  298. * Sets the behavior flags
  299. *
  300. * @param int $flags
  301. * @return void
  302. */
  303. public function setFlags($flags)
  304. {
  305. $this->flag = $flags;
  306. }
  307. /**
  308. * Sets the iterator classname for the ArrayObject
  309. *
  310. * @param string $class
  311. * @return void
  312. */
  313. public function setIteratorClass($class)
  314. {
  315. if (class_exists($class)) {
  316. $this->iteratorClass = $class;
  317. return ;
  318. }
  319. if (strpos($class, '\\') === 0) {
  320. $class = '\\' . $class;
  321. if (class_exists($class)) {
  322. $this->iteratorClass = $class;
  323. return ;
  324. }
  325. }
  326. throw new Exception\InvalidArgumentException('The iterator class does not exist');
  327. }
  328. /**
  329. * Sort the entries with a user-defined comparison function and maintain key association
  330. *
  331. * @param callable $function
  332. * @return void
  333. */
  334. public function uasort($function)
  335. {
  336. if (is_callable($function)) {
  337. uasort($this->storage, $function);
  338. }
  339. }
  340. /**
  341. * Sort the entries by keys using a user-defined comparison function
  342. *
  343. * @param callable $function
  344. * @return void
  345. */
  346. public function uksort($function)
  347. {
  348. if (is_callable($function)) {
  349. uksort($this->storage, $function);
  350. }
  351. }
  352. /**
  353. * Unserialize an ArrayObject
  354. *
  355. * @param string $data
  356. * @return void
  357. */
  358. public function unserialize($data)
  359. {
  360. $ar = unserialize($data);
  361. $this->protectedProperties = array_keys(get_object_vars($this));
  362. $this->setFlags($ar['flag']);
  363. $this->exchangeArray($ar['storage']);
  364. $this->setIteratorClass($ar['iteratorClass']);
  365. foreach ($ar as $k => $v) {
  366. switch ($k) {
  367. case 'flag':
  368. $this->setFlags($v);
  369. break;
  370. case 'storage':
  371. $this->exchangeArray($v);
  372. break;
  373. case 'iteratorClass':
  374. $this->setIteratorClass($v);
  375. break;
  376. case 'protectedProperties':
  377. continue;
  378. default:
  379. $this->__set($k, $v);
  380. }
  381. }
  382. }
  383. }