index.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. Introduction
  2. ============
  3. Doctrine Collections is a library that contains classes for working with
  4. arrays of data. Here is an example using the simple
  5. ``Doctrine\Common\Collections\ArrayCollection`` class:
  6. .. code-block:: php
  7. <?php
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. $collection = new ArrayCollection([1, 2, 3]);
  10. $filteredCollection = $collection->filter(function($element) {
  11. return $element > 1;
  12. }); // [2, 3]
  13. Collection Methods
  14. ==================
  15. Doctrine Collections provides an interface named ``Doctrine\Common\Collections\Collection``
  16. that resembles the nature of a regular PHP array. That is,
  17. it is essentially an **ordered map** that can also be used
  18. like a list.
  19. A Collection has an internal iterator just like a PHP array. In addition,
  20. a Collection can be iterated with external iterators, which is preferable.
  21. To use an external iterator simply use the foreach language construct to
  22. iterate over the collection, which calls ``getIterator()`` internally, or
  23. explicitly retrieve an iterator though ``getIterator()`` which can then be
  24. used to iterate over the collection. You can not rely on the internal iterator
  25. of the collection being at a certain position unless you explicitly positioned it before.
  26. Methods that do not alter the collection or have template types
  27. appearing in invariant or contravariant positions are not directly
  28. defined in ``Doctrine\Common\Collections\Collection``, but are inherited
  29. from the ``Doctrine\Common\Collections\ReadableCollection`` interface.
  30. The methods available on the interface are:
  31. add
  32. ---
  33. Adds an element at the end of the collection.
  34. .. code-block:: php
  35. $collection->add('test');
  36. clear
  37. -----
  38. Clears the collection, removing all elements.
  39. .. code-block:: php
  40. $collection->clear();
  41. contains
  42. --------
  43. Checks whether an element is contained in the collection. This is an O(n) operation, where n is the size of the collection.
  44. .. code-block:: php
  45. $collection = new Collection(['test']);
  46. $contains = $collection->contains('test'); // true
  47. containsKey
  48. -----------
  49. Checks whether the collection contains an element with the specified key/index.
  50. .. code-block:: php
  51. $collection = new Collection(['test' => true]);
  52. $contains = $collection->containsKey('test'); // true
  53. current
  54. -------
  55. Gets the element of the collection at the current iterator position.
  56. .. code-block:: php
  57. $collection = new Collection(['first', 'second', 'third']);
  58. $current = $collection->current(); // first
  59. get
  60. ---
  61. Gets the element at the specified key/index.
  62. .. code-block:: php
  63. $collection = new Collection([
  64. 'key' => 'value',
  65. ]);
  66. $value = $collection->get('key'); // value
  67. getKeys
  68. -------
  69. Gets all keys/indices of the collection.
  70. .. code-block:: php
  71. $collection = new Collection(['a', 'b', 'c']);
  72. $keys = $collection->getKeys(); // [0, 1, 2]
  73. getValues
  74. ---------
  75. Gets all values of the collection.
  76. .. code-block:: php
  77. $collection = new Collection([
  78. 'key1' => 'value1',
  79. 'key2' => 'value2',
  80. 'key3' => 'value3',
  81. ]);
  82. $values = $collection->getValues(); // ['value1', 'value2', 'value3']
  83. isEmpty
  84. -------
  85. Checks whether the collection is empty (contains no elements).
  86. .. code-block:: php
  87. $collection = new Collection(['a', 'b', 'c']);
  88. $isEmpty = $collection->isEmpty(); // false
  89. first
  90. -----
  91. Sets the internal iterator to the first element in the collection and returns this element.
  92. .. code-block:: php
  93. $collection = new Collection(['first', 'second', 'third']);
  94. $first = $collection->first(); // first
  95. exists
  96. ------
  97. Tests for the existence of an element that satisfies the given predicate.
  98. .. code-block:: php
  99. $collection = new Collection(['first', 'second', 'third']);
  100. $exists = $collection->exists(function($key, $value) {
  101. return $value === 'first';
  102. }); // true
  103. filter
  104. ------
  105. Returns all the elements of this collection for which your callback function returns `true`.
  106. The order and keys of the elements are preserved.
  107. .. code-block:: php
  108. $collection = new ArrayCollection([1, 2, 3]);
  109. $filteredCollection = $collection->filter(function($element) {
  110. return $element > 1;
  111. }); // [2, 3]
  112. forAll
  113. ------
  114. Tests whether the given predicate holds for all elements of this collection.
  115. .. code-block:: php
  116. $collection = new ArrayCollection([1, 2, 3]);
  117. $forAll = $collection->forAll(function($key, $value) {
  118. return $value > 1;
  119. }); // false
  120. indexOf
  121. -------
  122. Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match. For objects this means reference equality.
  123. .. code-block:: php
  124. $collection = new ArrayCollection([1, 2, 3]);
  125. $indexOf = $collection->indexOf(3); // 2
  126. key
  127. ---
  128. Gets the key/index of the element at the current iterator position.
  129. .. code-block:: php
  130. $collection = new ArrayCollection([1, 2, 3]);
  131. $collection->next();
  132. $key = $collection->key(); // 1
  133. last
  134. ----
  135. Sets the internal iterator to the last element in the collection and returns this element.
  136. .. code-block:: php
  137. $collection = new ArrayCollection([1, 2, 3]);
  138. $last = $collection->last(); // 3
  139. map
  140. ---
  141. Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
  142. .. code-block:: php
  143. $collection = new ArrayCollection([1, 2, 3]);
  144. $mappedCollection = $collection->map(function($value) {
  145. return $value + 1;
  146. }); // [2, 3, 4]
  147. next
  148. ----
  149. Moves the internal iterator position to the next element and returns this element.
  150. .. code-block:: php
  151. $collection = new ArrayCollection([1, 2, 3]);
  152. $next = $collection->next(); // 2
  153. partition
  154. ---------
  155. Partitions this collection in two collections according to a predicate. Keys are preserved in the resulting collections.
  156. .. code-block:: php
  157. $collection = new ArrayCollection([1, 2, 3]);
  158. $mappedCollection = $collection->partition(function($key, $value) {
  159. return $value > 1
  160. }); // [[2, 3], [1]]
  161. remove
  162. ------
  163. Removes the element at the specified index from the collection.
  164. .. code-block:: php
  165. $collection = new ArrayCollection([1, 2, 3]);
  166. $collection->remove(0); // [2, 3]
  167. removeElement
  168. -------------
  169. Removes the specified element from the collection, if it is found.
  170. .. code-block:: php
  171. $collection = new ArrayCollection([1, 2, 3]);
  172. $collection->removeElement(3); // [1, 2]
  173. set
  174. ---
  175. Sets an element in the collection at the specified key/index.
  176. .. code-block:: php
  177. $collection = new ArrayCollection();
  178. $collection->set('name', 'jwage');
  179. slice
  180. -----
  181. Extracts a slice of $length elements starting at position $offset from the Collection. If $length is null it returns all elements from $offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.
  182. .. code-block:: php
  183. $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
  184. $slice = $collection->slice(1, 2); // [1, 2]
  185. toArray
  186. -------
  187. Gets a native PHP array representation of the collection.
  188. .. code-block:: php
  189. $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
  190. $array = $collection->toArray(); // [0, 1, 2, 3, 4, 5]
  191. Selectable Methods
  192. ==================
  193. Some Doctrine Collections, like ``Doctrine\Common\Collections\ArrayCollection``,
  194. implement an interface named ``Doctrine\Common\Collections\Selectable``
  195. that offers the usage of a powerful expressions API, where conditions
  196. can be applied to a collection to get a result with matching elements
  197. only.
  198. matching
  199. --------
  200. Selects all elements from a selectable that match the expression and
  201. returns a new collection containing these elements.
  202. .. code-block:: php
  203. use Doctrine\Common\Collections\Criteria;
  204. use Doctrine\Common\Collections\Expr\Comparison;
  205. $collection = new ArrayCollection([
  206. [
  207. 'name' => 'jwage',
  208. ],
  209. [
  210. 'name' => 'romanb',
  211. ],
  212. ]);
  213. $expr = new Comparison('name', '=', 'jwage');
  214. $criteria = new Criteria();
  215. $criteria->where($expr);
  216. $matched = $collection->matching($criteria); // ['jwage']
  217. You can read more about expressions :ref:`here <expressions>`.