mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
PSR-0 for namespace and filename + tests
This commit is contained in:
66
FluentInterface/SQL.php
Normal file
66
FluentInterface/SQL.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\FluentInterface;
|
||||
|
||||
/**
|
||||
* fluent interface pattern
|
||||
*
|
||||
* Purpose:
|
||||
* to write code that is easy readable just like "real" sentences
|
||||
*
|
||||
* Examples:
|
||||
* - Doctrine2's QueryBuilder works something like that example class below
|
||||
* - PHPUnit uses fluent interfaces to build mock objects
|
||||
* - Yii Framework: CDbCommand and CActiveRecord use this pattern too
|
||||
*
|
||||
*/
|
||||
class SQL
|
||||
{
|
||||
protected $_fields = array();
|
||||
protected $_from = array();
|
||||
protected $_where = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $fields
|
||||
* @return SQL
|
||||
*/
|
||||
public function select(array $fields = array())
|
||||
{
|
||||
$this->_fields = $fields;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $alias
|
||||
* @return SQL
|
||||
*/
|
||||
public function from($table, $alias)
|
||||
{
|
||||
$this->_from[] = $table . ' AS ' . $alias;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $condition
|
||||
* @return SQL
|
||||
*/
|
||||
public function where($condition)
|
||||
{
|
||||
$this->_where[] = $condition;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the query, just an example of building a query,
|
||||
* no check on consistency
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return 'SELECT ' . implode(',', $this->_fields)
|
||||
. ' FROM ' . implode(',', $this->_from)
|
||||
. ' WHERE ' . implode(' AND ', $this->_where);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user