1. Go to this page and download the library: Download rasuvaeff/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/ */
rasuvaeff / specification example snippets
use Rasuvaeff\Specification\SpecificationBuilder;
use Rasuvaeff\Specification\QueryApplier;
$spec = SpecificationBuilder::create()
->whereEqual('status', 'active')
->whereGreaterThan('age', 18)
->whereIn('role', ['admin', 'moderator'])
->orderBy(['created_at' => 'DESC'])
->limit(20)
->build();
$query = (new \Yiisoft\Db\Query\Query($db))->from('users');
QueryApplier::apply($spec, $query);
$rows = $query->all();
use Rasuvaeff\Specification\SpecificationBuilder;
use Rasuvaeff\Specification\QueryApplier;
$spec = SpecificationBuilder::create()
->whereEqual('status', 'active')
->whereGreaterThan('age', 18)
->whereNull('deleted_at')
->build();
$query = (new Yiisoft\Db\Query\Query($db))->from('users');
QueryApplier::apply($spec, $query);
$rows = $query->all();
use Rasuvaeff\Specification\ComparisonSpecification;
use Rasuvaeff\Specification\CompositeSpecification;
use Rasuvaeff\Specification\NotSpecification;
use Rasuvaeff\Specification\OffsetSpecification;
use Rasuvaeff\Specification\OrConditionSpecification;
use Rasuvaeff\Specification\OrSpecification;
use Rasuvaeff\Specification\RawSpecification;
// AND conditions
$spec = CompositeSpecification::create()
->withComparison('status', 'active')
->withComparison('age', 18, '>')
->withOrderBy(['created_at' => 'DESC'])
->withLimit(20)
->withOffset(40);
// OR condition arrays via OrConditionSpecification.
$orConditionSpec = CompositeSpecification::create()
->withOrCondition(['status' => 'active', 'type' => 'pending']);
// OR conditions
$orSpec = OrSpecification::create(
ComparisonSpecification::equal('type', 'admin'),
ComparisonSpecification::equal('type', 'moderator'),
);
// NOT condition
$notSpec = new NotSpecification(
new ComparisonSpecification('status', 'banned'),
);
// Raw SQL — see the Security note below
$rawSpec = new RawSpecification('age > :age', ['age' => 18]);
// Offset for pagination
$offset = CompositeSpecification::create()
->withLimit(10)
->withOffset(20);
// Raw SQL — see the Security note below
$rawComposite = CompositeSpecification::create()
->withRaw('price > :min AND price < :max', ['min' => 10, 'max' => 100]);