Skip to content

Instantly share code, notes, and snippets.

@speto
Created November 21, 2018 18:12
Show Gist options
  • Save speto/ceb1dfe25128c2c5b0184fb0911d0e20 to your computer and use it in GitHub Desktop.
Save speto/ceb1dfe25128c2c5b0184fb0911d0e20 to your computer and use it in GitHub Desktop.
evaluation of callable conditions POC
<?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