ArrayCollection.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use ArrayIterator;
  4. use Closure;
  5. use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
  6. use const ARRAY_FILTER_USE_BOTH;
  7. use function array_filter;
  8. use function array_key_exists;
  9. use function array_keys;
  10. use function array_map;
  11. use function array_reverse;
  12. use function array_search;
  13. use function array_slice;
  14. use function array_values;
  15. use function count;
  16. use function current;
  17. use function end;
  18. use function in_array;
  19. use function key;
  20. use function next;
  21. use function reset;
  22. use function spl_object_hash;
  23. use function uasort;
  24. /**
  25. * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
  26. *
  27. * Warning: Using (un-)serialize() on a collection is not a supported use-case
  28. * and may break when we change the internals in the future. If you need to
  29. * serialize a collection use {@link toArray()} and reconstruct the collection
  30. * manually.
  31. *
  32. * @phpstan-template TKey
  33. * @psalm-template TKey of array-key
  34. * @psalm-template T
  35. * @template-implements Collection<TKey,T>
  36. * @template-implements Selectable<TKey,T>
  37. */
  38. class ArrayCollection implements Collection, Selectable
  39. {
  40. /**
  41. * An array containing the entries of this collection.
  42. *
  43. * @psalm-var array<TKey,T>
  44. * @var array
  45. */
  46. private $elements;
  47. /**
  48. * Initializes a new ArrayCollection.
  49. *
  50. * @param array $elements
  51. *
  52. * @psalm-param array<TKey,T> $elements
  53. */
  54. public function __construct(array $elements = [])
  55. {
  56. $this->elements = $elements;
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function toArray()
  62. {
  63. return $this->elements;
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function first()
  69. {
  70. return reset($this->elements);
  71. }
  72. /**
  73. * Creates a new instance from the specified elements.
  74. *
  75. * This method is provided for derived classes to specify how a new
  76. * instance should be created when constructor semantics have changed.
  77. *
  78. * @param array $elements Elements.
  79. *
  80. * @return static
  81. *
  82. * @psalm-param array<TKey,T> $elements
  83. * @psalm-return static<TKey,T>
  84. */
  85. protected function createFrom(array $elements)
  86. {
  87. return new static($elements);
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function last()
  93. {
  94. return end($this->elements);
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function key()
  100. {
  101. return key($this->elements);
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function next()
  107. {
  108. return next($this->elements);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function current()
  114. {
  115. return current($this->elements);
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function remove($key)
  121. {
  122. if (! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
  123. return null;
  124. }
  125. $removed = $this->elements[$key];
  126. unset($this->elements[$key]);
  127. return $removed;
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public function removeElement($element)
  133. {
  134. $key = array_search($element, $this->elements, true);
  135. if ($key === false) {
  136. return false;
  137. }
  138. unset($this->elements[$key]);
  139. return true;
  140. }
  141. /**
  142. * Required by interface ArrayAccess.
  143. *
  144. * {@inheritDoc}
  145. *
  146. * @psalm-param TKey $offset
  147. */
  148. public function offsetExists($offset)
  149. {
  150. return $this->containsKey($offset);
  151. }
  152. /**
  153. * Required by interface ArrayAccess.
  154. *
  155. * {@inheritDoc}
  156. *
  157. * @psalm-param TKey $offset
  158. */
  159. public function offsetGet($offset)
  160. {
  161. return $this->get($offset);
  162. }
  163. /**
  164. * Required by interface ArrayAccess.
  165. *
  166. * {@inheritDoc}
  167. */
  168. public function offsetSet($offset, $value)
  169. {
  170. if (! isset($offset)) {
  171. $this->add($value);
  172. return;
  173. }
  174. $this->set($offset, $value);
  175. }
  176. /**
  177. * Required by interface ArrayAccess.
  178. *
  179. * {@inheritDoc}
  180. *
  181. * @psalm-param TKey $offset
  182. */
  183. public function offsetUnset($offset)
  184. {
  185. $this->remove($offset);
  186. }
  187. /**
  188. * {@inheritDoc}
  189. */
  190. public function containsKey($key)
  191. {
  192. return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
  193. }
  194. /**
  195. * {@inheritDoc}
  196. */
  197. public function contains($element)
  198. {
  199. return in_array($element, $this->elements, true);
  200. }
  201. /**
  202. * {@inheritDoc}
  203. */
  204. public function exists(Closure $p)
  205. {
  206. foreach ($this->elements as $key => $element) {
  207. if ($p($key, $element)) {
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. /**
  214. * {@inheritDoc}
  215. */
  216. public function indexOf($element)
  217. {
  218. return array_search($element, $this->elements, true);
  219. }
  220. /**
  221. * {@inheritDoc}
  222. */
  223. public function get($key)
  224. {
  225. return $this->elements[$key] ?? null;
  226. }
  227. /**
  228. * {@inheritDoc}
  229. */
  230. public function getKeys()
  231. {
  232. return array_keys($this->elements);
  233. }
  234. /**
  235. * {@inheritDoc}
  236. */
  237. public function getValues()
  238. {
  239. return array_values($this->elements);
  240. }
  241. /**
  242. * {@inheritDoc}
  243. */
  244. public function count()
  245. {
  246. return count($this->elements);
  247. }
  248. /**
  249. * {@inheritDoc}
  250. */
  251. public function set($key, $value)
  252. {
  253. $this->elements[$key] = $value;
  254. }
  255. /**
  256. * {@inheritDoc}
  257. *
  258. * @psalm-suppress InvalidPropertyAssignmentValue
  259. *
  260. * This breaks assumptions about the template type, but it would
  261. * be a backwards-incompatible change to remove this method
  262. */
  263. public function add($element)
  264. {
  265. $this->elements[] = $element;
  266. return true;
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. public function isEmpty()
  272. {
  273. return empty($this->elements);
  274. }
  275. /**
  276. * Required by interface IteratorAggregate.
  277. *
  278. * {@inheritDoc}
  279. */
  280. public function getIterator()
  281. {
  282. return new ArrayIterator($this->elements);
  283. }
  284. /**
  285. * {@inheritDoc}
  286. *
  287. * @return static
  288. *
  289. * @psalm-template U
  290. * @psalm-param Closure(T=):U $func
  291. * @psalm-return static<TKey, U>
  292. */
  293. public function map(Closure $func)
  294. {
  295. return $this->createFrom(array_map($func, $this->elements));
  296. }
  297. /**
  298. * {@inheritDoc}
  299. *
  300. * @return static
  301. *
  302. * @psalm-return static<TKey,T>
  303. */
  304. public function filter(Closure $p)
  305. {
  306. return $this->createFrom(array_filter($this->elements, $p, ARRAY_FILTER_USE_BOTH));
  307. }
  308. /**
  309. * {@inheritDoc}
  310. */
  311. public function forAll(Closure $p)
  312. {
  313. foreach ($this->elements as $key => $element) {
  314. if (! $p($key, $element)) {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. /**
  321. * {@inheritDoc}
  322. */
  323. public function partition(Closure $p)
  324. {
  325. $matches = $noMatches = [];
  326. foreach ($this->elements as $key => $element) {
  327. if ($p($key, $element)) {
  328. $matches[$key] = $element;
  329. } else {
  330. $noMatches[$key] = $element;
  331. }
  332. }
  333. return [$this->createFrom($matches), $this->createFrom($noMatches)];
  334. }
  335. /**
  336. * Returns a string representation of this object.
  337. *
  338. * @return string
  339. */
  340. public function __toString()
  341. {
  342. return self::class . '@' . spl_object_hash($this);
  343. }
  344. /**
  345. * {@inheritDoc}
  346. */
  347. public function clear()
  348. {
  349. $this->elements = [];
  350. }
  351. /**
  352. * {@inheritDoc}
  353. */
  354. public function slice($offset, $length = null)
  355. {
  356. return array_slice($this->elements, $offset, $length, true);
  357. }
  358. /**
  359. * {@inheritDoc}
  360. */
  361. public function matching(Criteria $criteria)
  362. {
  363. $expr = $criteria->getWhereExpression();
  364. $filtered = $this->elements;
  365. if ($expr) {
  366. $visitor = new ClosureExpressionVisitor();
  367. $filter = $visitor->dispatch($expr);
  368. $filtered = array_filter($filtered, $filter);
  369. }
  370. $orderings = $criteria->getOrderings();
  371. if ($orderings) {
  372. $next = null;
  373. foreach (array_reverse($orderings) as $field => $ordering) {
  374. $next = ClosureExpressionVisitor::sortByField($field, $ordering === Criteria::DESC ? -1 : 1, $next);
  375. }
  376. uasort($filtered, $next);
  377. }
  378. $offset = $criteria->getFirstResult();
  379. $length = $criteria->getMaxResults();
  380. if ($offset || $length) {
  381. $filtered = array_slice($filtered, (int) $offset, $length);
  382. }
  383. return $this->createFrom($filtered);
  384. }
  385. }