ArrayCollection.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Collections;
  20. use Closure, ArrayIterator;
  21. /**
  22. * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
  23. *
  24. * @since 2.0
  25. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  26. * @author Jonathan Wage <jonwage@gmail.com>
  27. * @author Roman Borschel <roman@code-factory.org>
  28. */
  29. class ArrayCollection implements Collection
  30. {
  31. /**
  32. * An array containing the entries of this collection.
  33. *
  34. * @var array
  35. */
  36. private $_elements;
  37. /**
  38. * Initializes a new ArrayCollection.
  39. *
  40. * @param array $elements
  41. */
  42. public function __construct(array $elements = array())
  43. {
  44. $this->_elements = $elements;
  45. }
  46. /**
  47. * Gets the PHP array representation of this collection.
  48. *
  49. * @return array The PHP array representation of this collection.
  50. */
  51. public function toArray()
  52. {
  53. return $this->_elements;
  54. }
  55. /**
  56. * Sets the internal iterator to the first element in the collection and
  57. * returns this element.
  58. *
  59. * @return mixed
  60. */
  61. public function first()
  62. {
  63. return reset($this->_elements);
  64. }
  65. /**
  66. * Sets the internal iterator to the last element in the collection and
  67. * returns this element.
  68. *
  69. * @return mixed
  70. */
  71. public function last()
  72. {
  73. return end($this->_elements);
  74. }
  75. /**
  76. * Gets the current key/index at the current internal iterator position.
  77. *
  78. * @return mixed
  79. */
  80. public function key()
  81. {
  82. return key($this->_elements);
  83. }
  84. /**
  85. * Moves the internal iterator position to the next element.
  86. *
  87. * @return mixed
  88. */
  89. public function next()
  90. {
  91. return next($this->_elements);
  92. }
  93. /**
  94. * Gets the element of the collection at the current internal iterator position.
  95. *
  96. * @return mixed
  97. */
  98. public function current()
  99. {
  100. return current($this->_elements);
  101. }
  102. /**
  103. * Removes an element with a specific key/index from the collection.
  104. *
  105. * @param mixed $key
  106. * @return mixed The removed element or NULL, if no element exists for the given key.
  107. */
  108. public function remove($key)
  109. {
  110. if (isset($this->_elements[$key])) {
  111. $removed = $this->_elements[$key];
  112. unset($this->_elements[$key]);
  113. return $removed;
  114. }
  115. return null;
  116. }
  117. /**
  118. * Removes the specified element from the collection, if it is found.
  119. *
  120. * @param mixed $element The element to remove.
  121. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  122. */
  123. public function removeElement($element)
  124. {
  125. $key = array_search($element, $this->_elements, true);
  126. if ($key !== false) {
  127. unset($this->_elements[$key]);
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * ArrayAccess implementation of offsetExists()
  134. *
  135. * @see containsKey()
  136. */
  137. public function offsetExists($offset)
  138. {
  139. return $this->containsKey($offset);
  140. }
  141. /**
  142. * ArrayAccess implementation of offsetGet()
  143. *
  144. * @see get()
  145. */
  146. public function offsetGet($offset)
  147. {
  148. return $this->get($offset);
  149. }
  150. /**
  151. * ArrayAccess implementation of offsetGet()
  152. *
  153. * @see add()
  154. * @see set()
  155. */
  156. public function offsetSet($offset, $value)
  157. {
  158. if ( ! isset($offset)) {
  159. return $this->add($value);
  160. }
  161. return $this->set($offset, $value);
  162. }
  163. /**
  164. * ArrayAccess implementation of offsetUnset()
  165. *
  166. * @see remove()
  167. */
  168. public function offsetUnset($offset)
  169. {
  170. return $this->remove($offset);
  171. }
  172. /**
  173. * Checks whether the collection contains a specific key/index.
  174. *
  175. * @param mixed $key The key to check for.
  176. * @return boolean TRUE if the given key/index exists, FALSE otherwise.
  177. */
  178. public function containsKey($key)
  179. {
  180. return isset($this->_elements[$key]);
  181. }
  182. /**
  183. * Checks whether the given element is contained in the collection.
  184. * Only element values are compared, not keys. The comparison of two elements
  185. * is strict, that means not only the value but also the type must match.
  186. * For objects this means reference equality.
  187. *
  188. * @param mixed $element
  189. * @return boolean TRUE if the given element is contained in the collection,
  190. * FALSE otherwise.
  191. */
  192. public function contains($element)
  193. {
  194. return in_array($element, $this->_elements, true);
  195. }
  196. /**
  197. * Tests for the existance of an element that satisfies the given predicate.
  198. *
  199. * @param Closure $p The predicate.
  200. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  201. */
  202. public function exists(Closure $p)
  203. {
  204. foreach ($this->_elements as $key => $element) {
  205. if ($p($key, $element)) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * Searches for a given element and, if found, returns the corresponding key/index
  213. * of that element. The comparison of two elements is strict, that means not
  214. * only the value but also the type must match.
  215. * For objects this means reference equality.
  216. *
  217. * @param mixed $element The element to search for.
  218. * @return mixed The key/index of the element or FALSE if the element was not found.
  219. */
  220. public function indexOf($element)
  221. {
  222. return array_search($element, $this->_elements, true);
  223. }
  224. /**
  225. * Gets the element with the given key/index.
  226. *
  227. * @param mixed $key The key.
  228. * @return mixed The element or NULL, if no element exists for the given key.
  229. */
  230. public function get($key)
  231. {
  232. if (isset($this->_elements[$key])) {
  233. return $this->_elements[$key];
  234. }
  235. return null;
  236. }
  237. /**
  238. * Gets all keys/indexes of the collection elements.
  239. *
  240. * @return array
  241. */
  242. public function getKeys()
  243. {
  244. return array_keys($this->_elements);
  245. }
  246. /**
  247. * Gets all elements.
  248. *
  249. * @return array
  250. */
  251. public function getValues()
  252. {
  253. return array_values($this->_elements);
  254. }
  255. /**
  256. * Returns the number of elements in the collection.
  257. *
  258. * Implementation of the Countable interface.
  259. *
  260. * @return integer The number of elements in the collection.
  261. */
  262. public function count()
  263. {
  264. return count($this->_elements);
  265. }
  266. /**
  267. * Adds/sets an element in the collection at the index / with the specified key.
  268. *
  269. * When the collection is a Map this is like put(key,value)/add(key,value).
  270. * When the collection is a List this is like add(position,value).
  271. *
  272. * @param mixed $key
  273. * @param mixed $value
  274. */
  275. public function set($key, $value)
  276. {
  277. $this->_elements[$key] = $value;
  278. }
  279. /**
  280. * Adds an element to the collection.
  281. *
  282. * @param mixed $value
  283. * @return boolean Always TRUE.
  284. */
  285. public function add($value)
  286. {
  287. $this->_elements[] = $value;
  288. return true;
  289. }
  290. /**
  291. * Checks whether the collection is empty.
  292. *
  293. * Note: This is preferrable over count() == 0.
  294. *
  295. * @return boolean TRUE if the collection is empty, FALSE otherwise.
  296. */
  297. public function isEmpty()
  298. {
  299. return ! $this->_elements;
  300. }
  301. /**
  302. * Gets an iterator for iterating over the elements in the collection.
  303. *
  304. * @return ArrayIterator
  305. */
  306. public function getIterator()
  307. {
  308. return new ArrayIterator($this->_elements);
  309. }
  310. /**
  311. * Applies the given function to each element in the collection and returns
  312. * a new collection with the elements returned by the function.
  313. *
  314. * @param Closure $func
  315. * @return Collection
  316. */
  317. public function map(Closure $func)
  318. {
  319. return new static(array_map($func, $this->_elements));
  320. }
  321. /**
  322. * Returns all the elements of this collection that satisfy the predicate p.
  323. * The order of the elements is preserved.
  324. *
  325. * @param Closure $p The predicate used for filtering.
  326. * @return Collection A collection with the results of the filter operation.
  327. */
  328. public function filter(Closure $p)
  329. {
  330. return new static(array_filter($this->_elements, $p));
  331. }
  332. /**
  333. * Applies the given predicate p to all elements of this collection,
  334. * returning true, if the predicate yields true for all elements.
  335. *
  336. * @param Closure $p The predicate.
  337. * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  338. */
  339. public function forAll(Closure $p)
  340. {
  341. foreach ($this->_elements as $key => $element) {
  342. if ( ! $p($key, $element)) {
  343. return false;
  344. }
  345. }
  346. return true;
  347. }
  348. /**
  349. * Partitions this collection in two collections according to a predicate.
  350. * Keys are preserved in the resulting collections.
  351. *
  352. * @param Closure $p The predicate on which to partition.
  353. * @return array An array with two elements. The first element contains the collection
  354. * of elements where the predicate returned TRUE, the second element
  355. * contains the collection of elements where the predicate returned FALSE.
  356. */
  357. public function partition(Closure $p)
  358. {
  359. $coll1 = $coll2 = array();
  360. foreach ($this->_elements as $key => $element) {
  361. if ($p($key, $element)) {
  362. $coll1[$key] = $element;
  363. } else {
  364. $coll2[$key] = $element;
  365. }
  366. }
  367. return array(new static($coll1), new static($coll2));
  368. }
  369. /**
  370. * Returns a string representation of this object.
  371. *
  372. * @return string
  373. */
  374. public function __toString()
  375. {
  376. return __CLASS__ . '@' . spl_object_hash($this);
  377. }
  378. /**
  379. * Clears the collection.
  380. */
  381. public function clear()
  382. {
  383. $this->_elements = array();
  384. }
  385. /**
  386. * Extract a slice of $length elements starting at position $offset from the Collection.
  387. *
  388. * If $length is null it returns all elements from $offset to the end of the Collection.
  389. * Keys have to be preserved by this method. Calling this method will only return the
  390. * selected slice and NOT change the elements contained in the collection slice is called on.
  391. *
  392. * @param int $offset
  393. * @param int $length
  394. * @return array
  395. */
  396. public function slice($offset, $length = null)
  397. {
  398. return array_slice($this->_elements, $offset, $length, true);
  399. }
  400. }