<?php
namespace Imi\Db\Query;
use Imi\Db\Query\Traits\TRaw;
use Imi\Db\Query\Interfaces\IField;
use Imi\Db\Query\Traits\TKeyword;
class Field implements IField
{
use TRaw;
use TKeyword;
protected $database;
protected $table;
protected $field;
protected $alias;
public function __construct(string $database = null, string $table = null, string $field = null, string $alias = null)
{
$this->database = $database;
$this->table = $table;
$this->field = $field;
$this->alias = $alias;
}
public function getDatabase(): string
{
return $this->database;
}
public function getTable(): string
{
return $this->table;
}
public function getField(): string
{
return $this->field;
}
public function getAlias(): string
{
return $this->alias;
}
public function setDatabase(string $database = null)
{
$this->database = $database;
}
public function setTable(string $table = null)
{
$this->table = $table;
}
public function setField(string $field = null)
{
$this->field = $field;
}
public function setAlias(string $alias = null)
{
$this->alias = $alias;
}
public function setValue($value)
{
$matches = $this->parseKeywordText($value);
if(isset($matches['keywords']))
{
$keywords = $matches['keywords'];
if(isset($keywords[2]))
{
$this->database = $keywords[0];
$this->table = $keywords[1];
$this->field = $keywords[2];
}
else if(isset($keywords[1]))
{
$this->database = null;
$this->table = $keywords[0];
$this->field = $keywords[1];
}
else if(isset($keywords[0]))
{
$this->database = null;
$this->table = null;
$this->field = $keywords[0];
}
$this->alias = $matches['alias'];
}
}
public function __toString()
{
if($this->isRaw)
{
return $this->rawSQL;
}
return $this->parseKeywordToText([
$this->database,
$this->table,
$this->field
], $this->alias);
}
public function getBinds()
{
return [];
}
}