ArrayCollection.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Collections;
  20. use Closure, ArrayIterator;
  21. use Doctrine\Common\Collections\Expr\Expression;
  22. use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
  23. /**
  24. * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
  25. *
  26. * @since 2.0
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class ArrayCollection implements Collection, Selectable
  32. {
  33. /**
  34. * An array containing the entries of this collection.
  35. *
  36. * @var array
  37. */
  38. private $_elements;
  39. /**
  40. * Initializes a new ArrayCollection.
  41. *
  42. * @param array $elements
  43. */
  44. public function __construct(array $elements = array())
  45. {
  46. $this->_elements = $elements;
  47. }
  48. /**
  49. * Gets the PHP array representation of this collection.
  50. *
  51. * @return array The PHP array representation of this collection.
  52. */
  53. public function toArray()
  54. {
  55. return $this->_elements;
  56. }
  57. /**
  58. * Sets the internal iterator to the first element in the collection and
  59. * returns this element.
  60. *
  61. * @return mixed
  62. */
  63. public function first()
  64. {
  65. return reset($this->_elements);
  66. }
  67. /**
  68. * Sets the internal iterator to the last element in the collection and
  69. * returns this element.
  70. *
  71. * @return mixed
  72. */
  73. public function last()
  74. {
  75. return end($this->_elements);
  76. }
  77. /**
  78. * Gets the current key/index at the current internal iterator position.
  79. *
  80. * @return mixed
  81. */
  82. public function key()
  83. {
  84. return key($this->_elements);
  85. }
  86. /**
  87. * Moves the internal iterator position to the next element.
  88. *
  89. * @return mixed
  90. */
  91. public function next()
  92. {
  93. return next($this->_elements);
  94. }
  95. /**
  96. * Gets the element of the collection at the current internal iterator position.
  97. *
  98. * @return mixed
  99. */
  100. public function current()
  101. {
  102. return current($this->_elements);
  103. }
  104. /**
  105. * Removes an element with a specific key/index from the collection.
  106. *
  107. * @param mixed $key
  108. * @return mixed The removed element or NULL, if no element exists for the given key.
  109. */
  110. public function remove($key)
  111. {
  112. if (isset($this->_elements[$key])) {
  113. $removed = $this->_elements[$key];
  114. unset($this->_elements[$key]);
  115. return $removed;
  116. }
  117. return null;
  118. }
  119. /**
  120. * Removes the specified element from the collection, if it is found.
  121. *
  122. * @param mixed $element The element to remove.
  123. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  124. */
  125. public function removeElement($element)
  126. {
  127. $key = array_search($element, $this->_elements, true);
  128. if ($key !== false) {
  129. unset($this->_elements[$key]);
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * ArrayAccess implementation of offsetExists()
  136. *
  137. * @see containsKey()
  138. *
  139. * @param mixed $offset
  140. * @return bool
  141. */
  142. public function offsetExists($offset)
  143. {
  144. return $this->containsKey($offset);
  145. }
  146. /**
  147. * ArrayAccess implementation of offsetGet()
  148. *
  149. * @see get()
  150. *
  151. * @param mixed $offset
  152. * @return mixed
  153. */
  154. public function offsetGet($offset)
  155. {
  156. return $this->get($offset);
  157. }
  158. /**
  159. * ArrayAccess implementation of offsetSet()
  160. *
  161. * @see add()
  162. * @see set()
  163. *
  164. * @param mixed $offset
  165. * @param mixed $value
  166. * @return bool
  167. */
  168. public function offsetSet($offset, $value)
  169. {
  170. if ( ! isset($offset)) {
  171. return $this->add($value);
  172. }
  173. return $this->set($offset, $value);
  174. }
  175. /**
  176. * ArrayAccess implementation of offsetUnset()
  177. *
  178. * @see remove()
  179. *
  180. * @param mixed $offset
  181. * @return mixed
  182. */
  183. public function offsetUnset($offset)
  184. {
  185. return $this->remove($offset);
  186. }
  187. /**
  188. * Checks whether the collection contains a specific key/index.
  189. *
  190. * @param mixed $key The key to check for.
  191. * @return boolean TRUE if the given key/index exists, FALSE otherwise.
  192. */
  193. public function containsKey($key)
  194. {
  195. return isset($this->_elements[$key]);
  196. }
  197. /**
  198. * Checks whether the given element is contained in the collection.
  199. * Only element values are compared, not keys. The comparison of two elements
  200. * is strict, that means not only the value but also the type must match.
  201. * For objects this means reference equality.
  202. *
  203. * @param mixed $element
  204. * @return boolean TRUE if the given element is contained in the collection,
  205. * FALSE otherwise.
  206. */
  207. public function contains($element)
  208. {
  209. foreach ($this->_elements as $collectionElement) {
  210. if ($element === $collectionElement) {
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. /**
  217. * Tests for the existence of an element that satisfies the given predicate.
  218. *
  219. * @param Closure $p The predicate.
  220. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  221. */
  222. public function exists(Closure $p)
  223. {
  224. foreach ($this->_elements as $key => $element) {
  225. if ($p($key, $element)) {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. /**
  232. * Searches for a given element and, if found, returns the corresponding key/index
  233. * of that element. The comparison of two elements is strict, that means not
  234. * only the value but also the type must match.
  235. * For objects this means reference equality.
  236. *
  237. * @param mixed $element The element to search for.
  238. * @return mixed The key/index of the element or FALSE if the element was not found.
  239. */
  240. public function indexOf($element)
  241. {
  242. return array_search($element, $this->_elements, true);
  243. }
  244. /**
  245. * Gets the element with the given key/index.
  246. *
  247. * @param mixed $key The key.
  248. * @return mixed The element or NULL, if no element exists for the given key.
  249. */
  250. public function get($key)
  251. {
  252. if (isset($this->_elements[$key])) {
  253. return $this->_elements[$key];
  254. }
  255. return null;
  256. }
  257. /**
  258. * Gets all keys/indexes of the collection elements.
  259. *
  260. * @return array
  261. */
  262. public function getKeys()
  263. {
  264. return array_keys($this->_elements);
  265. }
  266. /**
  267. * Gets all elements.
  268. *
  269. * @return array
  270. */
  271. public function getValues()
  272. {
  273. return array_values($this->_elements);
  274. }
  275. /**
  276. * Returns the number of elements in the collection.
  277. *
  278. * Implementation of the Countable interface.
  279. *
  280. * @return integer The number of elements in the collection.
  281. */
  282. public function count()
  283. {
  284. return count($this->_elements);
  285. }
  286. /**
  287. * Adds/sets an element in the collection at the index / with the specified key.
  288. *
  289. * When the collection is a Map this is like put(key,value)/add(key,value).
  290. * When the collection is a List this is like add(position,value).
  291. *
  292. * @param mixed $key
  293. * @param mixed $value
  294. */
  295. public function set($key, $value)
  296. {
  297. $this->_elements[$key] = $value;
  298. }
  299. /**
  300. * Adds an element to the collection.
  301. *
  302. * @param mixed $value
  303. * @return boolean Always TRUE.
  304. */
  305. public function add($value)
  306. {
  307. $this->_elements[] = $value;
  308. return true;
  309. }
  310. /**
  311. * Checks whether the collection is empty.
  312. *
  313. * Note: This is preferable over count() == 0.
  314. *
  315. * @return boolean TRUE if the collection is empty, FALSE otherwise.
  316. */
  317. public function isEmpty()
  318. {
  319. return ! $this->_elements;
  320. }
  321. /**
  322. * Gets an iterator for iterating over the elements in the collection.
  323. *
  324. * @return ArrayIterator
  325. */
  326. public function getIterator()
  327. {
  328. return new ArrayIterator($this->_elements);
  329. }
  330. /**
  331. * Applies the given function to each element in the collection and returns
  332. * a new collection with the elements returned by the function.
  333. *
  334. * @param Closure $func
  335. * @return Collection
  336. */
  337. public function map(Closure $func)
  338. {
  339. return new static(array_map($func, $this->_elements));
  340. }
  341. /**
  342. * Returns all the elements of this collection that satisfy the predicate p.
  343. * The order of the elements is preserved.
  344. *
  345. * @param Closure $p The predicate used for filtering.
  346. * @return Collection A collection with the results of the filter operation.
  347. */
  348. public function filter(Closure $p)
  349. {
  350. return new static(array_filter($this->_elements, $p));
  351. }
  352. /**
  353. * Applies the given predicate p to all elements of this collection,
  354. * returning true, if the predicate yields true for all elements.
  355. *
  356. * @param Closure $p The predicate.
  357. * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  358. */
  359. public function forAll(Closure $p)
  360. {
  361. foreach ($this->_elements as $key => $element) {
  362. if ( ! $p($key, $element)) {
  363. return false;
  364. }
  365. }
  366. return true;
  367. }
  368. /**
  369. * Partitions this collection in two collections according to a predicate.
  370. * Keys are preserved in the resulting collections.
  371. *
  372. * @param Closure $p The predicate on which to partition.
  373. * @return array An array with two elements. The first element contains the collection
  374. * of elements where the predicate returned TRUE, the second element
  375. * contains the collection of elements where the predicate returned FALSE.
  376. */
  377. public function partition(Closure $p)
  378. {
  379. $coll1 = $coll2 = array();
  380. foreach ($this->_elements as $key => $element) {
  381. if ($p($key, $element)) {
  382. $coll1[$key] = $element;
  383. } else {
  384. $coll2[$key] = $element;
  385. }
  386. }
  387. return array(new static($coll1), new static($coll2));
  388. }
  389. /**
  390. * Returns a string representation of this object.
  391. *
  392. * @return string
  393. */
  394. public function __toString()
  395. {
  396. return __CLASS__ . '@' . spl_object_hash($this);
  397. }
  398. /**
  399. * Clears the collection.
  400. */
  401. public function clear()
  402. {
  403. $this->_elements = array();
  404. }
  405. /**
  406. * Extract a slice of $length elements starting at position $offset from the Collection.
  407. *
  408. * If $length is null it returns all elements from $offset to the end of the Collection.
  409. * Keys have to be preserved by this method. Calling this method will only return the
  410. * selected slice and NOT change the elements contained in the collection slice is called on.
  411. *
  412. * @param int $offset
  413. * @param int $length
  414. * @return array
  415. */
  416. public function slice($offset, $length = null)
  417. {
  418. return array_slice($this->_elements, $offset, $length, true);
  419. }
  420. /**
  421. * Select all elements from a selectable that match the criteria and
  422. * return a new collection containing these elements.
  423. *
  424. * @param Criteria $criteria
  425. * @return Collection
  426. */
  427. public function matching(Criteria $criteria)
  428. {
  429. $expr = $criteria->getWhereExpression();
  430. $filtered = $this->_elements;
  431. if ($expr) {
  432. $visitor = new ClosureExpressionVisitor();
  433. $filter = $visitor->dispatch($expr);
  434. $filtered = array_filter($filtered, $filter);
  435. }
  436. if ($orderings = $criteria->getOrderings()) {
  437. $next = null;
  438. foreach (array_reverse($orderings) as $field => $ordering) {
  439. $next = ClosureExpressionVisitor::sortByField($field, $ordering == 'DESC' ? -1 : 1, $next);
  440. }
  441. usort($filtered, $next);
  442. }
  443. $offset = $criteria->getFirstResult();
  444. $length = $criteria->getMaxResults();
  445. if ($offset || $length) {
  446. $filtered = array_slice($filtered, (int)$offset, $length);
  447. }
  448. return new static($filtered);
  449. }
  450. }