maartenpaauw / laravel-specification-pattern
This is my package laravel-specification-pattern
Fund package maintenance!
maartenpaauw
Installs: 4 701
Dependents: 1
Suggesters: 0
Security: 0
Stars: 13
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.16.2
- symfony/polyfill-php83: ^1.30
- webmozart/assert: ^1.11
Requires (Dev)
- illuminate/testing: ^10.0|^11.0
- larastan/larastan: ^2.8
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8|^8.1
- orchestra/testbench: ^8.8|^9.0
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.5
- pestphp/pest-plugin-laravel: ^2.2
- pestphp/pest-plugin-type-coverage: ^2.8
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.35
- spatie/phpunit-snapshot-assertions: ^5.1
README
Filter an Illuminate collection with specifications.
Support me
You can support me by buying Pennant feature flags for Filament.
Installation
You can install the package via composer:
composer require maartenpaauw/laravel-specification-pattern
You can publish the config file with:
php artisan vendor:publish --tag="laravel-specification-pattern-config"
This is the contents of the published config file:
return [ 'collection-method' => 'matching', ];
Usage
Here's how you can create a specification:
php artisan make:specification AdultSpecification
This will generate a specification class within the App\Specifications
namespace.
<?php namespace App\Specifications; use Maartenpaauw\Specifications\Specification; /** * @implements Specification<mixed> */ class AdultSpecification implements Specification { /** * {@inheritDoc} */ public function isSatisfiedBy(mixed $candidate): bool { return true; } }
Imagine we have the following class which represents a person with a given age.
class Person { public function __construct(public int $age) {} }
Let's apply the business logic:
<?php namespace App\Specifications; use Maartenpaauw\Specifications\Specification; /** - * @implements Specification<mixed> + * @implements Specification<Person> */ class AdultSpecification implements Specification { /** * {@inheritDoc} */ public function isSatisfiedBy(mixed $candidate): bool { - return true; + return $candidate->age >= 18; } }
After applying the bussiness logic we can use the specification by directly calling the isSatisfiedBy
method or indirectly be filtering an eloquent collection by calling the matching
method.
Direct
$specification = new AdultSpecification(); // ... $specification->isSatisfiedBy(new Person(16)); // false $specification->isSatisfiedBy(new Person(32)); // true
Indirect
$persons = collect([ new Person(10), new Person(17), new Person(18), new Person(32), ]); // ... // Returns a collection with persons matching the specification $persons = $persons->matching(new AdultSpecification()); // ... $persons->count(); // 2
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.