From 03d9bd610664e6eb784d0e1c37fdb529bbace9bb Mon Sep 17 00:00:00 2001 From: Marcelo Garbarino Date: Fri, 15 Aug 2014 00:51:45 -0300 Subject: [PATCH] Added method values() --- composer.json | 2 +- src/Collection.php | 14 ++++++++++++++ tests/CollectionTest.php | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index de8acee..aeba681 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "php": "~5.4" }, "require-dev": { - "phpunit/phpunit": "4.1.*", + "phpunit/phpunit": "4.*", "satooshi/php-coveralls": "dev-master" }, "autoload": { diff --git a/src/Collection.php b/src/Collection.php index 00db5a9..aee66bd 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -306,6 +306,20 @@ public function tail() return new Collection(array_slice($this->arr, 1)); } + /** + * Returns a collection with all the elements in this collection, + * but with regenerated numeric indexes. + *

+ * This uses the array_values() PHP built-in function. + *

+ * + * @return Collection + */ + public function values() + { + return new Collection(array_values($this->arr)); + } + /** * Creates the object. * diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 7f43b7a..2d47d32 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -187,6 +187,22 @@ public function testTail() $this->assertEquals($col2, Collection::fromArray(['x'])->tail()); } + public function testValues() + { + $col1 = $this->object->filter(function ($elem) { return $elem > 2; }); + $this->assertEquals(3, $col1[2]); + $this->assertEquals(4, $col1[3]); + $this->assertEquals(5, $col1[4]); + $this->assertEquals(6, $col1[5]); + $col2 = $col1->values(); + $this->assertEquals(3, $col2[0]); + $this->assertEquals(4, $col2[1]); + $this->assertEquals(5, $col2[2]); + $this->assertEquals(6, $col2[3]); + $col3 = Collection::fromArray([])->values(); + $this->assertCount(0, $col3); + } + public function testOffsetExists() { $this->assertTrue(isset($this->object[3]));