Добавление фильтра STARTS_WIDTH для регистронезависимого поиска

This commit is contained in:
Arsen Mirzaev Tatyano-Muradovich 2021-06-29 09:07:43 +10:00
parent cbc26916ea
commit b92e8792f0

View File

@ -81,6 +81,18 @@ class Query extends Component implements QueryInterface
*/ */
public string $searchType = 'START'; public string $searchType = 'START';
/**
* Поиск
*
* [свойство => его значение]
*/
public array $filter;
/**
* Тип поиска
*/
public string $filterType = 'START';
public $orderBy; public $orderBy;
public $indexBy; public $indexBy;
@ -229,13 +241,21 @@ class Query extends Component implements QueryInterface
return $this; return $this;
} }
/**
*/
public function filter(array $text, string $type = 'START'): self
{
$this->filter = $text;
$this->filterType = $type;
return $this;
}
/** /**
* Обойти коллекцию вершин по направлению * Обойти коллекцию вершин по направлению
* *
* Генерация AQL выражения * Генерация AQL выражения
* *
* @see https://www.arangodb.com/docs/3.7/aql/operations-let.html
*
* @param mixed $vertex Коллекция вершин из которой требуется обход * @param mixed $vertex Коллекция вершин из которой требуется обход
* @param string $direction Направление ('INBOUND', 'OUTBOUND', 'ANY') * @param string $direction Направление ('INBOUND', 'OUTBOUND', 'ANY')
*/ */
@ -851,6 +871,7 @@ class Query extends Component implements QueryInterface
$query->genForeach($query->foreach), $query->genForeach($query->foreach),
$query->genWhere($query->where, $params), $query->genWhere($query->where, $params),
isset($query->search) ? $query->genSearch($query->search, $query->searchType) : null, isset($query->search) ? $query->genSearch($query->search, $query->searchType) : null,
isset($query->filter) ? $query->genFilter($query->filter, $query->filterType) : null,
$query->genOrderBy($query->orderBy, $params), $query->genOrderBy($query->orderBy, $params),
$query->genLimit($query->limit, $query->offset, $params), $query->genLimit($query->limit, $query->offset, $params),
$query->genSelect($query->select, $params), $query->genSelect($query->select, $params),
@ -1115,6 +1136,24 @@ class Query extends Component implements QueryInterface
return match (strtoupper($type)) { return match (strtoupper($type)) {
'START', 'STARTS', 'STARTS_WITH' => $query . $this->filterStartsWith($expression), 'START', 'STARTS', 'STARTS_WITH' => $query . $this->filterStartsWith($expression),
'START_SENSETIVE' => $query . $this->filterStartsWith($expression, sensetive: true),
'CONTAINS', 'LIKE' => $query . $this->filterContains($expression),
default => $query . Serializer::encode($expression)
};
}
/**
* @param $collection
* @param $columns
* @return string
*/
protected function genFilter(array $expression, string $type = 'START'): string
{
$query = 'FILTER ';
return match (strtoupper($type)) {
'START', 'STARTS', 'STARTS_WITH' => $query . $this->filterStartsWith($expression),
'START_SENSETIVE' => $query . $this->filterStartsWith($expression, sensetive: true),
'CONTAINS', 'LIKE' => $query . $this->filterContains($expression), 'CONTAINS', 'LIKE' => $query . $this->filterContains($expression),
default => $query . Serializer::encode($expression) default => $query . Serializer::encode($expression)
}; };
@ -1157,8 +1196,6 @@ class Query extends Component implements QueryInterface
* *
* Генерация AQL выражения * Генерация AQL выражения
* *
* @see https://www.arangodb.com/docs/3.7/aql/operations-let.html
*
* @param string $direction Направление * @param string $direction Направление
* @param mixed $vertex Коллекция вершин из которой требуется обход * @param mixed $vertex Коллекция вершин из которой требуется обход
*/ */
@ -1303,7 +1340,7 @@ class Query extends Component implements QueryInterface
{ {
$this->foreach = match (true) { $this->foreach = match (true) {
empty($this->foreach) => [$expression], empty($this->foreach) => [$expression],
default => $this->foreach []= [$expression] default => $this->foreach[] = [$expression]
}; };
return $this; return $this;
@ -1399,20 +1436,38 @@ class Query extends Component implements QueryInterface
return $this; return $this;
} }
public function filterStartsWith(array $expression): string public function filterStartsWith(array $expression, bool $sensetive = false): string
{ {
// Генерация // Генерация
foreach ($expression as $key => $value) { foreach ($expression as $key => $value) {
if (isset($return)) { if ($sensetive) {
$return .= ' OR STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")"; if (isset($return)) {
$return .= ' OR STARTS_WITH(' . $this->filterLower($this->quoteCollectionName($this->collection) . ".$key") . ", " . $this->filterLower("\"$value\"") . ")";
} else {
$return = 'STARTS_WITH(' . $this->filterLower($this->quoteCollectionName($this->collection) . ".$key") . ", " . $this->filterLower("\"$value\"") . ")";
}
} else { } else {
$return = 'STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")"; if (isset($return)) {
$return .= ' OR STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")";
} else {
$return = 'STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")";
}
} }
} }
return $return; return $return;
} }
public function filterUpper(string $target): string
{
return "UPPER($target)";
}
public function filterLower(string $target): string
{
return "LOWER($target)";
}
public function filterContains(array $expression): string public function filterContains(array $expression): string
{ {
// Инициализация // Инициализация