Added safety: support to post searching

This commit is contained in:
Marcin Kurczewski 2014-09-30 22:42:02 +02:00
parent 016e89a463
commit 97a1c52018
4 changed files with 34 additions and 5 deletions

1
TODO
View file

@ -10,7 +10,6 @@ everything related to posts:
- add search form (query, order and safety) to post list presenter - add search form (query, order and safety) to post list presenter
- add warning if no posts were found - add warning if no posts were found
- search filters - search filters
- safety:safe/sketchy/unsafe
- comment:rr- - comment:rr-
- comment_count: 3..5 - comment_count: 3..5
- fav:rr- - fav:rr-

View file

@ -18,6 +18,7 @@ class PostFilter extends BasicFilter implements IFilter
const REQUIREMENT_FAV_COUNT = 'favCount'; const REQUIREMENT_FAV_COUNT = 'favCount';
const REQUIREMENT_SCORE = 'score'; const REQUIREMENT_SCORE = 'score';
const REQUIREMENT_UPLOADER = 'uploader.name'; const REQUIREMENT_UPLOADER = 'uploader.name';
const REQUIREMENT_SAFETY = 'safety';
public function __construct() public function __construct()
{ {

View file

@ -40,11 +40,21 @@ abstract class AbstractSearchParser
protected abstract function getOrderColumn($token); 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) if ((($flags & self::ALLOW_RANGES) === self::ALLOW_RANGES) and substr_count($text, '..') === 1)
{ {
list ($minValue, $maxValue) = explode('..', $text); list ($minValue, $maxValue) = explode('..', $text);
$minValue = $valueDecorator($minValue);
$maxValue = $valueDecorator($maxValue);
$tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementRangedValue(); $tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementRangedValue();
$tokenValue->setMinValue($minValue); $tokenValue->setMinValue($minValue);
$tokenValue->setMaxValue($maxValue); $tokenValue->setMaxValue($maxValue);
@ -53,19 +63,21 @@ abstract class AbstractSearchParser
else if ((($flags & self::ALLOW_COMPOSITE) === self::ALLOW_COMPOSITE) and strpos($text, ',') !== false) else if ((($flags & self::ALLOW_COMPOSITE) === self::ALLOW_COMPOSITE) and strpos($text, ',') !== false)
{ {
$values = explode(',', $text); $values = explode(',', $text);
$values = array_map($valueDecorator, $values);
$tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementCompositeValue(); $tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementCompositeValue();
$tokenValue->setValues($values); $tokenValue->setValues($values);
return $tokenValue; 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 = new \Szurubooru\SearchServices\Requirements\Requirement();
$requirement->setType($type, $flags); $requirement->setType($type, $flags);
$requirement->setValue($this->createRequirementValue($token->getValue(), $flags)); $requirement->setValue($this->createRequirementValue($token->getValue(), $flags, $valueDecorator));
$requirement->setNegated($token->isNegated()); $requirement->setNegated($token->isNegated());
$filter->addRequirement($requirement); $filter->addRequirement($requirement);
} }

View file

@ -40,6 +40,9 @@ class PostSearchParser extends AbstractSearchParser
elseif ($token->getKey() === 'uploader') elseif ($token->getKey() === 'uploader')
$this->addUploaderRequirement($filter, $token); $this->addUploaderRequirement($filter, $token);
elseif ($token->getKey() === 'safety')
$this->addSafetyRequirement($filter, $token);
else else
throw new \BadMethodCallException('Not supported'); throw new \BadMethodCallException('Not supported');
} }
@ -150,6 +153,20 @@ class PostSearchParser extends AbstractSearchParser
self::ALLOW_COMPOSITE); 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) private function dateToTime($value)
{ {
$value = strtolower(trim($value)); $value = strtolower(trim($value));