ArrayCollection.php 12 KB

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