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

class OrderBy
{
  private $name;
  private $asc;
  private $prefix;
  private $castToInt;

  public function __construct($name, $asc = true, $prefix = '', $castToInt = false)
  {
    $this->name = $name;
    $this->asc = $asc;
    $this->prefix = $prefix;
    $this->castToInt = $castToInt;
  }

  public function getNameOhnePrefix()
  {
    return $this->name;
  }

  public function getNameMitPrefix()
  {
    $newPrefix = (strcspn($this->getNameOhnePrefix(), '.(') == StringOperations::strlen($this->getNameOhnePrefix())) ? $this->getPrefix() : '';

    return $newPrefix . $this->getNameOhnePrefix();
  }

  public function getPrefix()
  {
    return $this->prefix;
  }

  public function setPrefix($prefix)
  {
    $this->prefix = $prefix;
  }

  public function setCastToInt($castToInt)
  {
    $this->castToInt = $castToInt;
  }

  public function castToType($name, $dbparser)
  {
    if ($this->castToInt) {
      $name = $dbparser->cast2Int($name);
    }

    return $name;
  }

  public function getOrderAsBool()
  {
    return $this->asc;
  }

  public function getOrderAsString()
  {
    return ($this->asc) ? 'ASC' : 'DESC';
  }
}