Skip to content

Commit 8e2ab7f

Browse files
jwagegreg0ire
authored andcommitted
Change Collection::slice() method to return an instance of Collection
instead of an array.
1 parent cec2511 commit 8e2ab7f

File tree

6 files changed

+17
-9
lines changed

6 files changed

+17
-9
lines changed

UPGRADE.md

+4
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,7 @@ You can find a list of major changes to public API below.
8484
| before | after |
8585
|-----------------------------:|:-----------------------------------------|
8686
| matching(Criteria $criteria) | matching(Criteria $criteria): Collection |
87+
88+
#### Doctrine\Common\Collections\Collection::slice()
89+
90+
The `Collection::slice()` method returns a new `Collection` instance now instead of an array.

docs/en/index.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,16 @@ Sets an element in the collection at the specified key/index.
296296
slice
297297
-----
298298

299-
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.
299+
Extracts a slice of ``$length`` elements starting at position $offset from
300+
the Collection. If ``$length`` is null it returns all elements from $offset
301+
to the end of the Collection. Keys have to be preserved by this method.
302+
Calling this method will only return the selected slice and NOT change
303+
the elements contained in the collection slice is called on.
300304

301305
.. code-block:: php
302306
$collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
303307
304-
$slice = $collection->slice(1, 2); // [1, 2]
308+
$slice = $collection->slice(1, 2); // new ArrayCollection([1, 2])
305309
306310
toArray
307311
-------

lib/Doctrine/Common/Collections/AbstractLazyCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function indexOf($element): string|int|false
254254
/**
255255
* {@inheritDoc}
256256
*/
257-
public function slice(int $offset, ?int $length = null): array
257+
public function slice(int $offset, ?int $length = null): Collection
258258
{
259259
$this->initialize();
260260

lib/Doctrine/Common/Collections/ArrayCollection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ public function clear(): void
381381
/**
382382
* {@inheritDoc}
383383
*/
384-
public function slice(int $offset, ?int $length = null): array
384+
public function slice(int $offset, ?int $length = null) : Collection
385385
{
386-
return array_slice($this->elements, $offset, $length, true);
386+
return $this->createFrom(array_slice($this->elements, $offset, $length, true));
387387
}
388388

389389
/**

lib/Doctrine/Common/Collections/Collection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,5 @@ public function indexOf(mixed $element): int|string|false;
290290
* @return mixed[]
291291
* @psalm-return array<TKey,T>
292292
*/
293-
public function slice(int $offset, ?int $length = null): array;
293+
public function slice(int $offset, ?int $length = null): self;
294294
}

tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ public function testSlice(): void
219219
$this->collection[] = 'two';
220220
$this->collection[] = 'three';
221221

222-
$slice = $this->collection->slice(0, 1);
222+
$slice = $this->collection->slice(0, 1)->toArray();
223223
self::assertIsArray($slice);
224224
self::assertEquals(['one'], $slice);
225225

226-
$slice = $this->collection->slice(1);
226+
$slice = $this->collection->slice(1)->toArray();
227227
self::assertEquals([1 => 'two', 2 => 'three'], $slice);
228228

229-
$slice = $this->collection->slice(1, 1);
229+
$slice = $this->collection->slice(1, 1)->toArray();
230230
self::assertEquals([1 => 'two'], $slice);
231231
}
232232

0 commit comments

Comments
 (0)