mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-29 12:16:04 +08:00
93c677a6a9
Updated/added tests to cover. Support for actual search queries still remains.
70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Search;
|
|
|
|
use BookStack\Search\Options\SearchOption;
|
|
|
|
class SearchOptionSet
|
|
{
|
|
/**
|
|
* @var SearchOption[]
|
|
*/
|
|
protected array $options = [];
|
|
|
|
public function __construct(array $options = [])
|
|
{
|
|
$this->options = $options;
|
|
}
|
|
|
|
public function toValueArray(): array
|
|
{
|
|
return array_map(fn(SearchOption $option) => $option->value, $this->options);
|
|
}
|
|
|
|
public function toValueMap(): array
|
|
{
|
|
$map = [];
|
|
foreach ($this->options as $index => $option) {
|
|
$key = $option->getKey() ?? $index;
|
|
$map[$key] = $option->value;
|
|
}
|
|
return $map;
|
|
}
|
|
|
|
public function merge(SearchOptionSet $set): self
|
|
{
|
|
return new self(array_merge($this->options, $set->options));
|
|
}
|
|
|
|
public function filterEmpty(): self
|
|
{
|
|
$filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value)));
|
|
return new self($filteredOptions);
|
|
}
|
|
|
|
/**
|
|
* @param class-string<SearchOption> $class
|
|
*/
|
|
public static function fromValueArray(array $values, string $class): self
|
|
{
|
|
$options = array_map(fn($val) => new $class($val), $values);
|
|
return new self($options);
|
|
}
|
|
|
|
/**
|
|
* @return SearchOption[]
|
|
*/
|
|
public function all(): array
|
|
{
|
|
return $this->options;
|
|
}
|
|
|
|
/**
|
|
* @return SearchOption[]
|
|
*/
|
|
public function negated(): array
|
|
{
|
|
return array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
|
|
}
|
|
}
|