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

Searching: Added negation support to UI and term handling

Updated/added tests to cover.
Support for actual search queries still remains.
This commit is contained in:
Dan Brown
2024-10-03 15:59:50 +01:00
parent 177cfd72bf
commit 93c677a6a9
11 changed files with 252 additions and 96 deletions

View File

@@ -2,12 +2,14 @@
namespace BookStack\Search;
use BookStack\Search\Options\SearchOption;
class SearchOptionSet
{
/**
* @var SearchOption[]
*/
public array $options = [];
protected array $options = [];
public function __construct(array $options = [])
{
@@ -22,7 +24,8 @@ class SearchOptionSet
public function toValueMap(): array
{
$map = [];
foreach ($this->options as $key => $option) {
foreach ($this->options as $index => $option) {
$key = $option->getKey() ?? $index;
$map[$key] = $option->value;
}
return $map;
@@ -35,22 +38,32 @@ class SearchOptionSet
public function filterEmpty(): self
{
$filteredOptions = array_filter($this->options, fn (SearchOption $option) => !empty($option->value));
$filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value)));
return new self($filteredOptions);
}
public static function fromValueArray(array $values): self
/**
* @param class-string<SearchOption> $class
*/
public static function fromValueArray(array $values, string $class): self
{
$options = array_map(fn($val) => new SearchOption($val), $values);
$options = array_map(fn($val) => new $class($val), $values);
return new self($options);
}
public static function fromMapArray(array $values): self
/**
* @return SearchOption[]
*/
public function all(): array
{
$options = [];
foreach ($values as $key => $value) {
$options[$key] = new SearchOption($value);
}
return new self($options);
return $this->options;
}
/**
* @return SearchOption[]
*/
public function negated(): array
{
return array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
}
}