Skip to content

Commit de19676

Browse files
author
Dominik Liebler
committed
PHP7 FluentInterface
1 parent b556436 commit de19676

File tree

5 files changed

+413
-262
lines changed

5 files changed

+413
-262
lines changed

Structural/FluentInterface/Sql.php

+13-41
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,51 @@
22

33
namespace DesignPatterns\Structural\FluentInterface;
44

5-
/**
6-
* class SQL.
7-
*/
85
class Sql
96
{
107
/**
118
* @var array
129
*/
13-
protected $fields = array();
10+
private $fields = [];
1411

1512
/**
1613
* @var array
1714
*/
18-
protected $from = array();
15+
private $from = [];
1916

2017
/**
2118
* @var array
2219
*/
23-
protected $where = array();
20+
private $where = [];
2421

25-
/**
26-
* adds select fields.
27-
*
28-
* @param array $fields
29-
*
30-
* @return SQL
31-
*/
32-
public function select(array $fields = array())
22+
public function select(array $fields): Sql
3323
{
3424
$this->fields = $fields;
3525

3626
return $this;
3727
}
3828

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)
29+
public function from(string $table, string $alias): Sql
4830
{
4931
$this->from[] = $table.' AS '.$alias;
5032

5133
return $this;
5234
}
5335

54-
/**
55-
* adds a WHERE condition.
56-
*
57-
* @param string $condition
58-
*
59-
* @return SQL
60-
*/
61-
public function where($condition)
36+
public function where(string $condition): Sql
6237
{
6338
$this->where[] = $condition;
6439

6540
return $this;
6641
}
6742

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()
43+
public function __toString(): string
7544
{
76-
return 'SELECT '.implode(',', $this->fields)
77-
.' FROM '.implode(',', $this->from)
78-
.' WHERE '.implode(' AND ', $this->where);
45+
return sprintf(
46+
'SELECT %s FROM %s WHERE %s',
47+
join(', ', $this->fields),
48+
join(', ', $this->from),
49+
join(' AND ', $this->where)
50+
);
7951
}
8052
}

Structural/FluentInterface/Tests/FluentInterfaceTest.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44

55
use DesignPatterns\Structural\FluentInterface\Sql;
66

7-
/**
8-
* FluentInterfaceTest tests the fluent interface SQL.
9-
*/
107
class FluentInterfaceTest extends \PHPUnit_Framework_TestCase
118
{
129
public function testBuildSQL()
1310
{
14-
$instance = new Sql();
15-
$query = $instance->select(array('foo', 'bar'))
11+
$query = (new Sql())
12+
->select(['foo', 'bar'])
1613
->from('foobar', 'f')
17-
->where('f.bar = ?')
18-
->getQuery();
14+
->where('f.bar = ?');
1915

20-
$this->assertEquals('SELECT foo,bar FROM foobar AS f WHERE f.bar = ?', $query);
16+
$this->assertEquals('SELECT foo, bar FROM foobar AS f WHERE f.bar = ?', (string) $query);
2117
}
2218
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<Diagram>
3-
<ID>PHP</ID>
4-
<OriginalElement>\DesignPatterns\Structural\FluentInterface\Sql</OriginalElement>
5-
<nodes>
6-
<node x="0.0" y="0.0">\DesignPatterns\Structural\FluentInterface\Sql</node>
7-
</nodes>
8-
<notes />
9-
<edges />
10-
<settings layout="Hierarchic Group" zoom="1.0" x="108.5" y="84.0" />
11-
<SelectedNodes>
12-
<node>\DesignPatterns\Structural\FluentInterface\Sql</node>
13-
</SelectedNodes>
14-
<Categories>
15-
<Category>Fields</Category>
16-
<Category>Constants</Category>
17-
<Category>Constructors</Category>
18-
<Category>Methods</Category>
19-
</Categories>
20-
<VISIBILITY>private</VISIBILITY>
21-
</Diagram>
22-
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Diagram>
3+
<ID>PHP</ID>
4+
<OriginalElement>\DesignPatterns\Structural\FluentInterface\Sql</OriginalElement>
5+
<nodes>
6+
<node x="0.0" y="0.0">\DesignPatterns\Structural\FluentInterface\Sql</node>
7+
</nodes>
8+
<notes />
9+
<edges />
10+
<settings layout="Hierarchic Group" zoom="1.0" x="25.5" y="14.5" />
11+
<SelectedNodes />
12+
<Categories>
13+
<Category>Fields</Category>
14+
<Category>Constants</Category>
15+
<Category>Methods</Category>
16+
</Categories>
17+
<VISIBILITY>private</VISIBILITY>
18+
</Diagram>
19+
19.5 KB
Loading

0 commit comments

Comments
 (0)