diff --git a/Structural/FluentInterface/Sql.php b/Structural/FluentInterface/Sql.php index 58ba491..ed54992 100644 --- a/Structural/FluentInterface/Sql.php +++ b/Structural/FluentInterface/Sql.php @@ -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) + ); } } diff --git a/Structural/FluentInterface/Tests/FluentInterfaceTest.php b/Structural/FluentInterface/Tests/FluentInterfaceTest.php index ae4e656..b75e2f9 100644 --- a/Structural/FluentInterface/Tests/FluentInterfaceTest.php +++ b/Structural/FluentInterface/Tests/FluentInterfaceTest.php @@ -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); } } diff --git a/Structural/FluentInterface/uml/FluentInterface.uml b/Structural/FluentInterface/uml/FluentInterface.uml index 8fca787..552b641 100644 --- a/Structural/FluentInterface/uml/FluentInterface.uml +++ b/Structural/FluentInterface/uml/FluentInterface.uml @@ -1,22 +1,19 @@ - - - PHP - \DesignPatterns\Structural\FluentInterface\Sql - - \DesignPatterns\Structural\FluentInterface\Sql - - - - - - \DesignPatterns\Structural\FluentInterface\Sql - - - Fields - Constants - Constructors - Methods - - private - - + + + PHP + \DesignPatterns\Structural\FluentInterface\Sql + + \DesignPatterns\Structural\FluentInterface\Sql + + + + + + + Fields + Constants + Methods + + private + + diff --git a/Structural/FluentInterface/uml/uml.png b/Structural/FluentInterface/uml/uml.png index e49aa57..2f38396 100644 Binary files a/Structural/FluentInterface/uml/uml.png and b/Structural/FluentInterface/uml/uml.png differ diff --git a/Structural/FluentInterface/uml/uml.svg b/Structural/FluentInterface/uml/uml.svg index 60a8564..9418314 100644 --- a/Structural/FluentInterface/uml/uml.svg +++ b/Structural/FluentInterface/uml/uml.svg @@ -1,191 +1,377 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - fields - - - - - - - - - - from - - - - - - - - - - where - - - - - - - - - - - - - select(fields) - - - - - - - - - - from(table, alias) - - - - - - - - - - where(condition) - - - - - - - - - - getQuery() - - - - - - - - - - - - - Sql - - - Sql - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + from + + + + + + + + + + + + + + + + where + + + + + + + + + + + + + + + + fields + + + + + + + + + + + + + + + + select(fields) + + + + + + + + + + + + + from(table, alias) + + + + + + + + + + + + + where(condition) + + + + + + + + + + + + + __toString() + + + + + + + + + + + + + Sql + + + Sql + + + + + + + + + + + + + + + + + + + + + + from + + + + + + + + + + + + + + + + where + + + + + + + + + + + + + + + + fields + + + + + + + + + + + + + + + + + + + select(fields) + + + + + + + + + + + + + + + + from(table, alias) + + + + + + + + + + + + + + + + where(condition) + + + + + + + + + + + + + + + + __toString() + + + + + + + + + + + + + Sql + + + Sql + + +