<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Utils\FilterSearch\Method;

use Kufer\Web\Classes\Dbfilter\ISQLFilter;

class TranslateValueMethod implements ISearchFilterMethod
{
  
  protected $method;

  
  protected $translateMap;

  
  protected $defaultValue;

  public function __construct(ISearchFilterMethod $method, array $translateMap, $defaultValue)
  {
    $this->method = $method;
    $this->translateMap = $translateMap;
    $this->defaultValue = $defaultValue;
  }

  public function fromRequest($value)
  {
    return $this->method->fromRequest($value);
  }

  public function fromSession($value)
  {
    return $this->method->fromSession($value);
  }

  public function toSession($value)
  {
    return $this->method->toSession($value);
  }

  public function apply($filter, $value): ISQLFilter
  {
    $realValue = $this->translateMap[$value] ?? $this->defaultValue;

    if (!is_null($realValue)) {
      return $this->method->apply($filter, $realValue);
    } else {
      return $filter;
    }
  }
}