Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 1 month 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 (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-01T04:27:38.000Z (11 months ago)
- Last Synced: 2024-09-30T13:42:45.900Z (about 2 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
[![code style](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/code-style-check.yml/badge.svg)](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/code-style-check.yml)
[![type coverage](https://shepherd.dev/github/kaivladimirv/laravel-specification-pattern/coverage.svg)](https://shepherd.dev/github/kaivladimirv/laravel-specification-pattern)
[![psalm level](https://shepherd.dev/github/kaivladimirv/laravel-specification-pattern/level.svg)](https://psalm.dev/)
[![tests](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/tests-check.yml/badge.svg)](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/tests-check.yml)
[![sast](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/semgrep.yml/badge.svg)](https://github.com/kaivladimirv/laravel-specification-pattern/actions/workflows/semgrep.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/9b3cf2b0022c1d836063/maintainability)](https://codeclimate.com/github/kaivladimirv/laravel-specification-pattern/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/9b3cf2b0022c1d836063/test_coverage)](https://codeclimate.com/github/kaivladimirv/laravel-specification-pattern/test_coverage)
[![Latest Version](https://img.shields.io/packagist/v/kaivladimirv/laravel-specification-pattern.svg?style=flat-square)](https://packagist.org/packages/kaivladimirv/laravel-specification-pattern)
[![License](https://img.shields.io/packagist/l/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.