-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use ArrayAccess; | ||
use drupol\collection\Collection; | ||
use drupol\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* Class Pluck. | ||
*/ | ||
final class Pluck extends Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(CollectionInterface $collection): CollectionInterface | ||
{ | ||
[$key, $default] = $this->parameters; | ||
$operation = $this; | ||
|
||
return Collection::withClosure( | ||
static function () use ($key, $default, $collection, $operation) { | ||
$key = \is_string($key) ? \explode('.', \trim($key, '.')) : $key; | ||
|
||
foreach ($collection as $item) { | ||
yield $operation->pick($item, $key, $default); | ||
} | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Get an item from an array or object using "dot" notation. | ||
* | ||
* @param mixed $target | ||
* @param array $key | ||
* @param mixed $default | ||
* | ||
* @throws \ReflectionException | ||
* | ||
* @return mixed | ||
*/ | ||
private function pick($target, array $key, $default = null) | ||
{ | ||
while (null !== $segment = \array_shift($key)) { | ||
if ('*' === $segment) { | ||
if (!\is_array($target)) { | ||
return $default; | ||
} | ||
|
||
$result = []; | ||
|
||
foreach ($target as $item) { | ||
$result[] = $this->pick($item, $key); | ||
} | ||
|
||
return \in_array('*', $key, true) ? Collection::withArray($result)->collapse() : $result; | ||
} | ||
|
||
if ((true === \is_array($target)) && (true === \array_key_exists($segment, $target))) { | ||
$target = $target[$segment]; | ||
} elseif (($target instanceof ArrayAccess) && (true === $target->offsetExists($segment))) { | ||
$target = $target[$segment]; | ||
} elseif ($target instanceof CollectionInterface) { | ||
$target = $target->get($segment, $default); | ||
} elseif ((true === \is_object($target)) && (true === \property_exists($target, $segment))) { | ||
$target = (new \ReflectionClass($target))->getProperty($segment)->getValue($target); | ||
} else { | ||
$target = $default; | ||
} | ||
} | ||
|
||
return $target; | ||
} | ||
} |