mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
134 lines
3.5 KiB
PHP
134 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Chill\MainBundle\Search;
|
|
|
|
class SearchApiQuery
|
|
{
|
|
private ?string $selectKey = null;
|
|
private array $selectKeyParams = [];
|
|
private ?string $jsonbMetadata = null;
|
|
private array $jsonbMetadataParams = [];
|
|
private ?string $pertinence = null;
|
|
private array $pertinenceParams = [];
|
|
private ?string $fromClause = null;
|
|
private array $fromClauseParams = [];
|
|
private array $whereClauses = [];
|
|
private array $whereClausesParams = [];
|
|
|
|
public function setSelectKey(string $selectKey, array $params = []): self
|
|
{
|
|
$this->selectKey = $selectKey;
|
|
$this->selectKeyParams = $params;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setSelectJsonbMetadata(string $jsonbMetadata, array $params = []): self
|
|
{
|
|
$this->jsonbMetadata = $jsonbMetadata;
|
|
$this->jsonbMetadataParams = $params;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setSelectPertinence(string $pertinence, array $params = []): self
|
|
{
|
|
$this->pertinence = $pertinence;
|
|
$this->pertinenceParams = $params;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setFromClause(string $fromClause, array $params = []): self
|
|
{
|
|
$this->fromClause = $fromClause;
|
|
$this->fromClauseParams = $params;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Set the where clause and replace all existing ones.
|
|
*
|
|
*/
|
|
public function setWhereClauses(string $whereClause, array $params = []): self
|
|
{
|
|
$this->whereClauses = [$whereClause];
|
|
$this->whereClausesParams = [$params];
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Add a where clause.
|
|
*
|
|
* This will add to previous where clauses with and `AND` join
|
|
*
|
|
* @param string $whereClause
|
|
* @param array $params
|
|
* @return $this
|
|
*/
|
|
public function andWhereClause(string $whereClause, array $params = []): self
|
|
{
|
|
$this->whereClauses[] = $whereClause;
|
|
$this->whereClausesParams[] = $params;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function buildQuery(bool $countOnly = false): string
|
|
{
|
|
$isMultiple = count($this->whereClauses);
|
|
$where =
|
|
($isMultiple ? '(' : '').
|
|
\implode(
|
|
($isMultiple ? ')' : '').' AND '.($isMultiple ? '(' : '')
|
|
, $this->whereClauses).
|
|
($isMultiple ? ')' : '')
|
|
;
|
|
|
|
if (!$countOnly) {
|
|
$select = \strtr("
|
|
'{key}' AS key,
|
|
{metadata} AS metadata,
|
|
{pertinence} AS pertinence
|
|
", [
|
|
'{key}' => $this->selectKey,
|
|
'{metadata}' => $this->jsonbMetadata,
|
|
'{pertinence}' => $this->pertinence,
|
|
]);
|
|
} else {
|
|
$select = "1 AS c";
|
|
}
|
|
|
|
return \strtr("SELECT
|
|
{select}
|
|
FROM {from}
|
|
WHERE {where}
|
|
", [
|
|
'{select}' => $select,
|
|
'{from}' => $this->fromClause,
|
|
'{where}' => $where,
|
|
]);
|
|
}
|
|
|
|
|
|
public function buildParameters(bool $countOnly = false): array
|
|
{
|
|
if (!$countOnly) {
|
|
return \array_merge(
|
|
$this->selectKeyParams,
|
|
$this->jsonbMetadataParams,
|
|
$this->pertinenceParams,
|
|
$this->fromClauseParams,
|
|
\array_merge([], ...$this->whereClausesParams),
|
|
);
|
|
} else {
|
|
return \array_merge(
|
|
$this->fromClauseParams,
|
|
\array_merge([], ...$this->whereClausesParams),
|
|
);
|
|
}
|
|
}
|
|
}
|