ArrayCollection.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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]) || array_key_exists($key, $this->_elements)) {
  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]) || array_key_exists($key, $this->_elements);
  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. return in_array($element, $this->_elements, true);
  210. }
  211. /**
  212. * Tests for the existence of an element that satisfies the given predicate.
  213. *
  214. * @param Closure $p The predicate.
  215. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  216. */
  217. public function exists(Closure $p)
  218. {
  219. foreach ($this->_elements as $key => $element) {
  220. if ($p($key, $element)) {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. /**
  227. * Searches for a given element and, if found, returns the corresponding key/index
  228. * of that element. The comparison of two elements is strict, that means not
  229. * only the value but also the type must match.
  230. * For objects this means reference equality.
  231. *
  232. * @param mixed $element The element to search for.
  233. * @return mixed The key/index of the element or FALSE if the element was not found.
  234. */
  235. public function indexOf($element)
  236. {
  237. return array_search($element, $this->_elements, true);
  238. }
  239. /**
  240. * Gets the element with the given key/index.
  241. *
  242. * @param mixed $key The key.
  243. * @return mixed The element or NULL, if no element exists for the given key.
  244. */
  245. public function get($key)
  246. {
  247. if (isset($this->_elements[$key])) {
  248. return $this->_elements[$key];
  249. }
  250. return null;
  251. }
  252. /**
  253. * Gets all keys/indexes of the collection elements.
  254. *
  255. * @return array
  256. */
  257. public function getKeys()
  258. {
  259. return array_keys($this->_elements);
  260. }
  261. /**
  262. * Gets all elements.
  263. *
  264. * @return array
  265. */
  266. public function getValues()
  267. {
  268. return array_values($this->_elements);
  269. }
  270. /**
  271. * Returns the number of elements in the collection.
  272. *
  273. * Implementation of the Countable interface.
  274. *
  275. * @return integer The number of elements in the collection.
  276. */
  277. public function count()
  278. {
  279. return count($this->_elements);
  280. }
  281. /**
  282. * Adds/sets an element in the collection at the index / with the specified key.
  283. *
  284. * When the collection is a Map this is like put(key,value)/add(key,value).
  285. * When the collection is a List this is like add(position,value).
  286. *
  287. * @param mixed $key
  288. * @param mixed $value
  289. */
  290. public function set($key, $value)
  291. {
  292. $this->_elements[$key] = $value;
  293. }
  294. /**
  295. * Adds an element to the collection.
  296. *
  297. * @param mixed $value
  298. * @return boolean Always TRUE.
  299. */
  300. public function add($value)
  301. {
  302. $this->_elements[] = $value;
  303. return true;
  304. }
  305. /**
  306. * Checks whether the collection is empty.
  307. *
  308. * Note: This is preferable over count() == 0.
  309. *
  310. * @return boolean TRUE if the collection is empty, FALSE otherwise.
  311. */
  312. public function isEmpty()
  313. {
  314. return ! $this->_elements;
  315. }
  316. /**
  317. * Gets an iterator for iterating over the elements in the collection.
  318. *
  319. * @return ArrayIterator
  320. */
  321. public function getIterator()
  322. {
  323. return new ArrayIterator($this->_elements);
  324. }
  325. /**
  326. * Applies the given function to each element in the collection and returns
  327. * a new collection with the elements returned by the function.
  328. *
  329. * @param Closure $func
  330. * @return Collection
  331. */
  332. public function map(Closure $func)
  333. {
  334. return new static(array_map($func, $this->_elements));
  335. }
  336. /**
  337. * Returns all the elements of this collection that satisfy the predicate p.
  338. * The order of the elements is preserved.
  339. *
  340. * @param Closure $p The predicate used for filtering.
  341. * @return Collection A collection with the results of the filter operation.
  342. */
  343. public function filter(Closure $p)
  344. {
  345. return new static(array_filter($this->_elements, $p));
  346. }
  347. /**
  348. * Applies the given predicate p to all elements of this collection,
  349. * returning true, if the predicate yields true for all elements.
  350. *
  351. * @param Closure $p The predicate.
  352. * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  353. */
  354. public function forAll(Closure $p)
  355. {
  356. foreach ($this->_elements as $key => $element) {
  357. if ( ! $p($key, $element)) {
  358. return false;
  359. }
  360. }
  361. return true;
  362. }
  363. /**
  364. * Partitions this collection in two collections according to a predicate.
  365. * Keys are preserved in the resulting collections.
  366. *
  367. * @param Closure $p The predicate on which to partition.
  368. * @return array An array with two elements. The first element contains the collection
  369. * of elements where the predicate returned TRUE, the second element
  370. * contains the collection of elements where the predicate returned FALSE.
  371. */
  372. public function partition(Closure $p)
  373. {
  374. $coll1 = $coll2 = array();
  375. foreach ($this->_elements as $key => $element) {
  376. if ($p($key, $element)) {
  377. $coll1[$key] = $element;
  378. } else {
  379. $coll2[$key] = $element;
  380. }
  381. }
  382. return array(new static($coll1), new static($coll2));
  383. }
  384. /**
  385. * Returns a string representation of this object.
  386. *
  387. * @return string
  388. */
  389. public function __toString()
  390. {
  391. return __CLASS__ . '@' . spl_object_hash($this);
  392. }
  393. /**
  394. * Clears the collection.
  395. */
  396. public function clear()
  397. {
  398. $this->_elements = array();
  399. }
  400. /**
  401. * Extract a slice of $length elements starting at position $offset from the Collection.
  402. *
  403. * If $length is null it returns all elements from $offset to the end of the Collection.
  404. * Keys have to be preserved by this method. Calling this method will only return the
  405. * selected slice and NOT change the elements contained in the collection slice is called on.
  406. *
  407. * @param int $offset
  408. * @param int $length
  409. * @return array
  410. */
  411. public function slice($offset, $length = null)
  412. {
  413. return array_slice($this->_elements, $offset, $length, true);
  414. }
  415. /**
  416. * Select all elements from a selectable that match the criteria and
  417. * return a new collection containing these elements.
  418. *
  419. * @param Criteria $criteria
  420. * @return Collection
  421. */
  422. public function matching(Criteria $criteria)
  423. {
  424. $expr = $criteria->getWhereExpression();
  425. $filtered = $this->_elements;
  426. if ($expr) {
  427. $visitor = new ClosureExpressionVisitor();
  428. $filter = $visitor->dispatch($expr);
  429. $filtered = array_filter($filtered, $filter);
  430. }
  431. if ($orderings = $criteria->getOrderings()) {
  432. $next = null;
  433. foreach (array_reverse($orderings) as $field => $ordering) {
  434. $next = ClosureExpressionVisitor::sortByField($field, $ordering == 'DESC' ? -1 : 1, $next);
  435. }
  436. usort($filtered, $next);
  437. }
  438. $offset = $criteria->getFirstResult();
  439. $length = $criteria->getMaxResults();
  440. if ($offset || $length) {
  441. $filtered = array_slice($filtered, (int)$offset, $length);
  442. }
  443. return new static($filtered);
  444. }
  445. }