Implementation of the Specification pattern in PHP
- PHP 8.1+
You can install the package via composer:
$ composer require kaivladimirv/laravel-specification-pattern
You can create a specification using the artisan command:
$ php artisan make:specification GreaterThanSpecification
This command will create a specification class in the App\Specifications namespace.
<?php
declare(strict_types=1);
namespace App\Specifications;
use Kaivladimirv\LaravelSpecificationPattern\AbstractSpecification
class GreaterThanSpecification extends AbstractSpecification
{
protected function defineMessage(): string
{
// TODO: Implement defineMessage() method.
}
protected function executeCheckIsSatisfiedBy(mixed $candidate): bool
{
return false;
}
}
Adding business logic to the created specification:
<?php
declare(strict_types=1);
namespace App\Specifications;
use Kaivladimirv\LaravelSpecificationPattern\AbstractSpecification
class GreaterThanSpecification extends AbstractSpecification
{
+ public function __construct(readonly private int|float $number)
+ {
+ }
+
protected function defineMessage(): string
{
- // TODO: Implement defineMessage() method.
+ return "Number must be greater than $this->number";
}
protected function executeCheckIsSatisfiedBy(mixed $candidate): bool
{
- return false;
+ return $candidate > $this->number;
}
}
After adding business logic, you can use the specification by calling the isSatisfiedBy or throwExceptionIfIsNotSatisfiedBy methods.
$greaterThan100Spec = new GreaterThanSpecification(100);
$greaterThan100Spec->isSatisfiedBy(200); // true
$greaterThan100Spec->isSatisfiedBy(99); // false
$greaterThan100Spec = new GreaterThanSpecification(100);
$greaterThan100Spec->throwExceptionIfIsNotSatisfiedBy(200); // No exception will be thrown here
$greaterThan100Spec->isSatisfiedBy(99); // An DomainException will be thrown here with the message "Number must be greater than 100"
You can change the exception type in the getExceptionClass method:
<?php
declare(strict_types=1);
namespace App\Specifications;
use Kaivladimirv\LaravelSpecificationPattern\AbstractSpecification
class GreaterThanSpecification extends AbstractSpecification
{
public function __construct(readonly private int|float $number)
{
}
protected function defineMessage(): string
{
return "Number must be greater than $this->number";
}
protected function executeCheckIsSatisfiedBy(mixed $candidate): bool
{
return $candidate > $this->number;
}
+
+ protected function getExceptionClass(): string
+ {
+ return MyException::class;
+ }
}
The Task manager project is licensed for use under the MIT License (MIT). Please see LICENSE for more information.