1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-09 10:22:51 +03:00

Search: Added exact/filter/tag term negation support

This commit is contained in:
Dan Brown
2024-10-03 19:27:03 +01:00
parent 93c677a6a9
commit cd84d08157
5 changed files with 154 additions and 116 deletions

View File

@@ -4,8 +4,34 @@ namespace BookStack\Search\Options;
class TagSearchOption extends SearchOption
{
/**
* Acceptable operators to be used within a tag search option.
*
* @var string[]
*/
protected array $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
public function toString(): string
{
return ($this->negated ? '-' : '') . "[{$this->value}]";
}
/**
* @return array{name: string, operator: string, value: string}
*/
public function getParts(): array
{
$operatorRegex = implode('|', array_map(fn($op) => preg_quote($op), $this->queryOperators));
preg_match('/^(.*?)((' . $operatorRegex . ')(.*?))?$/', $this->value, $tagSplit);
$extractedOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
$tagOperator = in_array($extractedOperator, $this->queryOperators) ? $extractedOperator : '=';
$tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
return [
'name' => $tagSplit[1],
'operator' => $tagOperator,
'value' => $tagValue,
];
}
}