summaryrefslogtreecommitdiffstats
path: root/vendor/doctrine/collections/docs/en/index.rst
blob: 8ca97229b4a8fe9d44df4a87e9960a76425f2394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
Introduction
============

Doctrine Collections is a library that contains classes for working with
arrays of data. Here is an example using the simple
``Doctrine\Common\Collections\ArrayCollection`` class:

.. code-block:: php
    <?php

    use Doctrine\Common\Collections\ArrayCollection;

    $collection = new ArrayCollection([1, 2, 3]);

    $filteredCollection = $collection->filter(function($element) {
        return $element > 1;
    }); // [2, 3]

Collection Methods
==================

Doctrine Collections provides an interface named ``Doctrine\Common\Collections\Collection``
that resembles the nature of a regular PHP array. That is,
it is essentially an **ordered map** that can also be used
like a list.

A Collection has an internal iterator just like a PHP array. In addition,
a Collection can be iterated with external iterators, which is preferable.
To use an external iterator simply use the foreach language construct to
iterate over the collection, which calls ``getIterator()`` internally, or
explicitly retrieve an iterator though ``getIterator()`` which can then be
used to iterate over the collection. You can not rely on the internal iterator
of the collection being at a certain position unless you explicitly positioned it before.

Methods that do not alter the collection or have template types
appearing in invariant or contravariant positions are not directly
defined in ``Doctrine\Common\Collections\Collection``, but are inherited
from the ``Doctrine\Common\Collections\ReadableCollection`` interface.

The methods available on the interface are:

add
---

Adds an element at the end of the collection.

.. code-block:: php
    $collection->add('test');

clear
-----

Clears the collection, removing all elements.

.. code-block:: php
    $collection->clear();

contains
--------

Checks whether an element is contained in the collection. This is an O(n) operation, where n is the size of the collection.

.. code-block:: php
    $collection = new Collection(['test']);

    $contains = $collection->contains('test'); // true

containsKey
-----------

Checks whether the collection contains an element with the specified key/index.

.. code-block:: php
    $collection = new Collection(['test' => true]);

    $contains = $collection->containsKey('test'); // true

current
-------

Gets the element of the collection at the current iterator position.

.. code-block:: php
    $collection = new Collection(['first', 'second', 'third']);

    $current = $collection->current(); // first

get
---

Gets the element at the specified key/index.

.. code-block:: php
    $collection = new Collection([
        'key' => 'value',
    ]);

    $value = $collection->get('key'); // value

getKeys
-------

Gets all keys/indices of the collection.

.. code-block:: php
    $collection = new Collection(['a', 'b', 'c']);

    $keys = $collection->getKeys(); // [0, 1, 2]

getValues
---------

Gets all values of the collection.

.. code-block:: php
    $collection = new Collection([
        'key1' => 'value1',
        'key2' => 'value2',
        'key3' => 'value3',
    ]);

    $values = $collection->getValues(); // ['value1', 'value2', 'value3']

isEmpty
-------

Checks whether the collection is empty (contains no elements).

.. code-block:: php
    $collection = new Collection(['a', 'b', 'c']);

    $isEmpty = $collection->isEmpty(); // false

first
-----

Sets the internal iterator to the first element in the collection and returns this element.

.. code-block:: php
    $collection = new Collection(['first', 'second', 'third']);

    $first = $collection->first(); // first

exists
------

Tests for the existence of an element that satisfies the given predicate.

.. code-block:: php
    $collection = new Collection(['first', 'second', 'third']);

    $exists = $collection->exists(function($key, $value) {
        return $value === 'first';
    }); // true

filter
------

Returns all the elements of this collection for which your callback function returns `true`.
The order and keys of the elements are preserved.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $filteredCollection = $collection->filter(function($element) {
        return $element > 1;
    }); // [2, 3]

forAll
------

Tests whether the given predicate holds for all elements of this collection.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $forAll = $collection->forAll(function($key, $value) {
        return $value > 1;
    }); // false

indexOf
-------

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.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $indexOf = $collection->indexOf(3); // 2

key
---

Gets the key/index of the element at the current iterator position.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $collection->next();

    $key = $collection->key(); // 1

last
----

Sets the internal iterator to the last element in the collection and returns this element.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $last = $collection->last(); // 3

map
---

Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $mappedCollection = $collection->map(function($value) {
        return $value + 1;
    }); // [2, 3, 4]

next
----

Moves the internal iterator position to the next element and returns this element.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $next = $collection->next(); // 2

partition
---------

Partitions this collection in two collections according to a predicate. Keys are preserved in the resulting collections.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $mappedCollection = $collection->partition(function($key, $value) {
        return $value > 1
    }); // [[2, 3], [1]]

remove
------

Removes the element at the specified index from the collection.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $collection->remove(0); // [2, 3]

removeElement
-------------

Removes the specified element from the collection, if it is found.

.. code-block:: php
    $collection = new ArrayCollection([1, 2, 3]);

    $collection->removeElement(3); // [1, 2]

set
---

Sets an element in the collection at the specified key/index.

.. code-block:: php
    $collection = new ArrayCollection();

    $collection->set('name', 'jwage');

slice
-----

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.

.. code-block:: php
    $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);

    $slice = $collection->slice(1, 2); // [1, 2]

toArray
-------

Gets a native PHP array representation of the collection.

.. code-block:: php
    $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);

    $array = $collection->toArray(); // [0, 1, 2, 3, 4, 5]

Selectable Methods
==================

Some Doctrine Collections, like ``Doctrine\Common\Collections\ArrayCollection``,
implement an interface named ``Doctrine\Common\Collections\Selectable``
that offers the usage of a powerful expressions API, where conditions
can be applied to a collection to get a result with matching elements
only.

matching
--------

Selects all elements from a selectable that match the expression and
returns a new collection containing these elements.

.. code-block:: php
    use Doctrine\Common\Collections\Criteria;
    use Doctrine\Common\Collections\Expr\Comparison;

    $collection = new ArrayCollection([
        [
            'name' => 'jwage',
        ],
        [
            'name' => 'romanb',
        ],
    ]);

    $expr = new Comparison('name', '=', 'jwage');

    $criteria = new Criteria();

    $criteria->where($expr);

    $matched = $collection->matching($criteria); // ['jwage']

You can read more about expressions :ref:`here <expressions>`.