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