Skip to content

Commit 5852e62

Browse files
author
Dominik Liebler
committed
cs FluentInterface
1 parent b05f570 commit 5852e62

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

FluentInterface/SQL.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,78 @@
1111
* Examples:
1212
* - Doctrine2's QueryBuilder works something like that example class below
1313
* - PHPUnit uses fluent interfaces to build mock objects
14-
* - Yii Framework: CDbCommand and CActiveRecord use this pattern too
15-
*
14+
* - Yii Framework: CDbCommand and CActiveRecord use this pattern, too
1615
*/
1716
class SQL
1817
{
19-
protected $_fields = array();
20-
protected $_from = array();
21-
protected $_where = array();
18+
/**
19+
* @var array
20+
*/
21+
protected $fields = array();
22+
23+
/**
24+
* @var array
25+
*/
26+
protected $from = array();
27+
28+
/**
29+
* @var array
30+
*/
31+
protected $where = array();
2232

2333
/**
34+
* adds select fields
2435
*
2536
* @param array $fields
37+
*
2638
* @return SQL
2739
*/
2840
public function select(array $fields = array())
2941
{
30-
$this->_fields = $fields;
42+
$this->fields = $fields;
43+
3144
return $this;
3245
}
3346

3447
/**
48+
* adds a FROM clause
3549
*
3650
* @param string $table
3751
* @param string $alias
52+
*
3853
* @return SQL
3954
*/
4055
public function from($table, $alias)
4156
{
42-
$this->_from[] = $table . ' AS ' . $alias;
57+
$this->from[] = $table . ' AS ' . $alias;
58+
4359
return $this;
4460
}
4561

4662
/**
63+
* adds a WHERE condition
64+
*
4765
* @param string $condition
66+
*
4867
* @return SQL
4968
*/
5069
public function where($condition)
5170
{
52-
$this->_where[] = $condition;
71+
$this->where[] = $condition;
72+
5373
return $this;
5474
}
55-
75+
5676
/**
5777
* Gets the query, just an example of building a query,
5878
* no check on consistency
79+
*
80+
* @return string
5981
*/
6082
public function getQuery()
6183
{
62-
return 'SELECT ' . implode(',', $this->_fields)
63-
. ' FROM ' . implode(',', $this->_from)
64-
. ' WHERE ' . implode(' AND ', $this->_where);
84+
return 'SELECT ' . implode(',', $this->fields)
85+
. ' FROM ' . implode(',', $this->from)
86+
. ' WHERE ' . implode(' AND ', $this->where);
6587
}
6688
}

0 commit comments

Comments
 (0)