-
-
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
9 changed files
with
192 additions
and
78 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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use drupol\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* Class All. | ||
*/ | ||
final class All extends Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(CollectionInterface $collection) | ||
{ | ||
$result = []; | ||
|
||
foreach ($collection as $key => $item) { | ||
if ($item instanceof CollectionInterface) { | ||
$result[$key] = $item->all(); | ||
} else { | ||
$result[$key] = $item; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use drupol\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* Class Contains. | ||
*/ | ||
final class Contains extends Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(CollectionInterface $collection) | ||
{ | ||
[$key] = $this->parameters; | ||
|
||
if (!\is_string($key) && \is_callable($key)) { | ||
$placeholder = new \stdClass(); | ||
|
||
return $collection->first($key, $placeholder) !== $placeholder; | ||
} | ||
|
||
foreach ($collection as $value) { | ||
if ($value === $key) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use drupol\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* Class First. | ||
*/ | ||
final class First extends Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(CollectionInterface $collection) | ||
{ | ||
[$callback, $default] = $this->parameters; | ||
|
||
/** @var \Iterator $iterator */ | ||
$iterator = $collection->getIterator(); | ||
|
||
if (null === $callback) { | ||
if (!$iterator->valid()) { | ||
return $default; | ||
} | ||
|
||
return $iterator->current(); | ||
} | ||
|
||
foreach ($iterator as $key => $value) { | ||
if ($callback($value, $key)) { | ||
return $value; | ||
} | ||
} | ||
|
||
return $default; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use drupol\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* Class Get. | ||
*/ | ||
final class Get extends Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(CollectionInterface $collection) | ||
{ | ||
[$key, $default] = $this->parameters; | ||
|
||
foreach ($collection as $outerKey => $outerValue) { | ||
if ($outerKey === $key) { | ||
return $outerValue; | ||
} | ||
} | ||
|
||
return $default; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use drupol\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* Class Reduce. | ||
*/ | ||
final class Reduce extends Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(CollectionInterface $collection) | ||
{ | ||
[$callback, $initial] = $this->parameters; | ||
|
||
$result = $initial; | ||
|
||
foreach ($collection as $value) { | ||
$result = $callback($result, $value); | ||
} | ||
|
||
return $result; | ||
} | ||
} |