Skip to content

kaivladimirv/laravel-specification-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

code style type coverage psalm level tests sast Maintainability Test Coverage Latest Version License PHP Programming Language

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.