1. Go to this page and download the library: Download ngmy/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/ */
ngmy / specification example snippets
declare(strict_types=1);
use Ngmy\Specification\AbstractSpecification;
/**
* Popular user specification.
*
* @extends AbstractSpecification<User>
*/
class PopularUserSpecification extends AbstractSpecification
{
/**
* {@inheritdoc}
*/
public function isSatisfiedBy($candidate): bool
{
return $candidate->getVotes() > 100;
}
}
$spec = new PopularUserSpecification();
$spec->isSatisfiedBy($user);
$spec = new PopularUserSpecification();
$popularUsers = array_filter(function (User $users) use ($spec): void {
return $spec->isSatisfiedBy($user);
}, $users);
use Illuminate\Contracts\Database\Eloquent\Builder;
/**
* {@inheritdoc}
*/
public function applyToEloquent(Builder $query): void
{
$query->where('votes', '>', 100);
}
$query = User::query(); // User is your Eloquent model
$spec = new PopularUserSpecification();
$spec->applyToEloquent($query);
$popularUsers = $query->get();
use Doctrine\ORM\QueryBuilder;
use Ngmy\Specification\Support\DoctrineUtils;
/**
* {@inheritdoc}
*/
public function applyToDoctrine(QueryBuilder $queryBuilder): void
{
$queryBuilder->andWhere($queryBuilder->expr()->gt(
DoctrineUtils::getRootAliasedColumnName($queryBuilder, 'votes'),
DoctrineUtils::createUniqueNamedParameter($this, $queryBuilder, 100),
));
}
/** @var \Doctrine\ORM\EntityManager $entityManager */
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')->from(User::class, 'u'); // User is your Doctrine entity
$spec = new PopularUserSpecification();
$spec->applyToDoctrine($queryBuilder);
$popularUsers = $queryBuilder->getQuery()->getResult();
$spec1 = new Specification1();
$spec2 = new Specification2();
$spec3 = $spec1->and($spec2);
$spec1 = new Specification1();
$spec2 = new Specification2();
$spec3 = $spec1->or($spec2);
$spec1 = new Specification1();
$spec2 = $spec1->not();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.