вот такие пироги
на ковырялся (+/-)
<?php
class SQLParts
{
private $sql;
private $start;
private $table;
private $fields = '*';
private $data; // array
private $objectId; // where id = ?
public function setStart($start)
{
$this->start = $start;
}
public function getStart()
{
return $this->start;
}
public function setFields($fields)
{
$this->fields = $fields;
}
public function getFields()
{
return $this->fields;
}
public function setData(array $data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
public function setTable($table)
{
$this->table = $table;
}
public function getTable()
{
return $this->table;
}
public function setParams($args)
{
foreach($args as $param => $value) {
$method = 'set' . ucfirst($param);
$this->$method($value);
}
}
}
// Strategy Pattern //
interface SqlTypeInterface
{
public function init($args);
}
class SqlType extends SQLParts
{
public function init($args)
{
$this->setParams($args);
}
}
class SelectType extends SqlType implements SqlTypeInterface{}
class InsertType extends SqlType implements SqlTypeInterface{}
class UpdateType extends SqlType implements SqlTypeInterface{}
class DeleteType extends SqlType implements SqlTypeInterface{}
class OtherType extends SqlType implements SqlTypeInterface{}
class SQLBuilder
{
private $sql;
private $type;
public function type(SqlTypeInterface $type)
{
$this->type = $type;
$this->type->setStart(strtolower(substr(get_class($this->type), 0, -4)));
return $this;
}
public function initType($args)
{
return $this->type->init($args);
}
public function test()
{
echo '<pre>' . print_r($this, 1) . '</pre>';
}
}
$sql = new SQLBuilder;
$args = ['fields' => ['id', 'name'], 'table' => 'sqltable'];
$sql->type(new SelectType)->initType($args);
$sql->test();