ArrayCollection.php 8.4 KB

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