From 5852e62966d09e715456be558ec1a3b2f333ebfc Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Wed, 11 Sep 2013 16:40:02 +0200 Subject: [PATCH] cs FluentInterface --- FluentInterface/SQL.php | 46 ++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/FluentInterface/SQL.php b/FluentInterface/SQL.php index 84034da..6074f70 100644 --- a/FluentInterface/SQL.php +++ b/FluentInterface/SQL.php @@ -11,56 +11,78 @@ namespace DesignPatterns\FluentInterface; * 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 - * + * - Yii Framework: CDbCommand and CActiveRecord use this pattern, too */ class SQL { - protected $_fields = array(); - protected $_from = array(); - protected $_where = array(); + /** + * @var array + */ + protected $fields = array(); /** + * @var array + */ + protected $from = array(); + + /** + * @var array + */ + protected $where = array(); + + /** + * adds select fields * * @param array $fields + * * @return SQL */ public function select(array $fields = array()) { - $this->_fields = $fields; + $this->fields = $fields; + return $this; } /** + * adds a FROM clause * * @param string $table * @param string $alias + * * @return SQL */ public function from($table, $alias) { - $this->_from[] = $table . ' AS ' . $alias; + $this->from[] = $table . ' AS ' . $alias; + return $this; } /** + * adds a WHERE condition + * * @param string $condition + * * @return SQL */ public function where($condition) { - $this->_where[] = $condition; + $this->where[] = $condition; + return $this; } - + /** * Gets the query, just an example of building a query, * no check on consistency + * + * @return string */ public function getQuery() { - return 'SELECT ' . implode(',', $this->_fields) - . ' FROM ' . implode(',', $this->_from) - . ' WHERE ' . implode(' AND ', $this->_where); + return 'SELECT ' . implode(',', $this->fields) + . ' FROM ' . implode(',', $this->from) + . ' WHERE ' . implode(' AND ', $this->where); } }