PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Ip/Internal/Vendor/Zend/Stdlib/ArrayObject/PhpReferenceCompatibility.php

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