PHP code example of kaivladimirv / laravel-specification-pattern
1. Go to this page and download the library: Download kaivladimirv/laravel-specification-pattern library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kaivladimirv / laravel-specification-pattern example snippets
bash
$ php artisan make:specification GreaterThanSpecification
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;
}
}
diff
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;
}
}
php
$greaterThan100Spec = new GreaterThanSpecification(100);
$greaterThan100Spec->isSatisfiedBy(200); // true
$greaterThan100Spec->isSatisfiedBy(99); // false
diff
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;
+ }
}