<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Dbfilter;


class WebStatistikParamsSQLFilter extends BasicSQLFilter
{
  public const FILTER_OPTION_WEB_STATISTIK = 'web_statistic';

  protected function __construct()
  {
    parent::__construct();

    $this->registerDb('Id', 'id', static::TYPE_INTEGER);
    $this->registerDb('StatistikId', 's_id', static::TYPE_INTEGER);
    $this->registerDb('ParamName', 'param_name', static::TYPE_STRING);
    $this->registerDb('ParamValue', 'param_value', static::TYPE_STRING);
  }

  
  public function getTablePraeffix(): string
  {
    return 'web_statistic_params';
  }

  
  public function equalsId($id): IFilter
  {
    $this->applyFilterCurrent(
      static::FILTER_OPTION_ID,
      SQLFilterUtils::filterEqualsIdInt('id', $id)
    );

    return $this;
  }

  public function applyWebStatistikFilter(WebStatistikSQLFilter $filter): WebStatistikParamsSQLFilter
  {
    $filter->makeImmutable();

    if ($filter->isEmpty()) {
      return $this;
    }

    $this->applyFilterCurrent(
      static::FILTER_OPTION_WEB_STATISTIK,
      function(\SelectQueryBuilder $builder) use ($filter): \SelectQueryBuilder {
        $paramsBuilder = $filter->createBuilder($builder->getDBOperation())
          ->select('id')
          ->from('web_statistic');

        $var = $builder->uniqueVar();

        return $builder
          ->and(":table:.s_id IN (:$var:)")
          ->paramSelectQuery($var, $paramsBuilder);
      }
    );

    return $this;
  }
}