Skip to content

Commit

Permalink
Renamed {n} to {*} in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 1, 2015
1 parent faec33e commit d5b0660
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Collection/CollectionInterface.php
Expand Up @@ -220,7 +220,7 @@ public function reduce(callable $c, $zero = null);
* ['comment' => ['votes' => [['value' => 1], ['value' => 2], ['value' => 3]]],
* ['comment' => ['votes' => [['value' => 4]]
* ];
* $extracted = (new Collection($items))->extract('comment.votes.{n}.value');
* $extracted = (new Collection($items))->extract('comment.votes.{*}.value');
*
* // Result will contain
* [1, 2, 3, 4]
Expand Down
5 changes: 2 additions & 3 deletions src/Collection/CollectionTrait.php
Expand Up @@ -162,11 +162,10 @@ public function reduce(callable $c, $zero = null)
public function extract($matcher)
{
$extractor = new ExtractIterator($this->unwrap(), $matcher);

if (is_string($matcher) && strpos($matcher, '{n}') !== false) {
if (is_string($matcher) && strpos($matcher, '{*}') !== false) {
$extractor = $extractor
->filter(function ($data) {
return $data instanceof \Traversable || is_array($data);
return $data !== null && ($data instanceof \Traversable || is_array($data));
})
->unfold();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Collection/ExtractTrait.php
Expand Up @@ -38,7 +38,7 @@ protected function _propertyExtractor($callback)

$path = explode('.', $callback);

if (strpos($callback, '{n}') !== false) {
if (strpos($callback, '{*}') !== false) {
return function ($element) use ($path) {
return $this->_extract($element, $path);
};
Expand All @@ -52,7 +52,7 @@ protected function _propertyExtractor($callback)
/**
* Returns a column from $data that can be extracted
* by iterating over the column names contained in $path.
* It will return arrays for elements in represented with `{n}`
* It will return arrays for elements in represented with `{*}`
*
* @param array|\ArrayAccess $data Data.
* @param array $path Path to extract from.
Expand All @@ -64,7 +64,7 @@ protected function _extract($data, $path)
$collectionTransform = false;

foreach ($path as $i => $column) {
if ($column === '{n}') {
if ($column === '{*}') {
$collectionTransform = true;
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -1383,7 +1383,7 @@ public function testSumOfWithIdentity()
}

/**
* Tests using extract with the {n} notation
* Tests using extract with the {*} notation
*
* @return void
*/
Expand All @@ -1395,7 +1395,7 @@ public function testUnfoldedExtract()
['comments' => [['id' => 7], ['nope' => 8]]],
];

$extracted = (new Collection($items))->extract('comments.{n}.id');
$extracted = (new Collection($items))->extract('comments.{*}.id');
$this->assertEquals([1, 2, 3, 4, 7, null], $extracted->toList());

$items = [
Expand Down Expand Up @@ -1429,7 +1429,7 @@ public function testUnfoldedExtract()
],
['not_comments' => []]
];
$extracted = (new Collection($items))->extract('comments.{n}.voters.{n}.id');
$extracted = (new Collection($items))->extract('comments.{*}.voters.{*}.id');
$expected = [1, 2, 3, 4, 5, null, 6];
$this->assertEquals($expected, $extracted->toList());
}
Expand Down

0 comments on commit d5b0660

Please sign in to comment.