<?php
namespace ProjectBiz\DatabaseBundle\Database\Criteria;
use Doctrine\DBAL\Query\QueryBuilder;
use ProjectBiz\PortalBundle\Exceptions\PortalException;
class CriteriaComposite implements CriteriaBuilderInterface, \JsonSerializable
{
const msgUnknownOperator = 'Der Operator "%operator%" ist unbekannt.';
private $operator;
/** @var CriteriaBuilderInterface[] */
private $params;
public function __construct($operator, $params)
{
$this->operator = strtoupper($operator);
$this->params = $params;
}
public function buildCriteria(QueryBuilder $builder, array $columnMap, $processedView, $mt_name = 'mt')
{
$params = array_map(
function (CriteriaBuilderInterface $param) use ($builder, $columnMap, $processedView, $mt_name) {
return $param->buildCriteria($builder, $columnMap, $processedView, $mt_name);
},
$this->params
);
if ($this->operator == 'AND') {
return call_user_func_array([$builder->expr(), 'andX'], $params);
} else {
if ($this->operator == 'OR') {
return call_user_func_array([$builder->expr(), 'orX'], $params);
} else {
throw new PortalException(self::msgUnknownOperator, ['%operator%' => $this->operator]);
}
}
}
/**
* (PHP 5 >= 5.4.0)<br/>
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
$params = [];
foreach ($this->params as $param) {
$params[] = $param->jsonSerialize();
}
return [
'type' => 'composite',
'operator' => $this->operator,
'params' => $params
];
}
}