169 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\GenericDoc;
use Nelmio\Alice\Throwable\Exception\FixtureBuilder\Denormalizer\UnexpectedValueException;
class FetchQuery implements FetchQueryInterface
{
/**
* @var list<string>
*/
private array $joins = [];
/**
* @var list<mixed>
*/
private array $joinParams = [];
/**
* @var list<string>
*/
private array $wheres = [];
/**
* @var list<mixed>
*/
private array $whereParams = [];
public function __construct(
private readonly string $selectKeyString,
private readonly string $selectIdentifierJsonB,
private readonly string $selectDate,
private string $from = '',
private array $selectIdentifierParams = [],
private array $selectDateParams = [],
) {
}
public function addJoinClause(string $sql, array $params = []): int
{
$this->joins[] = $sql;
$this->joinParams[] = $params;
return count($this->joins) - 1;
}
public function addWhereClause(string $sql, array $params = []): int
{
$this->wheres[] = $sql;
$this->whereParams[] = $params;
return count($this->wheres) - 1;
}
public function removeWhereClause(int $index): void
{
if (!array_key_exists($index, $this->wheres)) {
throw new \UnexpectedValueException("this index does not exists");
}
unset($this->wheres[$index], $this->whereParams[$index]);
}
public function removeJoinClause(int $index): void
{
if (!array_key_exists($index, $this->joins)) {
throw new \UnexpectedValueException("this index does not exists");
}
unset($this->joins[$index], $this->joinParams[$index]);
}
public function getSelectKeyString(): string
{
return $this->selectKeyString;
}
public function getSelectIdentifierJsonB(): string
{
return $this->selectIdentifierJsonB;
}
/**
* @inheritDoc
*/
public function getSelectIdentifierParams(): array
{
return $this->selectIdentifierParams;
}
public function getSelectDate(): string
{
return $this->selectDate;
}
/**
* @inheritDoc
*/
public function getSelectDateParams(): array
{
return $this->selectDateParams;
}
public function getFromQuery(): string
{
return $this->from . " " . implode(' ', $this->joins);
}
/**
* @inheritDoc
*/
public function getFromQueryParams(): array
{
$result = [];
foreach ($this->joinParams as $params) {
$result = [...$result, ...$params];
}
return $result;
}
public function getWhereQuery(): string
{
return implode(' AND ', $this->wheres);
}
/**
* @inheritDoc
*/
public function getWhereQueryParams(): array
{
$result = [];
foreach ($this->whereParams as $params) {
$result = [...$result, ...$params];
}
return $result;
}
/**
* @param array $selectIdentifierParams
*/
public function setSelectIdentifierParams(array $selectIdentifierParams): void
{
$this->selectIdentifierParams = $selectIdentifierParams;
}
/**
* @param array $selectDateParams
*/
public function setSelectDateParams(array $selectDateParams): void
{
$this->selectDateParams = $selectDateParams;
}
}