. */ namespace SP\Mvc\Model; /** * Class QueryCondition * * @package SP\Mvc\Model */ final class QueryCondition { const CONDITION_AND = ' AND '; const CONDITION_OR = ' OR '; /** * @var array */ protected $query = []; /** * @var array */ protected $param = []; /** * @param string $query * @param array $params * * @return QueryCondition */ public function addFilter($query, array $params = null) { $this->query[] = '(' . $query . ')'; if ($params !== null) { $this->param = array_merge($this->param, $params); } return $this; } /** * @param string $type * * @return string|null */ public function getFilters($type = self::CONDITION_AND) { if ($type !== self::CONDITION_AND && $type !== self::CONDITION_OR) { throw new \RuntimeException(__u('Tipo de filtro inválido')); } return $this->hasFilters() ? '(' . implode($type, $this->query) . ')' : null; } /** * @return bool */ public function hasFilters() { return !empty($this->query); } /** * @return array */ public function getParams() { return $this->param; } /** * @return int */ public function getFiltersCount() { return count($this->query); } }