mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-10 18:06:20 +02:00
PHP7 FluentInterface
This commit is contained in:
@ -2,79 +2,51 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Structural\FluentInterface;
|
namespace DesignPatterns\Structural\FluentInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
* class SQL.
|
|
||||||
*/
|
|
||||||
class Sql
|
class Sql
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $fields = array();
|
private $fields = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $from = array();
|
private $from = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $where = array();
|
private $where = [];
|
||||||
|
|
||||||
/**
|
public function select(array $fields): Sql
|
||||||
* adds select fields.
|
|
||||||
*
|
|
||||||
* @param array $fields
|
|
||||||
*
|
|
||||||
* @return SQL
|
|
||||||
*/
|
|
||||||
public function select(array $fields = array())
|
|
||||||
{
|
{
|
||||||
$this->fields = $fields;
|
$this->fields = $fields;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function from(string $table, string $alias): Sql
|
||||||
* adds a FROM clause.
|
|
||||||
*
|
|
||||||
* @param string $table
|
|
||||||
* @param string $alias
|
|
||||||
*
|
|
||||||
* @return SQL
|
|
||||||
*/
|
|
||||||
public function from($table, $alias)
|
|
||||||
{
|
{
|
||||||
$this->from[] = $table.' AS '.$alias;
|
$this->from[] = $table.' AS '.$alias;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function where(string $condition): Sql
|
||||||
* adds a WHERE condition.
|
|
||||||
*
|
|
||||||
* @param string $condition
|
|
||||||
*
|
|
||||||
* @return SQL
|
|
||||||
*/
|
|
||||||
public function where($condition)
|
|
||||||
{
|
{
|
||||||
$this->where[] = $condition;
|
$this->where[] = $condition;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function __toString(): string
|
||||||
* Gets the query, just an example of building a query,
|
|
||||||
* no check on consistency.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getQuery()
|
|
||||||
{
|
{
|
||||||
return 'SELECT '.implode(',', $this->fields)
|
return sprintf(
|
||||||
.' FROM '.implode(',', $this->from)
|
'SELECT %s FROM %s WHERE %s',
|
||||||
.' WHERE '.implode(' AND ', $this->where);
|
join(', ', $this->fields),
|
||||||
|
join(', ', $this->from),
|
||||||
|
join(' AND ', $this->where)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,19 +4,15 @@ namespace DesignPatterns\Structural\FluentInterface\Tests;
|
|||||||
|
|
||||||
use DesignPatterns\Structural\FluentInterface\Sql;
|
use DesignPatterns\Structural\FluentInterface\Sql;
|
||||||
|
|
||||||
/**
|
|
||||||
* FluentInterfaceTest tests the fluent interface SQL.
|
|
||||||
*/
|
|
||||||
class FluentInterfaceTest extends \PHPUnit_Framework_TestCase
|
class FluentInterfaceTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testBuildSQL()
|
public function testBuildSQL()
|
||||||
{
|
{
|
||||||
$instance = new Sql();
|
$query = (new Sql())
|
||||||
$query = $instance->select(array('foo', 'bar'))
|
->select(['foo', 'bar'])
|
||||||
->from('foobar', 'f')
|
->from('foobar', 'f')
|
||||||
->where('f.bar = ?')
|
->where('f.bar = ?');
|
||||||
->getQuery();
|
|
||||||
|
|
||||||
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,11 @@
|
|||||||
</nodes>
|
</nodes>
|
||||||
<notes />
|
<notes />
|
||||||
<edges />
|
<edges />
|
||||||
<settings layout="Hierarchic Group" zoom="1.0" x="108.5" y="84.0" />
|
<settings layout="Hierarchic Group" zoom="1.0" x="25.5" y="14.5" />
|
||||||
<SelectedNodes>
|
<SelectedNodes />
|
||||||
<node>\DesignPatterns\Structural\FluentInterface\Sql</node>
|
|
||||||
</SelectedNodes>
|
|
||||||
<Categories>
|
<Categories>
|
||||||
<Category>Fields</Category>
|
<Category>Fields</Category>
|
||||||
<Category>Constants</Category>
|
<Category>Constants</Category>
|
||||||
<Category>Constructors</Category>
|
|
||||||
<Category>Methods</Category>
|
<Category>Methods</Category>
|
||||||
</Categories>
|
</Categories>
|
||||||
<VISIBILITY>private</VISIBILITY>
|
<VISIBILITY>private</VISIBILITY>
|
||||||
|
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 |
Reference in New Issue
Block a user