-
-
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.
Add the Until operation with tests and example in README.
- Loading branch information
Showing
6 changed files
with
116 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ interface Collection extends | |
Sliceable, | ||
Sortable, | ||
Splitable, | ||
Untilable, | ||
Walkable, | ||
Zipable | ||
{ | ||
|
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Contract; | ||
|
||
/** | ||
* Interface Untilable. | ||
*/ | ||
interface Untilable | ||
{ | ||
/** | ||
* @param callable $callable | ||
* | ||
* @return \drupol\collection\Contract\Collection | ||
*/ | ||
public function until(callable $callable): Base; | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use drupol\collection\Contract\Operation; | ||
|
||
/** | ||
* Class Until. | ||
*/ | ||
final class Until implements Operation | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $callable; | ||
|
||
/** | ||
* Until constructor. | ||
* | ||
* @param callable $until | ||
*/ | ||
public function __construct(callable $until) | ||
{ | ||
$this->callable = $until; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection): \Closure | ||
{ | ||
$until = $this->callable; | ||
|
||
return static function () use ($until, $collection): \Generator { | ||
foreach ($collection as $key => $value) { | ||
yield $key => $value; | ||
|
||
if (true === $until($value, $key)) { | ||
break; | ||
} | ||
} | ||
}; | ||
} | ||
} |