1. Go to this page and download the library: Download webmozart/expression 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/ */
webmozart / expression example snippets
use Webmozart\Expression\Expression;
interface PersonRepository
{
public function findPersons(Expression $expr);
}
class PersonRepositoryImpl implements PersonRepository
{
private $persons = [];
public function findPersons(Expression $expr)
{
return Expr::filter($this->persons, $expr);
}
}
class IsPremium extends Method
{
public function __construct()
{
parent::__construct('isPremium', [], Expr::same(true));
}
}
class HasPreviousBookings extends Method
{
public function __construct()
{
parent::__construct(
'getBookings',
[],
Expr::count(Expr::greaterThan(0))
);
}
}
// Check if a customer is premium
if ((new IsPremium())->evaluate($customer)) {
// ...
}
// Get premium customers with bookings
$customers = $repo->findCustomers(Expr::andX([
new IsPremium(),
new HasPreviousBookings(),
]));
use Webmozart\Expression\Traversal\ExpressionVisitor;
class QueryBuilderVisitor implements ExpressionVisitor
{
private $qb;
public function __construct(QueryBuilder $qb)
{
$this->qb = $qb;
}
public function enterExpression(Expression $expr)
{
// configure the $qb...
}
public function leaveExpression(Expression $expr)
{
// configure the $qb...
}
}
public function expressionToQueryBuilder(Expression $expr)
{
$qb = new QueryBuilder();
$traverser = new ExpressionTraverser();
$traverser->addVisitor(new QueryBuilderVisitor($qb));
$traverser->traverse($expr);
return $qb;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.