generated from spawnia/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class
MLL\Utils\Specification
for logical combinations of predi…
…cates
Showing
3 changed files
with
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils; | ||
|
||
/** | ||
* Allows the logical combination of specification callables. | ||
* | ||
* We define specifications through a functional interface in the form `(mixed): bool`. | ||
* This allows the usage of ad-hoc closures, first-class callables, and invokable classes. | ||
* | ||
* https://en.wikipedia.org/wiki/Specification_pattern | ||
*/ | ||
class Specification | ||
{ | ||
/** | ||
* @template TCandidate | ||
* | ||
* @param callable(TCandidate): bool $specification | ||
* | ||
* @return callable(TCandidate): bool | ||
*/ | ||
public static function not(callable $specification): callable | ||
{ | ||
return fn ($value): bool => ! $specification($value); | ||
} | ||
|
||
/** | ||
* @template TCandidate | ||
* | ||
* @param callable(TCandidate): bool ...$specifications | ||
* | ||
* @return callable(TCandidate): bool | ||
*/ | ||
public static function or(callable ...$specifications): callable | ||
{ | ||
return function ($value) use ($specifications): bool { | ||
foreach ($specifications as $specification) { | ||
if ($specification($value)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
} | ||
|
||
/** | ||
* @template TCandidate | ||
* | ||
* @param callable(TCandidate): bool ...$specifications | ||
* | ||
* @return callable(TCandidate): bool | ||
*/ | ||
public static function and(callable ...$specifications): callable | ||
{ | ||
return function ($value) use ($specifications): bool { | ||
foreach ($specifications as $specification) { | ||
if (! $specification($value)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
} | ||
} |
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,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\Tests; | ||
|
||
use MLL\Utils\Specification; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SpecificationTest extends TestCase | ||
{ | ||
public function testNot(): void | ||
{ | ||
$identity = fn ($value) => $value; | ||
|
||
self::assertTrue($identity(true)); | ||
|
||
$negatedIdentity = Specification::not($identity); | ||
self::assertFalse($negatedIdentity(true)); | ||
} | ||
|
||
public function testOr(): void | ||
{ | ||
$is1 = fn ($value): bool => $value === 1; | ||
$is2 = fn ($value): bool => $value === 2; | ||
|
||
$is1Or2 = Specification::or($is1, $is2); | ||
self::assertTrue($is1Or2(1)); | ||
self::assertTrue($is1Or2(2)); | ||
self::assertFalse($is1Or2(3)); | ||
} | ||
|
||
public function testAnd(): void | ||
{ | ||
$isPositive = fn ($value): bool => $value > 0; | ||
$isOdd = fn ($value): bool => $value % 2 === 1; | ||
|
||
$isPositiveAndOdd = Specification::and($isPositive, $isOdd); | ||
self::assertTrue($isPositiveAndOdd(1)); | ||
self::assertFalse($isPositiveAndOdd(2)); | ||
self::assertFalse($isPositiveAndOdd(-1)); | ||
self::assertFalse($isPositiveAndOdd(0)); | ||
self::assertFalse($isPositiveAndOdd(-2)); | ||
} | ||
} |