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

use Kufer\Web\Classes\Identifier\Identifier;

class IdParams extends Parameters
{
  protected $idColName;
  protected $guidColName;

  public function __construct($idColName, $guidColName)
  {
    $this->idColName = $idColName;
    $this->guidColName = $guidColName;
  }

  public function is_valid_type($value)
  {
    return ($value instanceof Identifier) && !$value->isEmpty();
  }

  
  public function prepare_value($value, $dboperation)
  {
    $buffer = '';

    if($value->isEmpty()) {
      \Logger::logg('ParamId: Übergebener Identifier hat weder gültige ID noch GUID. Das SQL-Statement wird so gesetzt, dass diese ID nicht gefunden wird. Backtrace: ' . DebugOperations::backtrace(), 746);
      return '(1=0)';
    }

    if (!$value->isGuidEmpty()) {
      $buffer .= "($this->guidColName IS NULL OR $this->guidColName = {$dboperation->getGuidForSql($value->getGuid())})";
    }
    if (!$value->isIdEmpty()) {
      if ($buffer != '') {
        $buffer .= ' AND ';
      }
      $buffer .= "$this->idColName = {$dboperation->getInt4Sql($value->getId())}";
    }

    return $buffer;
  }
}