Skip to content

Commit cd84d08

Browse files
committed
Search: Added exact/filter/tag term negation support
1 parent 93c677a commit cd84d08

File tree

5 files changed

+153
-115
lines changed

5 files changed

+153
-115
lines changed

app/Search/Options/TagSearchOption.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,34 @@
44

55
class TagSearchOption extends SearchOption
66
{
7+
/**
8+
* Acceptable operators to be used within a tag search option.
9+
*
10+
* @var string[]
11+
*/
12+
protected array $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
13+
714
public function toString(): string
815
{
916
return ($this->negated ? '-' : '') . "[{$this->value}]";
1017
}
18+
19+
/**
20+
* @return array{name: string, operator: string, value: string}
21+
*/
22+
public function getParts(): array
23+
{
24+
$operatorRegex = implode('|', array_map(fn($op) => preg_quote($op), $this->queryOperators));
25+
preg_match('/^(.*?)((' . $operatorRegex . ')(.*?))?$/', $this->value, $tagSplit);
26+
27+
$extractedOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
28+
$tagOperator = in_array($extractedOperator, $this->queryOperators) ? $extractedOperator : '=';
29+
$tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
30+
31+
return [
32+
'name' => $tagSplit[1],
33+
'operator' => $tagOperator,
34+
'value' => $tagValue,
35+
];
36+
}
1137
}

app/Search/SearchOptionSet.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use BookStack\Search\Options\SearchOption;
66

7+
/**
8+
* @template T of SearchOption
9+
*/
710
class SearchOptionSet
811
{
912
/**
10-
* @var SearchOption[]
13+
* @var T[]
1114
*/
1215
protected array $options = [];
1316

@@ -52,15 +55,15 @@ public static function fromValueArray(array $values, string $class): self
5255
}
5356

5457
/**
55-
* @return SearchOption[]
58+
* @return T[]
5659
*/
5760
public function all(): array
5861
{
5962
return $this->options;
6063
}
6164

6265
/**
63-
* @return SearchOption[]
66+
* @return T[]
6467
*/
6568
public function negated(): array
6669
{

app/Search/SearchOptions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
class SearchOptions
1313
{
14+
/** @var SearchOptionSet<TermSearchOption> */
1415
public SearchOptionSet $searches;
16+
/** @var SearchOptionSet<ExactSearchOption> */
1517
public SearchOptionSet $exacts;
18+
/** @var SearchOptionSet<TagSearchOption> */
1619
public SearchOptionSet $tags;
20+
/** @var SearchOptionSet<FilterSearchOption> */
1721
public SearchOptionSet $filters;
1822

1923
public function __construct()

0 commit comments

Comments
 (0)