ArrayObject.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. declare(strict_types=1);
  3. namespace Laminas\Stdlib;
  4. use AllowDynamicProperties;
  5. use ArrayAccess;
  6. use ArrayIterator;
  7. use Countable;
  8. use Iterator;
  9. use IteratorAggregate;
  10. use ReturnTypeWillChange;
  11. use Serializable;
  12. use UnexpectedValueException;
  13. use function array_key_exists;
  14. use function array_keys;
  15. use function asort;
  16. use function class_exists;
  17. use function count;
  18. use function get_debug_type;
  19. use function get_object_vars;
  20. use function gettype;
  21. use function in_array;
  22. use function is_array;
  23. use function is_callable;
  24. use function is_object;
  25. use function is_string;
  26. use function ksort;
  27. use function natcasesort;
  28. use function natsort;
  29. use function serialize;
  30. use function sprintf;
  31. use function str_starts_with;
  32. use function uasort;
  33. use function uksort;
  34. use function unserialize;
  35. /**
  36. * Custom framework ArrayObject implementation
  37. *
  38. * Extends version-specific "abstract" implementation.
  39. *
  40. * @template TKey of array-key
  41. * @template TValue
  42. * @template-implements IteratorAggregate<TKey, TValue>
  43. * @template-implements ArrayAccess<TKey, TValue>
  44. */
  45. #[AllowDynamicProperties]
  46. class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
  47. {
  48. /**
  49. * Properties of the object have their normal functionality
  50. * when accessed as list (var_dump, foreach, etc.).
  51. */
  52. public const STD_PROP_LIST = 1;
  53. /**
  54. * Entries can be accessed as properties (read and write).
  55. */
  56. public const ARRAY_AS_PROPS = 2;
  57. /** @var array<TKey, TValue> */
  58. protected $storage;
  59. /** @var self::STD_PROP_LIST|self::ARRAY_AS_PROPS */
  60. protected $flag;
  61. /** @var class-string<Iterator> */
  62. protected $iteratorClass;
  63. /** @var list<string> */
  64. protected $protectedProperties;
  65. /**
  66. * @param array<TKey, TValue>|object $input Object values must act like ArrayAccess
  67. * @param self::STD_PROP_LIST|self::ARRAY_AS_PROPS $flags
  68. * @param class-string<Iterator> $iteratorClass
  69. */
  70. public function __construct($input = [], $flags = self::STD_PROP_LIST, $iteratorClass = ArrayIterator::class)
  71. {
  72. $this->setFlags($flags);
  73. $this->storage = $input;
  74. $this->setIteratorClass($iteratorClass);
  75. $this->protectedProperties = array_keys(get_object_vars($this));
  76. }
  77. /**
  78. * Returns whether the requested key exists
  79. *
  80. * @param TKey $key
  81. * @return bool
  82. */
  83. public function __isset(mixed $key)
  84. {
  85. if ($this->flag === self::ARRAY_AS_PROPS) {
  86. return $this->offsetExists($key);
  87. }
  88. if (in_array($key, $this->protectedProperties)) {
  89. throw new Exception\InvalidArgumentException("$key is a protected property, use a different key");
  90. }
  91. return isset($this->$key);
  92. }
  93. /**
  94. * Sets the value at the specified key to value
  95. *
  96. * @param TKey $key
  97. * @param TValue $value
  98. * @return void
  99. */
  100. public function __set(mixed $key, mixed $value)
  101. {
  102. if ($this->flag === self::ARRAY_AS_PROPS) {
  103. $this->offsetSet($key, $value);
  104. return;
  105. }
  106. if (in_array($key, $this->protectedProperties)) {
  107. throw new Exception\InvalidArgumentException("$key is a protected property, use a different key");
  108. }
  109. $this->$key = $value;
  110. }
  111. /**
  112. * Unsets the value at the specified key
  113. *
  114. * @param TKey $key
  115. * @return void
  116. */
  117. public function __unset(mixed $key)
  118. {
  119. if ($this->flag === self::ARRAY_AS_PROPS) {
  120. $this->offsetUnset($key);
  121. return;
  122. }
  123. if (in_array($key, $this->protectedProperties)) {
  124. throw new Exception\InvalidArgumentException("$key is a protected property, use a different key");
  125. }
  126. unset($this->$key);
  127. }
  128. /**
  129. * Returns the value at the specified key by reference
  130. *
  131. * @param TKey $key
  132. * @return TValue|null
  133. */
  134. public function &__get(mixed $key)
  135. {
  136. if ($this->flag === self::ARRAY_AS_PROPS) {
  137. $ret = &$this->offsetGet($key);
  138. return $ret;
  139. }
  140. if (in_array($key, $this->protectedProperties, true)) {
  141. throw new Exception\InvalidArgumentException("$key is a protected property, use a different key");
  142. }
  143. return $this->$key;
  144. }
  145. /**
  146. * Appends the value
  147. *
  148. * @param TValue $value
  149. * @return void
  150. */
  151. public function append(mixed $value)
  152. {
  153. $this->storage[] = $value;
  154. }
  155. /**
  156. * Sort the entries by value
  157. *
  158. * @return void
  159. */
  160. public function asort()
  161. {
  162. asort($this->storage);
  163. }
  164. /**
  165. * Get the number of public properties in the ArrayObject
  166. *
  167. * @return positive-int|0
  168. */
  169. #[ReturnTypeWillChange]
  170. public function count()
  171. {
  172. return count($this->storage);
  173. }
  174. /**
  175. * Exchange the array for another one.
  176. *
  177. * @param array<TKey, TValue>|ArrayObject<TKey, TValue>|ArrayIterator<TKey, TValue>|object $data
  178. * @return array<TKey, TValue>
  179. */
  180. public function exchangeArray($data)
  181. {
  182. if (! is_array($data) && ! is_object($data)) {
  183. throw new Exception\InvalidArgumentException(
  184. 'Passed variable is not an array or object, using empty array instead'
  185. );
  186. }
  187. if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
  188. $data = $data->getArrayCopy();
  189. }
  190. if (! is_array($data)) {
  191. $data = (array) $data;
  192. }
  193. $storage = $this->storage;
  194. $this->storage = $data;
  195. return $storage;
  196. }
  197. /**
  198. * Creates a copy of the ArrayObject.
  199. *
  200. * @return array<TKey, TValue>
  201. */
  202. public function getArrayCopy()
  203. {
  204. return $this->storage;
  205. }
  206. /**
  207. * Gets the behavior flags.
  208. *
  209. * @return self::STD_PROP_LIST|self::ARRAY_AS_PROPS
  210. */
  211. public function getFlags()
  212. {
  213. return $this->flag;
  214. }
  215. /**
  216. * Create a new iterator from an ArrayObject instance
  217. *
  218. * @return Iterator<TKey, TValue>
  219. */
  220. #[ReturnTypeWillChange]
  221. public function getIterator()
  222. {
  223. $class = $this->iteratorClass;
  224. return new $class($this->storage);
  225. }
  226. /**
  227. * Gets the iterator classname for the ArrayObject.
  228. *
  229. * @return class-string<Iterator>
  230. */
  231. public function getIteratorClass()
  232. {
  233. return $this->iteratorClass;
  234. }
  235. /**
  236. * Sort the entries by key
  237. *
  238. * @return void
  239. */
  240. public function ksort()
  241. {
  242. ksort($this->storage);
  243. }
  244. /**
  245. * Sort an array using a case insensitive "natural order" algorithm
  246. *
  247. * @return void
  248. */
  249. public function natcasesort()
  250. {
  251. natcasesort($this->storage);
  252. }
  253. /**
  254. * Sort entries using a "natural order" algorithm
  255. *
  256. * @return void
  257. */
  258. public function natsort()
  259. {
  260. natsort($this->storage);
  261. }
  262. /**
  263. * Returns whether the requested key exists
  264. *
  265. * @param TKey $key
  266. * @return bool
  267. */
  268. #[ReturnTypeWillChange]
  269. public function offsetExists(mixed $key)
  270. {
  271. return isset($this->storage[$key]);
  272. }
  273. /**
  274. * {@inheritDoc}
  275. *
  276. * @param TKey $key
  277. * @return TValue|null
  278. */
  279. #[ReturnTypeWillChange]
  280. public function &offsetGet(mixed $key)
  281. {
  282. $ret = null;
  283. if (! $this->offsetExists($key)) {
  284. return $ret;
  285. }
  286. $ret = &$this->storage[$key];
  287. return $ret;
  288. }
  289. /**
  290. * Sets the value at the specified key to value
  291. *
  292. * @param TKey $offset
  293. * @param TValue $value
  294. * @return void
  295. */
  296. #[ReturnTypeWillChange]
  297. public function offsetSet(mixed $offset, mixed $value)
  298. {
  299. $this->storage[$offset] = $value;
  300. }
  301. /**
  302. * Unsets the value at the specified key
  303. *
  304. * @param TKey $offset
  305. * @return void
  306. */
  307. #[ReturnTypeWillChange]
  308. public function offsetUnset(mixed $offset)
  309. {
  310. if ($this->offsetExists($offset)) {
  311. unset($this->storage[$offset]);
  312. }
  313. }
  314. /**
  315. * Serialize an ArrayObject
  316. *
  317. * @return string
  318. */
  319. public function serialize()
  320. {
  321. return serialize($this->__serialize());
  322. }
  323. /**
  324. * Magic method used for serializing of an instance.
  325. *
  326. * @return array<string, mixed>
  327. */
  328. public function __serialize()
  329. {
  330. return get_object_vars($this);
  331. }
  332. /**
  333. * Sets the behavior flags
  334. *
  335. * @param self::STD_PROP_LIST|self::ARRAY_AS_PROPS $flags
  336. * @return void
  337. */
  338. public function setFlags($flags)
  339. {
  340. $this->flag = $flags;
  341. }
  342. /**
  343. * Sets the iterator classname for the ArrayObject
  344. *
  345. * @param class-string<Iterator> $class
  346. * @return void
  347. */
  348. public function setIteratorClass($class)
  349. {
  350. if (class_exists($class)) {
  351. $this->iteratorClass = $class;
  352. return;
  353. }
  354. if (str_starts_with($class, '\\')) {
  355. $class = '\\' . $class;
  356. if (class_exists($class)) {
  357. $this->iteratorClass = $class;
  358. return;
  359. }
  360. }
  361. throw new Exception\InvalidArgumentException('The iterator class does not exist');
  362. }
  363. /**
  364. * Sort the entries with a user-defined comparison function and maintain key association
  365. *
  366. * @param callable(TValue, TValue): int $function
  367. * @return void
  368. */
  369. public function uasort($function)
  370. {
  371. if (is_callable($function)) {
  372. uasort($this->storage, $function);
  373. }
  374. }
  375. /**
  376. * Sort the entries by keys using a user-defined comparison function
  377. *
  378. * @param callable(TKey, TKey): int $function
  379. * @return void
  380. */
  381. public function uksort($function)
  382. {
  383. if (is_callable($function)) {
  384. uksort($this->storage, $function);
  385. }
  386. }
  387. /**
  388. * Unserialize an ArrayObject
  389. *
  390. * @param string $data
  391. * @return void
  392. */
  393. public function unserialize($data)
  394. {
  395. $toUnserialize = unserialize($data);
  396. if (! is_array($toUnserialize)) {
  397. throw new UnexpectedValueException(sprintf(
  398. 'Cannot deserialize %s instance; corrupt serialization data',
  399. self::class
  400. ));
  401. }
  402. $this->__unserialize($toUnserialize);
  403. }
  404. /**
  405. * Magic method used to rebuild an instance.
  406. *
  407. * @param array $data Data array.
  408. * @return void
  409. */
  410. public function __unserialize($data)
  411. {
  412. $this->protectedProperties = array_keys(get_object_vars($this));
  413. // Unserialize protected internal properties first
  414. if (array_key_exists('flag', $data)) {
  415. $this->setFlags((int) $data['flag']);
  416. unset($data['flag']);
  417. }
  418. if (array_key_exists('storage', $data)) {
  419. if (! is_array($data['storage']) && ! is_object($data['storage'])) {
  420. throw new UnexpectedValueException(sprintf(
  421. 'Cannot deserialize %s instance: corrupt storage data; expected array or object, received %s',
  422. self::class,
  423. gettype($data['storage'])
  424. ));
  425. }
  426. $this->exchangeArray($data['storage']);
  427. unset($data['storage']);
  428. }
  429. if (array_key_exists('iteratorClass', $data)) {
  430. if (! is_string($data['iteratorClass'])) {
  431. throw new UnexpectedValueException(sprintf(
  432. 'Cannot deserialize %s instance: invalid iteratorClass; expected string, received %s',
  433. self::class,
  434. get_debug_type($data['iteratorClass'])
  435. ));
  436. }
  437. $this->setIteratorClass($data['iteratorClass']);
  438. unset($data['iteratorClass']);
  439. }
  440. unset($data['protectedProperties']);
  441. // Unserialize array keys after resolving protected properties to ensure configuration is used.
  442. foreach ($data as $k => $v) {
  443. $this->__set($k, $v);
  444. }
  445. }
  446. }