From 93885988e1baf2b3af4e65e662f19b49519edab8 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 16 Jan 2024 11:15:29 +0100 Subject: [PATCH] Remove Collection::pushEach() --- src/Collection.php | 13 ------------- tests/CollectionTest.php | 21 ++++++++++----------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index c82fe7f0..d438257e 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -177,19 +177,6 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable return new self($items); } - public function pushEach(array $data, ?callable $callback = null): CollectionInterface - { - if (! is_iterable($data)) { - throw new RuntimeException('Unable to iterate given data.'); - } - - foreach ($data as $item) { - $this->push(is_callable($callback) ? $callback($item) : $item); - } - - return $this; - } - /** * {@inheritdoc} * diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 3b3aef9f..7df66225 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -76,17 +76,6 @@ class CollectionTest extends TestCase $this->assertInstanceOf(Collection::class, $result); } - public function testPushEach() - { - $collection = Collection::create()->pushEach(['foo', 'bar', 'baz'], function ($item) { - return strtoupper($item); - }); - $this->assertEquals(3, $collection->count()); - $this->assertEquals('FOO', $collection->get(0)); - $this->assertEquals('BAR', $collection->get(1)); - $this->assertEquals('BAZ', $collection->get(2)); - } - public function testToArray() { $collection = new Collection(['foo', 'bar', 'baz']); @@ -137,6 +126,16 @@ class CollectionTest extends TestCase $this->assertEquals(['example' => 'value'], $collection->get('baz.test3')); } + public function testGetAtPosition(): void + { + $collection = new Collection([1, 2, 'foo' => 'bar']); + $this->assertEquals(1, $collection->getAtPosition(0)); + $this->assertEquals(2, $collection->getAtPosition(1)); + $this->assertEquals('bar', $collection->getAtPosition(2)); + $this->assertNull($collection->getAtPosition(3)); + $this->assertEquals('default', $collection->getAtPosition(3, 'default')); + } + public function testEmpty(): void { $collection = new Collection([1, 2, 3]);