PHP7 FluentInterface

This commit is contained in:
Dominik Liebler 2016-09-23 10:24:23 +02:00
parent b556436fa2
commit de196765cf
5 changed files with 413 additions and 262 deletions

View File

@ -2,79 +2,51 @@
namespace DesignPatterns\Structural\FluentInterface;
/**
* class SQL.
*/
class Sql
{
/**
* @var array
*/
protected $fields = array();
private $fields = [];
/**
* @var array
*/
protected $from = array();
private $from = [];
/**
* @var array
*/
protected $where = array();
private $where = [];
/**
* adds select fields.
*
* @param array $fields
*
* @return SQL
*/
public function select(array $fields = array())
public function select(array $fields): Sql
{
$this->fields = $fields;
return $this;
}
/**
* adds a FROM clause.
*
* @param string $table
* @param string $alias
*
* @return SQL
*/
public function from($table, $alias)
public function from(string $table, string $alias): Sql
{
$this->from[] = $table.' AS '.$alias;
return $this;
}
/**
* adds a WHERE condition.
*
* @param string $condition
*
* @return SQL
*/
public function where($condition)
public function where(string $condition): Sql
{
$this->where[] = $condition;
return $this;
}
/**
* Gets the query, just an example of building a query,
* no check on consistency.
*
* @return string
*/
public function getQuery()
public function __toString(): string
{
return 'SELECT '.implode(',', $this->fields)
.' FROM '.implode(',', $this->from)
.' WHERE '.implode(' AND ', $this->where);
return sprintf(
'SELECT %s FROM %s WHERE %s',
join(', ', $this->fields),
join(', ', $this->from),
join(' AND ', $this->where)
);
}
}

View File

@ -4,19 +4,15 @@ namespace DesignPatterns\Structural\FluentInterface\Tests;
use DesignPatterns\Structural\FluentInterface\Sql;
/**
* FluentInterfaceTest tests the fluent interface SQL.
*/
class FluentInterfaceTest extends \PHPUnit_Framework_TestCase
{
public function testBuildSQL()
{
$instance = new Sql();
$query = $instance->select(array('foo', 'bar'))
$query = (new Sql())
->select(['foo', 'bar'])
->from('foobar', 'f')
->where('f.bar = ?')
->getQuery();
->where('f.bar = ?');
$this->assertEquals('SELECT foo,bar FROM foobar AS f WHERE f.bar = ?', $query);
$this->assertEquals('SELECT foo, bar FROM foobar AS f WHERE f.bar = ?', (string) $query);
}
}

View File

@ -1,22 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\FluentInterface\Sql</OriginalElement>
<nodes>
<node x="0.0" y="0.0">\DesignPatterns\Structural\FluentInterface\Sql</node>
</nodes>
<notes />
<edges />
<settings layout="Hierarchic Group" zoom="1.0" x="108.5" y="84.0" />
<SelectedNodes>
<node>\DesignPatterns\Structural\FluentInterface\Sql</node>
</SelectedNodes>
<Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\FluentInterface\Sql</OriginalElement>
<nodes>
<node x="0.0" y="0.0">\DesignPatterns\Structural\FluentInterface\Sql</node>
</nodes>
<notes />
<edges />
<settings layout="Hierarchic Group" zoom="1.0" x="25.5" y="14.5" />
<SelectedNodes />
<Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Methods</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 24 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 68 KiB