- First commit, User loading and Authentication are working
This commit is contained in:
commit
8d1a310921
15 files changed
+1274
No files matched your search
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Ldap;
|
||||
|
||||
use Ulmus;
|
||||
|
||||
class QueryBuilder implements Ulmus\Query\QueryBuilderInterface
|
||||
{
|
||||
public Query\Filter $where;
|
||||
|
||||
public Ulmus\Query\QueryBuilderInterface $parent;
|
||||
|
||||
/**
|
||||
* Those are the parameters we are going to bind to PDO.
|
||||
*/
|
||||
public array $parameters = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* Those values are to be inserted or updated
|
||||
*/
|
||||
public array $values = [];
|
||||
|
||||
public string $whereConditionOperator = Ulmus\Query\Where::CONDITION_AND;
|
||||
|
||||
public string $havingConditionOperator = Ulmus\Query\Where::CONDITION_AND;
|
||||
|
||||
protected int $parameterIndex = 0;
|
||||
|
||||
protected array $queryStack = [];
|
||||
|
||||
public function select($field) : self
|
||||
{
|
||||
if ( null !== ( $select = $this->getFragment(Query\Select::class) ) ) {
|
||||
$select->add($field);
|
||||
}
|
||||
else {
|
||||
$select = new Query\Select();
|
||||
$select->set($field);
|
||||
$this->push($select);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function from(string $table, ? string $alias, ? string $schema) : self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function update(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete() : self
|
||||
{
|
||||
if ( ! $this->getFragment(Ulmus\Query\Delete::class) ) {
|
||||
$this->push(new Ulmus\Query\Delete());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function open(string $condition = Ulmus\Query\Where::CONDITION_AND) : self
|
||||
{
|
||||
if ( null !== ($this->where ?? null) ) {
|
||||
$this->where->conditionList[] = $new = new Query\Filter($this, $condition);
|
||||
$this->where = $new;
|
||||
}
|
||||
else {
|
||||
$this->where = new Query\Filter($this, $condition);
|
||||
$this->push($this->where);
|
||||
$this->where->conditionList[] = $new = new Query\Filter($this, $condition);
|
||||
$this->where = $new;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function close() : self
|
||||
{
|
||||
if ( null !== ($this->where ?? null) && $this->where->parent ) {
|
||||
|
||||
# if an enclosure was opened, and nothing done, we must remove the unused node
|
||||
if ( empty($this->where->conditionList) && (count($this->where->parent->conditionList) === 1) ) {
|
||||
unset($this->where->parent->conditionList);
|
||||
}
|
||||
|
||||
$this->where = $this->where->parent;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where(/* stringable*/ $field, $value, string $operator = Ulmus\Query\Where::OPERATOR_EQUAL, string $condition = Ulmus\Query\Where::CONDITION_AND, bool $not = false) : self
|
||||
{
|
||||
# Empty IN case
|
||||
if ( [] === $value ) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ( $this->where ?? false ) {
|
||||
$where = $this->where;
|
||||
}
|
||||
elseif ( null === ( $where = $this->getFragment(Query\Filter::class) ) ) {
|
||||
$this->where = $where = new Query\Filter($this);
|
||||
$this->push($where);
|
||||
}
|
||||
|
||||
$this->whereConditionOperator = $operator;
|
||||
|
||||
$where->add($field, $value, $operator, $condition, $not);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function notWhere($field, $value, string $operator = Ulmus\Query\Where::CONDITION_AND) : self
|
||||
{
|
||||
return $this->where($field, $value, $operator, true);
|
||||
}
|
||||
|
||||
public function limit(int $value) : self
|
||||
{
|
||||
if ( null === $limit = $this->getFragment(Ulmus\Query\Limit::class) ) {
|
||||
$limit = new Query\Limit();
|
||||
$this->push($limit);
|
||||
}
|
||||
|
||||
$limit->set($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function offset(int $value) : self
|
||||
{
|
||||
if ( null === $offset = $this->getFragment(Ulmus\Query\Offset::class) ) {
|
||||
$offset = new Ulmus\Query\Offset();
|
||||
$this->push($offset);
|
||||
}
|
||||
|
||||
$offset->set($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orderBy(string $field, ? string $direction = null) : self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create(array $fieldlist, string $table, ? string $database = null, ? string $schema = null) : self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function push(Ulmus\Query\Fragment $queryFragment) : self
|
||||
{
|
||||
$this->queryStack[] = $queryFragment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function pull(Ulmus\Query\Fragment $queryFragment) : self
|
||||
{
|
||||
return array_shift($this->queryStack);
|
||||
}
|
||||
|
||||
public function render(bool $skipToken = false) : array
|
||||
{
|
||||
$stack = [];
|
||||
|
||||
foreach($this->queryStack as $fragment) {
|
||||
$stack = array_merge($stack, (array) $fragment->render($skipToken));
|
||||
}
|
||||
|
||||
return $stack;
|
||||
}
|
||||
|
||||
public function reset() : void
|
||||
{
|
||||
$this->parameters = $this->values = $this->queryStack = [];
|
||||
$this->whereConditionOperator = Ulmus\Query\Where::CONDITION_AND;
|
||||
$this->havingConditionOperator = Ulmus\Query\Where::CONDITION_AND;
|
||||
$this->parameterIndex = 0;
|
||||
|
||||
unset($this->where, $this->having);
|
||||
}
|
||||
|
||||
public function getFragment(string $class, int $index = 0) : ? Ulmus\Query\Fragment
|
||||
{
|
||||
foreach($this->queryStack as $item) {
|
||||
if ( get_class($item) === $class ) {
|
||||
if ( $index-- === 0 ) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function removeFragment(Ulmus\Query\Fragment $fragment) : void
|
||||
{
|
||||
foreach($this->queryStack as $key => $item) {
|
||||
if ( $item === $fragment ) {
|
||||
unset($this->queryStack[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
public function addParameter($value, string $key = null) : string
|
||||
{
|
||||
if ( $this->parent ?? false ) {
|
||||
return $this->parent->addParameter($value, $key);
|
||||
}
|
||||
|
||||
if ( $key === null ) {
|
||||
$key = ":p" . $this->parameterIndex++;
|
||||
}
|
||||
|
||||
$this->parameters[$key] = $value;
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
public function addValues(array $values) : void
|
||||
{
|
||||
$this->values = $values;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user