Skip to content

Commit

Permalink
Update Pluck operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 9, 2019
1 parent 2351988 commit 9e96a94
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,16 +702,19 @@ public function it_can_pluck(): void

$input = [
[
0 => 'A',
'foo' => [
'bar' => 0,
],
],
[
0 => 'B',
'foo' => [
'bar' => 1,
],
],
[
0 => 'C',
'foo' => [
'bar' => 2,
],
Expand All @@ -734,6 +737,7 @@ public function it_can_pluck(): void
];
},
[
0 => 'D',
'foo' => [
'bar' => $six,
],
Expand All @@ -759,6 +763,10 @@ public function it_can_pluck(): void
$this::with($input)
->pluck('azerty', 'taz')
->shouldIterateAs([0 => 'taz', 1 => 'taz', 2 => 'taz', 3 => 'taz', 4 => 'taz', 5 => 'taz', 6 => 'taz']);

$this::with($input)
->pluck(0)
->shouldIterateAs([0 => 'A', 1 => 'B', 2 => 'C', null, null, null, 6 => 'D']);
}

public function it_can_prepend(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Pluck.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Pluck implements Operation
private $default;

/**
* @var array|string
* @var mixed
*/
private $key;

Expand All @@ -47,7 +47,7 @@ public function on(iterable $collection): \Closure
$operation = $this;

return static function () use ($key, $default, $collection, $operation) {
$key = \is_string($key) ? \explode('.', \trim($key, '.')) : $key;
$key = true === \is_scalar($key) ? \explode('.', \trim((string) $key, '.')) : $key;

foreach ($collection as $value) {
yield $operation->pick($collection, $value, $key, $default);
Expand Down

0 comments on commit 9e96a94

Please sign in to comment.