1. Go to this page and download the library: Download nicmart/building 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/ */
nicmart / building example snippets
interface BooleanPredicate
{
/** @return bool **/
public function evaluate($value);
}
class EqualityPredicate implements BooleanPredicate
{
public function __construct($valueToBeEqualTo) { ... }
...
}
class GreaterThanPredicate implements BooleanPredicate
{
public function __construct($greaterThan) { ... }
...
}
class LssThanPredicate implements BooleanPredicate
{
public function __construct($greaterThan) { ... }
...
}
interface CompositePredicate extends BooleanPredicate
{
/** @return $this **/
public function add(BooleanPredicate $predicate);
}
class OrPredicate implements CompositePredicate
{
/** @return $this **/
public function add(BooleanPredicate $predicate) { ... }
}
class AndPredicate implements CompositePredicate
{
/** @return $this **/
public function add(BooleanPredicate $predicate) { ... }
}
use NicMart\Building\AbstractBuilder;
abstract class CompositePredicateBuilder extends AbstractBuilder
{
/** @var CompositePredicate **/
protected $building;
public function eq($value)
{
$this->building->add(new EqualityPredicate($value));
return $this;
}
public function greaterThan($value)
{
$this->building->add(new GreaterThanPredicate($value));
return $this;
}
public function lessThan($value)
{
$this->building->add(new LessThanPredicate($value));
return $this;
}
public function and()
{
return new AndPredicate($this->getAddCallback());
}
public function or()
{
return new OrPredicate($this->getAddCallback());
}
/** @return callable **/
private function getAddCallback()
{
return function(BooleanPredicate $predicate)
{
$this->getCompositePredicate()->add($predicate);
// This will be the return value of the end() method
return $this;
};
}
}
class OrPredicateBuilder extends CompositePredicateBuilder
{
public function __construct(callable $callback = null)
{
$this->building = new OrPredicate;
}
}
class AndPredicateBuilder extends CompositePredicateBuilder
{
public function __construct(callable $callback = null)
{
$this->building = new AndPredicate;
}
}