<?php


namespace App\DAO\Filter;


use App\Utils\Builder;
use App\Utils\Collections\GeneralCollection;
use App\Utils\ResponseException;

class QueryFilterBuilder implements Builder
{
    private $value;
    private $name_param;
    private $is_exact;
    private $table;
    private $comparator;

    public function setValue($value): QueryFilterBuilder
    {
        $this->value = $value;
        return $this;
    }

    public function setNameParam(string $name_param): QueryFilterBuilder
    {
        $this->name_param = $name_param;
        return $this;
    }

    public function setIsExact(bool $is_exact): QueryFilterBuilder
    {
        $this->is_exact = $is_exact;
        return $this;
    }

    public function setTable(string $table) : QueryFilterBuilder
    {
        $this->table = $table;
        return $this;
    }

    public function setComparator(?string $comparator = '') : QueryFilterBuilder
    {
        if ($comparator) {
            if ($this->is_exact) {
                throw new ResponseException(['comparator' => 'Não pode existir $comparator quando $is_exact está true.'],'Erro na construção do objeto QueryFilter');
            } else {
                $valid_comparators = new GeneralCollection(['<>','>','<','>=','<=']);
                if (!$valid_comparators->includes($comparator)) {
                    throw new ResponseException(['comparator' => "Comparator $comparator não é válido."],'Erro na construção do objeto QueryFilter');
                }
            }
        } else {
            $comparator = '';
        }
        $this->comparator = $comparator;
        return $this;
    }

    public function build() : QueryFilter
    {
        $query_filter = new QueryFilter();
        $query_filter->setValue($this->value);
        $query_filter->setNameParam($this->name_param);
        $query_filter->setTable($this->table);
        $query_filter->setIsExact($this->is_exact);
        $query_filter->setComparator($this->comparator ?? '');
        return $query_filter;
    }


}