kaivladimirv / laravel-specification-pattern
Implementation of the specification pattern
Installs: 1 016
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- orchestra/testbench: ^8.10
- phpunit/phpunit: ^10.3
- squizlabs/php_codesniffer: ^3.7
README
Specification pattern for Laravel
Implementation of the Specification pattern in PHP
Requirements
- PHP 8.1+
Installation
You can install the package via composer:
$ composer require kaivladimirv/laravel-specification-pattern
Usage
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.
Using the isSatisfiedBy method:
$greaterThan100Spec = new GreaterThanSpecification(100); $greaterThan100Spec->isSatisfiedBy(200); // true $greaterThan100Spec->isSatisfiedBy(99); // false
Using the throwExceptionIfIsNotSatisfiedBy method:
$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; + } }
License
The Task manager project is licensed for use under the MIT License (MIT). Please see LICENSE for more information.