From 97a1c520181da562e6c325bfa9ffd039628fa14e Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 30 Sep 2014 22:42:02 +0200 Subject: [PATCH] Added safety: support to post searching --- TODO | 1 - src/SearchServices/Filters/PostFilter.php | 1 + .../Parsers/AbstractSearchParser.php | 20 +++++++++++++++---- .../Parsers/PostSearchParser.php | 17 ++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 51095c91..2dcfb7d2 100644 --- a/TODO +++ b/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- diff --git a/src/SearchServices/Filters/PostFilter.php b/src/SearchServices/Filters/PostFilter.php index 7d6a4506..167fdc68 100644 --- a/src/SearchServices/Filters/PostFilter.php +++ b/src/SearchServices/Filters/PostFilter.php @@ -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() { diff --git a/src/SearchServices/Parsers/AbstractSearchParser.php b/src/SearchServices/Parsers/AbstractSearchParser.php index 724bb9d1..be81a8ee 100644 --- a/src/SearchServices/Parsers/AbstractSearchParser.php +++ b/src/SearchServices/Parsers/AbstractSearchParser.php @@ -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); } diff --git a/src/SearchServices/Parsers/PostSearchParser.php b/src/SearchServices/Parsers/PostSearchParser.php index 1275da86..ee8d31b1 100644 --- a/src/SearchServices/Parsers/PostSearchParser.php +++ b/src/SearchServices/Parsers/PostSearchParser.php @@ -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));