Created
November 21, 2018 18:12
-
-
Save speto/ceb1dfe25128c2c5b0184fb0911d0e20 to your computer and use it in GitHub Desktop.
evaluation of callable conditions POC
This file contains hidden or 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
<?php | |
class IsTrue | |
{ | |
public function __invoke() | |
{ | |
return true; | |
} | |
} | |
class IsFalse | |
{ | |
public function __invoke() | |
{ | |
return false; | |
} | |
} | |
$conditions = [new IsTrue(), new IsFalse]; | |
$result = array_reduce( | |
$conditions, | |
function ($carry, $item) { | |
$carry = $carry === null ? $item() : $carry && $item(); | |
return $carry; | |
} | |
); | |
var_dump($result); | |
$evaluate = function (callable ...$conditions) { | |
return array_reduce( | |
$conditions, | |
function ($carry, $item) { | |
$carry = $carry === null ? $item() : $carry && $item(); | |
return $carry; | |
} | |
); | |
}; | |
var_dump($evaluate(new IsTrue(), new IsTrue())); | |
var_dump($evaluate(new IsTrue(), new IsFalse())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment