-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: specify next release add OrPredicate and AndPredicate add Set::match()
- Loading branch information
Showing
9 changed files
with
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
use Innmind\Immutable\Predicate\Instance; | ||
|
||
return static function() { | ||
yield test( | ||
'Or predicate', | ||
static function($assert) { | ||
$array = new \SplFixedArray; | ||
|
||
$assert->true( | ||
Instance::of(\Countable::class) | ||
->or(Instance::of(\stdClass::class)) | ||
($array), | ||
); | ||
$assert->true( | ||
Instance::of(\stdClass::class) | ||
->or(Instance::of(\Countable::class)) | ||
($array), | ||
); | ||
$assert->false( | ||
Instance::of(\Throwable::class) | ||
->or(Instance::of(\Unknown::class)) | ||
($array), | ||
); | ||
}, | ||
); | ||
|
||
yield test( | ||
'And predicate', | ||
static function($assert) { | ||
$array = new \SplFixedArray; | ||
|
||
$assert->true( | ||
Instance::of(\Countable::class) | ||
->and(Instance::of(\Traversable::class)) | ||
($array), | ||
); | ||
$assert->false( | ||
Instance::of(\Throwable::class) | ||
->and(Instance::of(\Countable::class)) | ||
($array), | ||
); | ||
$assert->false( | ||
Instance::of(\Countable::class) | ||
->and(Instance::of(\Throwable::class)) | ||
($array), | ||
); | ||
}, | ||
); | ||
}; |
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,37 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
use Innmind\Immutable\{ | ||
Set, | ||
Sequence, | ||
}; | ||
use Innmind\BlackBox\Set as DataSet; | ||
|
||
return static function() { | ||
yield proof( | ||
'Set::match()', | ||
given( | ||
DataSet\Type::any(), | ||
DataSet\Sequence::of(DataSet\Type::any()) | ||
->map(static fn($values) => Sequence::of(...$values)->distinct()->toList()), | ||
)->filter(static fn($first, $rest) => !\in_array($first, $rest, true)), | ||
static function($assert, $first, $rest) { | ||
$assert->same( | ||
$first, | ||
Set::of()->match( | ||
static fn() => false, | ||
static fn() => $first, | ||
), | ||
); | ||
|
||
$packed = Set::of($first, ...$rest)->match( | ||
static fn($first, $rest) => [$first, $rest], | ||
static fn() => null, | ||
); | ||
|
||
$assert->not()->null($packed); | ||
$assert->same($first, $packed[0]); | ||
$assert->same($rest, $packed[1]->toList()); | ||
}, | ||
); | ||
}; |
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,50 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable\Predicate; | ||
|
||
use Innmind\Immutable\Predicate; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @template A | ||
* @template B | ||
* @implements Predicate<A&B> | ||
*/ | ||
final class AndPredicate implements Predicate | ||
{ | ||
/** @var Predicate<A> */ | ||
private Predicate $a; | ||
/** @var Predicate<B> */ | ||
private Predicate $b; | ||
|
||
/** | ||
* @param Predicate<A> $a | ||
* @param Predicate<B> $b | ||
*/ | ||
private function __construct(Predicate $a, Predicate $b) | ||
{ | ||
$this->a = $a; | ||
$this->b = $b; | ||
} | ||
|
||
public function __invoke(mixed $value): bool | ||
{ | ||
return ($this->a)($value) && ($this->b)($value); | ||
} | ||
|
||
/** | ||
* @psalm-pure | ||
* @template T | ||
* @template V | ||
* | ||
* @param Predicate<T> $a | ||
* @param Predicate<V> $b | ||
* | ||
* @return self<T, V> | ||
*/ | ||
public static function of(Predicate $a, Predicate $b): self | ||
{ | ||
return new self($a, $b); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable\Predicate; | ||
|
||
use Innmind\Immutable\Predicate; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @template A | ||
* @template B | ||
* @implements Predicate<A|B> | ||
*/ | ||
final class OrPredicate implements Predicate | ||
{ | ||
/** @var Predicate<A> */ | ||
private Predicate $a; | ||
/** @var Predicate<B> */ | ||
private Predicate $b; | ||
|
||
/** | ||
* @param Predicate<A> $a | ||
* @param Predicate<B> $b | ||
*/ | ||
private function __construct(Predicate $a, Predicate $b) | ||
{ | ||
$this->a = $a; | ||
$this->b = $b; | ||
} | ||
|
||
public function __invoke(mixed $value): bool | ||
{ | ||
return ($this->a)($value) || ($this->b)($value); | ||
} | ||
|
||
/** | ||
* @psalm-pure | ||
* @template T | ||
* @template V | ||
* | ||
* @param Predicate<T> $a | ||
* @param Predicate<V> $b | ||
* | ||
* @return self<T, V> | ||
*/ | ||
public static function of(Predicate $a, Predicate $b): self | ||
{ | ||
return new self($a, $b); | ||
} | ||
} |
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