add constant value predicate
This commit is contained in:
parent
40f8bfeab8
commit
4820e9fdb4
@ -10,8 +10,8 @@
|
|||||||
namespace Kadet\Functional\Predicate;
|
namespace Kadet\Functional\Predicate;
|
||||||
|
|
||||||
|
|
||||||
|
use function Kadet\Functional\{ all, any };
|
||||||
use Kadet\Functional\Predicate;
|
use Kadet\Functional\Predicate;
|
||||||
use function Kadet\Functional\predicate;
|
|
||||||
|
|
||||||
abstract class AbstractPredicate implements Predicate
|
abstract class AbstractPredicate implements Predicate
|
||||||
{
|
{
|
||||||
@ -23,12 +23,12 @@ abstract class AbstractPredicate implements Predicate
|
|||||||
public function and(...$predicate): Predicate
|
public function and(...$predicate): Predicate
|
||||||
{
|
{
|
||||||
array_unshift($predicate, $this);
|
array_unshift($predicate, $this);
|
||||||
return new AllOfPredicate(...array_map(predicate::class, $predicate));
|
return all($predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function or(...$predicate): Predicate
|
public function or(...$predicate): Predicate
|
||||||
{
|
{
|
||||||
array_unshift($predicate, $this);
|
array_unshift($predicate, $this);
|
||||||
return new AnyOfPredicate(...array_map(predicate::class, $predicate));
|
return any($predicate);
|
||||||
}
|
}
|
||||||
}
|
}
|
58
src/Predicate/ConstantPredicate.php
Normal file
58
src/Predicate/ConstantPredicate.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright 2018 Kacper Donat
|
||||||
|
*
|
||||||
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
||||||
|
*
|
||||||
|
* Full license available in separate LICENSE file
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Kadet\Functional\Predicate;
|
||||||
|
|
||||||
|
|
||||||
|
use function Kadet\Functional\{ all, any };
|
||||||
|
use Kadet\Functional\Predicate;
|
||||||
|
|
||||||
|
class ConstantPredicate extends AbstractPredicate
|
||||||
|
{
|
||||||
|
/** @var boolean */
|
||||||
|
private $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ConstantPredicate constructor.
|
||||||
|
*
|
||||||
|
* @param bool $value
|
||||||
|
*/
|
||||||
|
public function __construct(bool $value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function not(): Predicate
|
||||||
|
{
|
||||||
|
return new self(!$this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function and (...$predicate): Predicate
|
||||||
|
{
|
||||||
|
if (!$this->value) {
|
||||||
|
return new self(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return all($predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function or (...$predicate): Predicate
|
||||||
|
{
|
||||||
|
if ($this->value) {
|
||||||
|
return new self(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return any($predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(...$args): bool
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
namespace Kadet\Functional;
|
namespace Kadet\Functional;
|
||||||
|
|
||||||
|
use Kadet\Functional\Predicate\AllOfPredicate;
|
||||||
|
use Kadet\Functional\Predicate\AnyOfPredicate;
|
||||||
use Kadet\Functional\Predicate\ClosurePredicate;
|
use Kadet\Functional\Predicate\ClosurePredicate;
|
||||||
|
use Kadet\Functional\Predicate\ConstantPredicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $predicate
|
* @param $predicate
|
||||||
@ -24,3 +27,37 @@ function predicate($predicate): Predicate
|
|||||||
throw new \Exception();
|
throw new \Exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function not($predicate)
|
||||||
|
{
|
||||||
|
return predicate($predicate)->not();
|
||||||
|
}
|
||||||
|
|
||||||
|
function always()
|
||||||
|
{
|
||||||
|
return new ConstantPredicate(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function never()
|
||||||
|
{
|
||||||
|
return new ConstantPredicate(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function all(...$predicates)
|
||||||
|
{
|
||||||
|
// extract array
|
||||||
|
if (count($predicates) === 1 && is_array($predicates[0])) {
|
||||||
|
$predicates = $predicates[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AllOfPredicate(...array_map(predicate::class, $predicates));
|
||||||
|
}
|
||||||
|
|
||||||
|
function any(...$predicates)
|
||||||
|
{
|
||||||
|
// extract array
|
||||||
|
if (count($predicates) === 1 && is_array($predicates[0])) {
|
||||||
|
$predicates = $predicates[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AnyOfPredicate(...array_map(predicate::class, $predicates));
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user