Added safety: support to post searching
This commit is contained in:
parent
016e89a463
commit
97a1c52018
4 changed files with 34 additions and 5 deletions
1
TODO
1
TODO
|
@ -10,7 +10,6 @@ everything related to posts:
|
|||
- add search form (query, order and safety) to post list presenter
|
||||
- add warning if no posts were found
|
||||
- search filters
|
||||
- safety:safe/sketchy/unsafe
|
||||
- comment:rr-
|
||||
- comment_count: 3..5
|
||||
- fav:rr-
|
||||
|
|
|
@ -18,6 +18,7 @@ class PostFilter extends BasicFilter implements IFilter
|
|||
const REQUIREMENT_FAV_COUNT = 'favCount';
|
||||
const REQUIREMENT_SCORE = 'score';
|
||||
const REQUIREMENT_UPLOADER = 'uploader.name';
|
||||
const REQUIREMENT_SAFETY = 'safety';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
|
|
@ -40,11 +40,21 @@ abstract class AbstractSearchParser
|
|||
|
||||
protected abstract function getOrderColumn($token);
|
||||
|
||||
protected function createRequirementValue($text, $flags = 0)
|
||||
protected function createRequirementValue($text, $flags = 0, $valueDecorator = null)
|
||||
{
|
||||
if ($valueDecorator === null)
|
||||
{
|
||||
$valueDecorator = function($value)
|
||||
{
|
||||
return $value;
|
||||
};
|
||||
}
|
||||
|
||||
if ((($flags & self::ALLOW_RANGES) === self::ALLOW_RANGES) and substr_count($text, '..') === 1)
|
||||
{
|
||||
list ($minValue, $maxValue) = explode('..', $text);
|
||||
$minValue = $valueDecorator($minValue);
|
||||
$maxValue = $valueDecorator($maxValue);
|
||||
$tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementRangedValue();
|
||||
$tokenValue->setMinValue($minValue);
|
||||
$tokenValue->setMaxValue($maxValue);
|
||||
|
@ -53,19 +63,21 @@ abstract class AbstractSearchParser
|
|||
else if ((($flags & self::ALLOW_COMPOSITE) === self::ALLOW_COMPOSITE) and strpos($text, ',') !== false)
|
||||
{
|
||||
$values = explode(',', $text);
|
||||
$values = array_map($valueDecorator, $values);
|
||||
$tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementCompositeValue();
|
||||
$tokenValue->setValues($values);
|
||||
return $tokenValue;
|
||||
}
|
||||
|
||||
return new \Szurubooru\SearchServices\Requirements\RequirementSingleValue($text);
|
||||
$value = $valueDecorator($text);
|
||||
return new \Szurubooru\SearchServices\Requirements\RequirementSingleValue($value);
|
||||
}
|
||||
|
||||
protected function addRequirementFromToken($filter, $token, $type, $flags)
|
||||
protected function addRequirementFromToken($filter, $token, $type, $flags, $valueDecorator = null)
|
||||
{
|
||||
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
|
||||
$requirement->setType($type, $flags);
|
||||
$requirement->setValue($this->createRequirementValue($token->getValue(), $flags));
|
||||
$requirement->setValue($this->createRequirementValue($token->getValue(), $flags, $valueDecorator));
|
||||
$requirement->setNegated($token->isNegated());
|
||||
$filter->addRequirement($requirement);
|
||||
}
|
||||
|
|
|
@ -40,6 +40,9 @@ class PostSearchParser extends AbstractSearchParser
|
|||
elseif ($token->getKey() === 'uploader')
|
||||
$this->addUploaderRequirement($filter, $token);
|
||||
|
||||
elseif ($token->getKey() === 'safety')
|
||||
$this->addSafetyRequirement($filter, $token);
|
||||
|
||||
else
|
||||
throw new \BadMethodCallException('Not supported');
|
||||
}
|
||||
|
@ -150,6 +153,20 @@ class PostSearchParser extends AbstractSearchParser
|
|||
self::ALLOW_COMPOSITE);
|
||||
}
|
||||
|
||||
private function addSafetyRequirement($filter, $token)
|
||||
{
|
||||
$this->addRequirementFromToken(
|
||||
$filter,
|
||||
$token,
|
||||
\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_SAFETY,
|
||||
self::ALLOW_COMPOSITE,
|
||||
function ($value)
|
||||
{
|
||||
return \Szurubooru\Helpers\EnumHelper::postSafetyFromString($value);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function dateToTime($value)
|
||||
{
|
||||
$value = strtolower(trim($value));
|
||||
|
|
Loading…
Reference in a new issue