Skip to content

Commit

Permalink
Implemented Collection::toList() to avoid confusion when getting results
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 4, 2015
1 parent 7f5d029 commit 1662f2e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Collection/CollectionInterface.php
Expand Up @@ -625,6 +625,7 @@ public function nest($idPath, $parentPath);
* @return \Cake\Collection\CollectionInterface
*/
public function insert($path, $values);

/**
* Returns an array representation of the results
*
Expand All @@ -636,6 +637,14 @@ public function insert($path, $values);
*/
public function toArray($preserveKeys = true);

/**
* Returns an numerically-indexed array representation of the results.
* This is equivalent to calling `toArray(false)`
*
* @return array
*/
public function toList();

/**
* Convert a result set into JSON.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Collection/CollectionTrait.php
Expand Up @@ -414,6 +414,15 @@ public function toArray($preserveKeys = true)
return iterator_to_array($this, $preserveKeys);
}

/**
* {@inheritDoc}
*
*/
public function toList()
{
return iterator_to_array($this, false);
}

/**
* {@inheritDoc}
*
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -528,6 +528,18 @@ public function testToArray()
$this->assertEquals($data, $collection->toArray());
}

/**
* Test toList method
*
* @return void
*/
public function testToList()
{
$data = [100 => 1, 300 => 2, 500 => 3, 1 => 4];
$collection = new Collection($data);
$this->assertEquals(array_values($data), $collection->toList());
}

/**
* Test json enconding
*
Expand Down

0 comments on commit 1662f2e

Please sign in to comment.