1. Go to this page and download the library: Download vjik/specification 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/ */
vjik / specification example snippets
use Vjik\Specification\BaseSpecification;
/**
* @template T as User
* @template-extends BaseSpecification<T>
*/
final class UserIsAdultSpecification extends BaseSpecification
{
/**
* @param T $value
*/
public function isSatisfiedBy(mixed $value): bool
{
return $value->age >= 18;
}
}
$specification = new UserIsAdultSpecification();
// true or false
$specification->isSatisfiedBy($user);
// throws an exception if user is not adult
$specification->satisfiedBy($user);
use Vjik\Specification\AndSpecification;
use Vjik\Specification\OrSpecification;
// User is adult AND active
$isActiveAdultUserSpecification = new AndSpecification([
new UserIsAdultSpecification(),
new UserIsActiveSpecification(),
]);
// User is adult OR user with parents
$userHasAccessSpecification = new OrSpecification([
new UserIsAdultSpecification(),
new UserWithParentsSpecification(),
]);
use Vjik\Specification\NotSpecification;
// User is not adult
$userIsNotAdultSpecification = new NotSpecification(
new UserIsAdultSpecification()
);
$userIsAdultSpecification = new UserIsAdultSpecification();
// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::isSatisfiedBy expects User, but 'test' provided
$userIsAdultSpecification->isSatisfiedBy('test');
// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::satisfiedBy expects User, but 'test' provided
$userIsAdultSpecification->satisfiedBy('test');
// ERROR: InvalidArgument Incompatible types found for T (must have only one of User, Post)
$isActiveAdultUserSpecification = new AndSpecification([
new UserIsAdultSpecification(),
new PostIsActiveSpecificaion(),
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.