summaryrefslogtreecommitdiff
path: root/vendor/doctrine/collections/docs/en/index.rst
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/collections/docs/en/index.rst')
-rw-r--r--vendor/doctrine/collections/docs/en/index.rst357
1 files changed, 357 insertions, 0 deletions
diff --git a/vendor/doctrine/collections/docs/en/index.rst b/vendor/doctrine/collections/docs/en/index.rst
new file mode 100644
index 0000000..7f2e38c
--- /dev/null
+++ b/vendor/doctrine/collections/docs/en/index.rst
@@ -0,0 +1,357 @@
1Introduction
2============
3
4Doctrine Collections is a library that contains classes for working with
5arrays of data. Here is an example using the simple
6``Doctrine\Common\Collections\ArrayCollection`` class:
7
8.. code-block:: php
9 <?php
10
11 use Doctrine\Common\Collections\ArrayCollection;
12
13 $collection = new ArrayCollection([1, 2, 3]);
14
15 $filteredCollection = $collection->filter(function($element) {
16 return $element > 1;
17 }); // [2, 3]
18
19Collection Methods
20==================
21
22Doctrine Collections provides an interface named ``Doctrine\Common\Collections\Collection``
23that resembles the nature of a regular PHP array. That is,
24it is essentially an **ordered map** that can also be used
25like a list.
26
27A Collection has an internal iterator just like a PHP array. In addition,
28a Collection can be iterated with external iterators, which is preferable.
29To use an external iterator simply use the foreach language construct to
30iterate over the collection, which calls ``getIterator()`` internally, or
31explicitly retrieve an iterator though ``getIterator()`` which can then be
32used to iterate over the collection. You can not rely on the internal iterator
33of the collection being at a certain position unless you explicitly positioned it before.
34
35Methods that do not alter the collection or have template types
36appearing in invariant or contravariant positions are not directly
37defined in ``Doctrine\Common\Collections\Collection``, but are inherited
38from the ``Doctrine\Common\Collections\ReadableCollection`` interface.
39
40The methods available on the interface are:
41
42add
43---
44
45Adds an element at the end of the collection.
46
47.. code-block:: php
48 $collection->add('test');
49
50clear
51-----
52
53Clears the collection, removing all elements.
54
55.. code-block:: php
56 $collection->clear();
57
58contains
59--------
60
61Checks whether an element is contained in the collection. This is an O(n) operation, where n is the size of the collection.
62
63.. code-block:: php
64 $collection = new Collection(['test']);
65
66 $contains = $collection->contains('test'); // true
67
68containsKey
69-----------
70
71Checks whether the collection contains an element with the specified key/index.
72
73.. code-block:: php
74 $collection = new Collection(['test' => true]);
75
76 $contains = $collection->containsKey('test'); // true
77
78current
79-------
80
81Gets the element of the collection at the current iterator position.
82
83.. code-block:: php
84 $collection = new Collection(['first', 'second', 'third']);
85
86 $current = $collection->current(); // first
87
88get
89---
90
91Gets the element at the specified key/index.
92
93.. code-block:: php
94 $collection = new Collection([
95 'key' => 'value',
96 ]);
97
98 $value = $collection->get('key'); // value
99
100getKeys
101-------
102
103Gets all keys/indices of the collection.
104
105.. code-block:: php
106 $collection = new Collection(['a', 'b', 'c']);
107
108 $keys = $collection->getKeys(); // [0, 1, 2]
109
110getValues
111---------
112
113Gets all values of the collection.
114
115.. code-block:: php
116 $collection = new Collection([
117 'key1' => 'value1',
118 'key2' => 'value2',
119 'key3' => 'value3',
120 ]);
121
122 $values = $collection->getValues(); // ['value1', 'value2', 'value3']
123
124isEmpty
125-------
126
127Checks whether the collection is empty (contains no elements).
128
129.. code-block:: php
130 $collection = new Collection(['a', 'b', 'c']);
131
132 $isEmpty = $collection->isEmpty(); // false
133
134first
135-----
136
137Sets the internal iterator to the first element in the collection and returns this element.
138
139.. code-block:: php
140 $collection = new Collection(['first', 'second', 'third']);
141
142 $first = $collection->first(); // first
143
144exists
145------
146
147Tests for the existence of an element that satisfies the given predicate.
148
149.. code-block:: php
150 $collection = new Collection(['first', 'second', 'third']);
151
152 $exists = $collection->exists(function($key, $value) {
153 return $value === 'first';
154 }); // true
155
156findFirst
157---------
158
159Returns the first element of this collection that satisfies the given predicate.
160
161.. code-block:: php
162 $collection = new Collection([1, 2, 3, 2, 1]);
163
164 $one = $collection->findFirst(function(int $key, int $value): bool {
165 return $value > 2 && $key > 1;
166 }); // 3
167
168filter
169------
170
171Returns all the elements of this collection for which your callback function returns `true`.
172The order and keys of the elements are preserved.
173
174.. code-block:: php
175 $collection = new ArrayCollection([1, 2, 3]);
176
177 $filteredCollection = $collection->filter(function($element) {
178 return $element > 1;
179 }); // [2, 3]
180
181forAll
182------
183
184Tests whether the given predicate holds for all elements of this collection.
185
186.. code-block:: php
187 $collection = new ArrayCollection([1, 2, 3]);
188
189 $forAll = $collection->forAll(function($key, $value) {
190 return $value > 1;
191 }); // false
192
193indexOf
194-------
195
196Gets 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.
197
198.. code-block:: php
199 $collection = new ArrayCollection([1, 2, 3]);
200
201 $indexOf = $collection->indexOf(3); // 2
202
203key
204---
205
206Gets the key/index of the element at the current iterator position.
207
208.. code-block:: php
209 $collection = new ArrayCollection([1, 2, 3]);
210
211 $collection->next();
212
213 $key = $collection->key(); // 1
214
215last
216----
217
218Sets the internal iterator to the last element in the collection and returns this element.
219
220.. code-block:: php
221 $collection = new ArrayCollection([1, 2, 3]);
222
223 $last = $collection->last(); // 3
224
225map
226---
227
228Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
229
230.. code-block:: php
231 $collection = new ArrayCollection([1, 2, 3]);
232
233 $mappedCollection = $collection->map(function($value) {
234 return $value + 1;
235 }); // [2, 3, 4]
236
237reduce
238------
239
240Applies iteratively the given function to each element in the collection, so as to reduce the collection to a single value.
241
242.. code-block:: php
243 $collection = new ArrayCollection([1, 2, 3]);
244
245 $reduce = $collection->reduce(function(int $accumulator, int $value): int {
246 return $accumulator + $value;
247 }, 0); // 6
248
249next
250----
251
252Moves the internal iterator position to the next element and returns this element.
253
254.. code-block:: php
255 $collection = new ArrayCollection([1, 2, 3]);
256
257 $next = $collection->next(); // 2
258
259partition
260---------
261
262Partitions this collection in two collections according to a predicate. Keys are preserved in the resulting collections.
263
264.. code-block:: php
265 $collection = new ArrayCollection([1, 2, 3]);
266
267 $mappedCollection = $collection->partition(function($key, $value) {
268 return $value > 1
269 }); // [[2, 3], [1]]
270
271remove
272------
273
274Removes the element at the specified index from the collection.
275
276.. code-block:: php
277 $collection = new ArrayCollection([1, 2, 3]);
278
279 $collection->remove(0); // [2, 3]
280
281removeElement
282-------------
283
284Removes the specified element from the collection, if it is found.
285
286.. code-block:: php
287 $collection = new ArrayCollection([1, 2, 3]);
288
289 $collection->removeElement(3); // [1, 2]
290
291set
292---
293
294Sets an element in the collection at the specified key/index.
295
296.. code-block:: php
297 $collection = new ArrayCollection();
298
299 $collection->set('name', 'jwage');
300
301slice
302-----
303
304Extracts 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.
305
306.. code-block:: php
307 $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
308
309 $slice = $collection->slice(1, 2); // [1, 2]
310
311toArray
312-------
313
314Gets a native PHP array representation of the collection.
315
316.. code-block:: php
317 $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
318
319 $array = $collection->toArray(); // [0, 1, 2, 3, 4, 5]
320
321Selectable Methods
322==================
323
324Some Doctrine Collections, like ``Doctrine\Common\Collections\ArrayCollection``,
325implement an interface named ``Doctrine\Common\Collections\Selectable``
326that offers the usage of a powerful expressions API, where conditions
327can be applied to a collection to get a result with matching elements
328only.
329
330matching
331--------
332
333Selects all elements from a selectable that match the expression and
334returns a new collection containing these elements and preserved keys.
335
336.. code-block:: php
337 use Doctrine\Common\Collections\Criteria;
338 use Doctrine\Common\Collections\Expr\Comparison;
339
340 $collection = new ArrayCollection([
341 'wage' => [
342 'name' => 'jwage',
343 ],
344 'roman' => [
345 'name' => 'romanb',
346 ],
347 ]);
348
349 $expr = new Comparison('name', '=', 'jwage');
350
351 $criteria = new Criteria();
352
353 $criteria->where($expr);
354
355 $matchingCollection = $collection->matching($criteria); // [ 'wage' => [ 'name' => 'jwage' ]]
356
357You can read more about expressions :ref:`here <expressions>`.