|
11 | 11 | * Examples:
|
12 | 12 | * - Doctrine2's QueryBuilder works something like that example class below
|
13 | 13 | * - 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 |
16 | 15 | */
|
17 | 16 | class SQL
|
18 | 17 | {
|
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(); |
22 | 32 |
|
23 | 33 | /**
|
| 34 | + * adds select fields |
24 | 35 | *
|
25 | 36 | * @param array $fields
|
| 37 | + * |
26 | 38 | * @return SQL
|
27 | 39 | */
|
28 | 40 | public function select(array $fields = array())
|
29 | 41 | {
|
30 |
| - $this->_fields = $fields; |
| 42 | + $this->fields = $fields; |
| 43 | + |
31 | 44 | return $this;
|
32 | 45 | }
|
33 | 46 |
|
34 | 47 | /**
|
| 48 | + * adds a FROM clause |
35 | 49 | *
|
36 | 50 | * @param string $table
|
37 | 51 | * @param string $alias
|
| 52 | + * |
38 | 53 | * @return SQL
|
39 | 54 | */
|
40 | 55 | public function from($table, $alias)
|
41 | 56 | {
|
42 |
| - $this->_from[] = $table . ' AS ' . $alias; |
| 57 | + $this->from[] = $table . ' AS ' . $alias; |
| 58 | + |
43 | 59 | return $this;
|
44 | 60 | }
|
45 | 61 |
|
46 | 62 | /**
|
| 63 | + * adds a WHERE condition |
| 64 | + * |
47 | 65 | * @param string $condition
|
| 66 | + * |
48 | 67 | * @return SQL
|
49 | 68 | */
|
50 | 69 | public function where($condition)
|
51 | 70 | {
|
52 |
| - $this->_where[] = $condition; |
| 71 | + $this->where[] = $condition; |
| 72 | + |
53 | 73 | return $this;
|
54 | 74 | }
|
55 |
| - |
| 75 | + |
56 | 76 | /**
|
57 | 77 | * Gets the query, just an example of building a query,
|
58 | 78 | * no check on consistency
|
| 79 | + * |
| 80 | + * @return string |
59 | 81 | */
|
60 | 82 | public function getQuery()
|
61 | 83 | {
|
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); |
65 | 87 | }
|
66 | 88 | }
|
0 commit comments