https://github.com/kaivladimirv/laravel-specification-pattern
Implementation of the Specification pattern in PHP
https://github.com/kaivladimirv/laravel-specification-pattern
laravel-package php8
Last synced: 4 months ago
JSON representation
Implementation of the Specification pattern in PHP
- Host: GitHub
- URL: https://github.com/kaivladimirv/laravel-specification-pattern
- Owner: kaivladimirv
- License: mit
- Created: 2023-08-29T02:32:30.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-01T04:27:38.000Z (over 1 year ago)
- Last Synced: 2025-01-22T10:07:30.184Z (6 months ago)
- Topics: laravel-package, php8
- Language: PHP
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/code-style-check.yml)
[](https://shepherd.dev/github/kaivladimirv/laravel-specification-pattern)
[](https://psalm.dev/)
[](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/tests-check.yml)
[](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/semgrep.yml)
[](https://codeclimate.com/github/kaivladimirv/laravel-specification-pattern/maintainability)
[](https://codeclimate.com/github/kaivladimirv/laravel-specification-pattern/test_coverage)
[](https://packagist.org/packages/kaivladimirv/laravel-specification-pattern)
[](https://github.com/kaivladimirv/laravel-specification-pattern/blob/main/LICENSE)## Specification pattern for Laravel
Implementation of the Specification pattern in PHP## Requirements
* PHP 8.1+## Installation
You can install the package via composer:``` bash
$ composer require kaivladimirv/laravel-specification-pattern
```## Usage
You can create a specification using the artisan command:``` bash
$ php artisan make:specification GreaterThanSpecification
```This command will create a specification class in the App\Specifications namespace.
``` php
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:
``` php
$greaterThan100Spec = new GreaterThanSpecification(100);$greaterThan100Spec->isSatisfiedBy(200); // true
$greaterThan100Spec->isSatisfiedBy(99); // false
```### Using the throwExceptionIfIsNotSatisfiedBy method:
``` php
$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:
``` diff
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](/LICENSE) for more information.