mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 13:37:27 +02:00
cs FluentInterface
This commit is contained in:
@@ -11,56 +11,78 @@ namespace DesignPatterns\FluentInterface;
|
|||||||
* Examples:
|
* Examples:
|
||||||
* - Doctrine2's QueryBuilder works something like that example class below
|
* - Doctrine2's QueryBuilder works something like that example class below
|
||||||
* - PHPUnit uses fluent interfaces to build mock objects
|
* - PHPUnit uses fluent interfaces to build mock objects
|
||||||
* - Yii Framework: CDbCommand and CActiveRecord use this pattern too
|
* - Yii Framework: CDbCommand and CActiveRecord use this pattern, too
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
class SQL
|
class SQL
|
||||||
{
|
{
|
||||||
protected $_fields = array();
|
/**
|
||||||
protected $_from = array();
|
* @var array
|
||||||
protected $_where = array();
|
*/
|
||||||
|
protected $fields = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $from = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $where = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* adds select fields
|
||||||
*
|
*
|
||||||
* @param array $fields
|
* @param array $fields
|
||||||
|
*
|
||||||
* @return SQL
|
* @return SQL
|
||||||
*/
|
*/
|
||||||
public function select(array $fields = array())
|
public function select(array $fields = array())
|
||||||
{
|
{
|
||||||
$this->_fields = $fields;
|
$this->fields = $fields;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* adds a FROM clause
|
||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
|
*
|
||||||
* @return SQL
|
* @return SQL
|
||||||
*/
|
*/
|
||||||
public function from($table, $alias)
|
public function from($table, $alias)
|
||||||
{
|
{
|
||||||
$this->_from[] = $table . ' AS ' . $alias;
|
$this->from[] = $table . ' AS ' . $alias;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* adds a WHERE condition
|
||||||
|
*
|
||||||
* @param string $condition
|
* @param string $condition
|
||||||
|
*
|
||||||
* @return SQL
|
* @return SQL
|
||||||
*/
|
*/
|
||||||
public function where($condition)
|
public function where($condition)
|
||||||
{
|
{
|
||||||
$this->_where[] = $condition;
|
$this->where[] = $condition;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the query, just an example of building a query,
|
* Gets the query, just an example of building a query,
|
||||||
* no check on consistency
|
* no check on consistency
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getQuery()
|
public function getQuery()
|
||||||
{
|
{
|
||||||
return 'SELECT ' . implode(',', $this->_fields)
|
return 'SELECT ' . implode(',', $this->fields)
|
||||||
. ' FROM ' . implode(',', $this->_from)
|
. ' FROM ' . implode(',', $this->from)
|
||||||
. ' WHERE ' . implode(' AND ', $this->_where);
|
. ' WHERE ' . implode(' AND ', $this->where);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user