Skip to content

Commit

Permalink
Added method values()
Browse files Browse the repository at this point in the history
  • Loading branch information
marcegarba committed Aug 15, 2014
1 parent 77a2c82 commit 03d9bd6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "~5.4"
},
"require-dev": {
"phpunit/phpunit": "4.1.*",
"phpunit/phpunit": "4.*",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
Expand Down
14 changes: 14 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* This uses the <code>array_values()</code> PHP built-in function.
* </p>
*
* @return Collection
*/
public function values()
{
return new Collection(array_values($this->arr));
}

/**
* Creates the object.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down

0 comments on commit 03d9bd6

Please sign in to comment.