mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-01 04:23:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,20 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\MainBundle\Search;
|
||||
|
||||
use function array_push;
|
||||
use function implode;
|
||||
use function strtr;
|
||||
|
||||
class SearchApiQuery
|
||||
{
|
||||
private array $select = [];
|
||||
private array $selectParams = [];
|
||||
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 ?string $jsonbMetadata = null;
|
||||
|
||||
private array $jsonbMetadataParams = [];
|
||||
|
||||
private ?string $pertinence = null;
|
||||
|
||||
private array $pertinenceParams = [];
|
||||
|
||||
private array $select = [];
|
||||
|
||||
private ?string $selectKey = null;
|
||||
|
||||
private array $selectKeyParams = [];
|
||||
|
||||
private array $selectParams = [];
|
||||
|
||||
private array $whereClauses = [];
|
||||
|
||||
private array $whereClausesParams = [];
|
||||
|
||||
public function addSelectClause(string $select, array $params = []): self
|
||||
@@ -25,6 +47,81 @@ class SearchApiQuery
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a where clause.
|
||||
*
|
||||
* This will add to previous where clauses with and `AND` join
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function andWhereClause(string $whereClause, array $params = []): self
|
||||
{
|
||||
$this->whereClauses[] = $whereClause;
|
||||
array_push($this->whereClausesParams, ...$params);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function buildParameters(bool $countOnly = false): array
|
||||
{
|
||||
if (!$countOnly) {
|
||||
return [
|
||||
...$this->buildSelectParams($countOnly),
|
||||
...$this->fromClauseParams,
|
||||
...$this->whereClausesParams,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
...$this->fromClauseParams,
|
||||
...$this->whereClausesParams,
|
||||
];
|
||||
}
|
||||
|
||||
public function buildQuery(bool $countOnly = false): string
|
||||
{
|
||||
$isMultiple = count($this->whereClauses);
|
||||
$where =
|
||||
($isMultiple ? '(' : '') .
|
||||
implode(
|
||||
($isMultiple ? ')' : '') . ' AND ' . ($isMultiple ? '(' : ''),
|
||||
$this->whereClauses
|
||||
) .
|
||||
($isMultiple ? ')' : '');
|
||||
|
||||
$select = $this->buildSelectClause($countOnly);
|
||||
|
||||
return strtr('SELECT
|
||||
{select}
|
||||
FROM {from}
|
||||
WHERE {where}
|
||||
', [
|
||||
'{select}' => $select,
|
||||
'{from}' => $this->fromClause,
|
||||
'{where}' => $where,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getFromClause(): string
|
||||
{
|
||||
return $this->fromClause;
|
||||
}
|
||||
|
||||
public function getFromParams(): array
|
||||
{
|
||||
return $this->fromClauseParams;
|
||||
}
|
||||
|
||||
public function getSelectClauses(): array
|
||||
{
|
||||
return $this->select;
|
||||
}
|
||||
|
||||
public function getSelectParams(): array
|
||||
{
|
||||
return $this->selectParams;
|
||||
}
|
||||
|
||||
public function resetSelectClause(): self
|
||||
{
|
||||
$this->select = [];
|
||||
@@ -39,20 +136,10 @@ class SearchApiQuery
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSelectClauses(): array
|
||||
public function setFromClause(string $fromClause, array $params = []): self
|
||||
{
|
||||
return $this->select;
|
||||
}
|
||||
|
||||
public function getSelectParams(): array
|
||||
{
|
||||
return $this->selectParams;
|
||||
}
|
||||
|
||||
public function setSelectKey(string $selectKey, array $params = []): self
|
||||
{
|
||||
$this->selectKey = $selectKey;
|
||||
$this->selectKeyParams = $params;
|
||||
$this->fromClause = $fromClause;
|
||||
$this->fromClauseParams = $params;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -65,6 +152,14 @@ class SearchApiQuery
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSelectKey(string $selectKey, array $params = []): self
|
||||
{
|
||||
$this->selectKey = $selectKey;
|
||||
$this->selectKeyParams = $params;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSelectPertinence(string $pertinence, array $params = []): self
|
||||
{
|
||||
$this->pertinence = $pertinence;
|
||||
@@ -73,27 +168,8 @@ class SearchApiQuery
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setFromClause(string $fromClause, array $params = []): self
|
||||
{
|
||||
$this->fromClause = $fromClause;
|
||||
$this->fromClauseParams = $params;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFromClause(): string
|
||||
{
|
||||
return $this->fromClause;
|
||||
}
|
||||
|
||||
public function getFromParams(): array
|
||||
{
|
||||
return $this->fromClauseParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the where clause and replace all existing ones.
|
||||
*
|
||||
*/
|
||||
public function setWhereClauses(string $whereClause, array $params = []): self
|
||||
{
|
||||
@@ -103,21 +179,27 @@ class SearchApiQuery
|
||||
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
|
||||
private function buildSelectClause(bool $countOnly = false): string
|
||||
{
|
||||
$this->whereClauses[] = $whereClause;
|
||||
\array_push($this->whereClausesParams, ...$params);
|
||||
if ($countOnly) {
|
||||
return 'count(*) AS c';
|
||||
}
|
||||
|
||||
return $this;
|
||||
$selects = $this->getSelectClauses();
|
||||
|
||||
if (null !== $this->selectKey) {
|
||||
$selects[] = strtr("'{key}' AS key", ['{key}' => $this->selectKey]);
|
||||
}
|
||||
|
||||
if (null !== $this->jsonbMetadata) {
|
||||
$selects[] = strtr('{metadata} AS metadata', ['{metadata}' => $this->jsonbMetadata]);
|
||||
}
|
||||
|
||||
if (null !== $this->pertinence) {
|
||||
$selects[] = strtr('{pertinence} AS pertinence', ['{pertinence}' => $this->pertinence]);
|
||||
}
|
||||
|
||||
return implode(', ', $selects);
|
||||
}
|
||||
|
||||
private function buildSelectParams(bool $count = false): array
|
||||
@@ -131,76 +213,15 @@ class SearchApiQuery
|
||||
if (null !== $this->selectKey) {
|
||||
$args = [...$args, ...$this->selectKeyParams];
|
||||
}
|
||||
|
||||
if (null !== $this->jsonbMetadata) {
|
||||
$args = [...$args, ...$this->jsonbMetadataParams];
|
||||
}
|
||||
|
||||
if (null !== $this->pertinence) {
|
||||
$args = [...$args, ...$this->pertinenceParams];
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
private function buildSelectClause(bool $countOnly = false): string
|
||||
{
|
||||
if ($countOnly) {
|
||||
return 'count(*) AS c';
|
||||
}
|
||||
|
||||
$selects = $this->getSelectClauses();
|
||||
|
||||
if (null !== $this->selectKey) {
|
||||
$selects[] = \strtr("'{key}' AS key", [ '{key}' => $this->selectKey ]);
|
||||
}
|
||||
if (null !== $this->jsonbMetadata) {
|
||||
$selects[] = \strtr('{metadata} AS metadata', [ '{metadata}' => $this->jsonbMetadata]);
|
||||
}
|
||||
if (null !== $this->pertinence) {
|
||||
$selects[] = \strtr('{pertinence} AS pertinence', [ '{pertinence}' => $this->pertinence]);
|
||||
}
|
||||
|
||||
return \implode(', ', $selects);
|
||||
}
|
||||
|
||||
public function buildQuery(bool $countOnly = false): string
|
||||
{
|
||||
$isMultiple = count($this->whereClauses);
|
||||
$where =
|
||||
($isMultiple ? '(' : '').
|
||||
\implode(
|
||||
($isMultiple ? ')' : '').' AND '.($isMultiple ? '(' : '')
|
||||
, $this->whereClauses).
|
||||
($isMultiple ? ')' : '')
|
||||
;
|
||||
|
||||
$select = $this->buildSelectClause($countOnly);
|
||||
|
||||
|
||||
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 [
|
||||
...$this->buildSelectParams($countOnly),
|
||||
...$this->fromClauseParams,
|
||||
...$this->whereClausesParams,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
...$this->fromClauseParams,
|
||||
...$this->whereClausesParams,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user