Skip to content

Commit 1cecbab

Browse files
author
Dominik Liebler
committed
readded in correct case
1 parent 51f6b7f commit 1cecbab

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Structural/FluentInterface/Sql.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\FluentInterface;
4+
5+
/**
6+
* class SQL
7+
*/
8+
class Sql
9+
{
10+
/**
11+
* @var array
12+
*/
13+
protected $fields = array();
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $from = array();
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $where = array();
24+
25+
/**
26+
* adds select fields
27+
*
28+
* @param array $fields
29+
*
30+
* @return SQL
31+
*/
32+
public function select(array $fields = array())
33+
{
34+
$this->fields = $fields;
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* adds a FROM clause
41+
*
42+
* @param string $table
43+
* @param string $alias
44+
*
45+
* @return SQL
46+
*/
47+
public function from($table, $alias)
48+
{
49+
$this->from[] = $table . ' AS ' . $alias;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* adds a WHERE condition
56+
*
57+
* @param string $condition
58+
*
59+
* @return SQL
60+
*/
61+
public function where($condition)
62+
{
63+
$this->where[] = $condition;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* Gets the query, just an example of building a query,
70+
* no check on consistency
71+
*
72+
* @return string
73+
*/
74+
public function getQuery()
75+
{
76+
return 'SELECT ' . implode(',', $this->fields)
77+
. ' FROM ' . implode(',', $this->from)
78+
. ' WHERE ' . implode(' AND ', $this->where);
79+
}
80+
}

0 commit comments

Comments
 (0)