-
-
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
6 changed files
with
142 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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
interface IfThenElseable | ||
{ | ||
/** | ||
* @psalm-return \loophp\collection\Contract\Collection<TKey, T> | ||
*/ | ||
public function ifThenElse(callable $condition, callable $then, ?callable $else = null): Collection; | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use Iterator; | ||
use loophp\collection\Contract\Operation; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
final class IfThenElse extends AbstractOperation implements Operation | ||
{ | ||
/** | ||
* @psalm-param callable(T, TKey): bool $condition | ||
* @psalm-param callable(T, TKey): (T|TKey) $then | ||
* @psalm-param callable(T, TKey): (T|TKey) $else | ||
*/ | ||
public function __construct(callable $condition, callable $then, callable $else) | ||
{ | ||
$this->storage = [ | ||
'condition' => $condition, | ||
'then' => $then, | ||
'else' => $else, | ||
]; | ||
} | ||
|
||
// phpcs:disable | ||
/** | ||
* @psalm-return Closure(Iterator<TKey, T>, callable(T, TKey): bool, callable(T, TKey): (T|TKey), callable(T, TKey): (T|TKey)): Generator<TKey, T> | ||
*/ | ||
// phpcs:enable | ||
public function __invoke(): Closure | ||
{ | ||
return static function (Iterator $iterator, callable $condition, callable $then, callable $else): Generator { | ||
foreach ($iterator as $key => $value) { | ||
yield $key => $condition($value, $key) ? | ||
$then($value, $key) : | ||
$else($value, $key); | ||
} | ||
}; | ||
} | ||
} |