Query/src/State.php

236 lines
4.8 KiB
PHP
Raw Permalink Normal View History

<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
*
2018-02-09 16:14:40 -05:00
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
2018-02-09 16:14:40 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
2023-03-17 16:34:21 -04:00
* @version 4.1.0
*/
2023-03-17 15:18:33 -04:00
namespace Query;
use function is_array;
/**
* Query builder state
*
* @method getFromString(): string
* @method getGroupArray(): array
2023-03-17 15:18:33 -04:00
* @method getGroupString(): string
* @method getHavingMap(): array
* @method getLimit(): int|null
* @method getOffset()
2023-03-17 15:18:33 -04:00
* @method getOrderArray(): array
* @method getOrderString(): string
* @method getQueryMap(): array
2023-03-17 15:18:33 -04:00
* @method getSelectString(): string
* @method getSetArrayKeys(): array
* @method getSetString(): string
* @method getValues(): array
* @method getWhereValues(): array
*
* @method setFromString(string $fromString): self
* @method setGroupArray(array $array): self
2023-03-17 15:18:33 -04:00
* @method setGroupString(string $groupString): self
* @method setLimit(int $limit): self
* @method setOffset(?int $offset): self
2023-03-17 15:18:33 -04:00
* @method setOrderString(string $orderString): self
* @method setSelectString(string $selectString): self
* @method setSetArrayKeys(array $arrayKeys): self
* @method setSetString(string $setString): self
*/
2023-03-17 15:18:33 -04:00
class State
{
// --------------------------------------------------------------------------
// ! SQL Clause Strings
// --------------------------------------------------------------------------
/**
* Compiled 'select' clause
*/
protected string $selectString = '';
/**
* Compiled 'from' clause
*/
protected string $fromString = '';
/**
* Compiled arguments for insert / update
*/
protected string $setString = '';
/**
* Order by clause
*/
protected string $orderString = '';
/**
* Group by clause
*/
protected string $groupString = '';
// --------------------------------------------------------------------------
// ! SQL Clause Arrays
// --------------------------------------------------------------------------
/**
* Keys for insert/update statement
*/
protected array $setArrayKeys = [];
/**
* Key/val pairs for order by clause
*/
protected array $orderArray = [];
/**
* Key/val pairs for group by clause
*/
protected array $groupArray = [];
// --------------------------------------------------------------------------
// ! Other Class vars
// --------------------------------------------------------------------------
/**
* Values to apply to prepared statements
*/
protected array $values = [];
/**
* Values to apply to where clauses in prepared statements
*/
protected array $whereValues = [];
/**
* Value for limit string
*/
protected ?int $limit = NULL;
/**
* Value for offset in limit string
*/
protected ?int $offset = NULL;
/**
* Query component order mapping
* for complex select queries
*
* Format:
2018-01-26 08:39:30 -05:00
* [
* 'type' => 'where',
* 'conjunction' => ' AND ',
* 'string' => 'k=?'
2018-01-26 08:39:30 -05:00
* ]
*/
protected array $queryMap = [];
/**
* Map for having clause
*/
protected array $havingMap = [];
public function __call(string $name, array $arguments)
{
if (str_starts_with($name, 'get'))
{
$maybeProp = lcfirst(substr($name, 3));
if (isset($this->$maybeProp))
{
return $this->$maybeProp;
}
}
if (str_starts_with($name, 'set'))
{
$maybeProp = lcfirst(substr($name, 3));
if (isset($this->$maybeProp))
{
$this->$maybeProp = $arguments[0];
2023-03-17 15:18:33 -04:00
return $this;
}
}
return NULL;
}
public function appendSelectString(string $str): self
{
$this->selectString .= $str;
2023-03-17 15:18:33 -04:00
return $this;
}
2018-01-24 13:14:03 -05:00
public function appendSetArrayKeys(array $setArrayKeys): self
{
$this->setArrayKeys = array_merge($this->setArrayKeys, $setArrayKeys);
2023-03-17 15:18:33 -04:00
return $this;
}
2023-01-13 13:14:28 -05:00
public function setOrderArray(string $key, mixed $orderArray): self
{
$this->orderArray[$key] = $orderArray;
2023-03-17 15:18:33 -04:00
return $this;
}
2018-01-24 13:14:03 -05:00
public function appendGroupArray(string $groupArray): self
{
$this->groupArray[] = $groupArray;
2023-03-17 15:18:33 -04:00
return $this;
}
2018-01-24 13:14:03 -05:00
public function appendValues(array $values): self
{
$this->values = array_merge($this->values, $values);
2023-03-17 15:18:33 -04:00
return $this;
}
2023-01-13 13:14:28 -05:00
public function appendWhereValues(mixed $val): self
{
if (is_array($val))
{
2023-03-17 15:18:33 -04:00
foreach ($val as $v)
{
$this->whereValues[] = $v;
}
return $this;
}
$this->whereValues[] = $val;
2023-03-17 15:18:33 -04:00
return $this;
}
/**
* Add an additional set of mapping pairs to a internal map
*/
2023-01-13 13:14:28 -05:00
public function appendMap(string $conjunction = '', string $string = '', MapType $type = MapType::WHERE): self
{
$this->queryMap[] = [
'type' => $type,
'conjunction' => $conjunction,
2023-03-17 15:18:33 -04:00
'string' => $string,
];
2023-03-17 15:18:33 -04:00
return $this;
}
2018-01-24 13:14:03 -05:00
public function appendHavingMap(array $item): self
{
$this->havingMap[] = $item;
2023-03-17 15:18:33 -04:00
return $this;
}
}