From 11931075f5dd700ba4c3680fc18fea9dd0e9ce5d Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 30 Sep 2014 15:29:04 +0200 Subject: [PATCH] Added score: support to post searching --- TODO | 1 - src/SearchServices/Filters/PostFilter.php | 1 + .../Parsers/AbstractSearchParser.php | 9 +++ .../Parsers/PostSearchParser.php | 60 ++++++++++++------- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index 379047a2..945e630b 100644 --- a/TODO +++ b/TODO @@ -15,7 +15,6 @@ everything related to posts: - comment:rr- - comment_count: 3..5 - fav:rr- - - score:3..5 - file_size:3K..5M - image_size:huge/large/medium/small - type:img/flash/yt/video diff --git a/src/SearchServices/Filters/PostFilter.php b/src/SearchServices/Filters/PostFilter.php index d7f8f1cb..ed85ec84 100644 --- a/src/SearchServices/Filters/PostFilter.php +++ b/src/SearchServices/Filters/PostFilter.php @@ -16,6 +16,7 @@ class PostFilter extends BasicFilter implements IFilter const REQUIREMENT_HASH = 'name'; const REQUIREMENT_TAG_COUNT = 'tagCount'; const REQUIREMENT_FAV_COUNT = 'favCount'; + const REQUIREMENT_SCORE = 'score'; public function __construct() { diff --git a/src/SearchServices/Parsers/AbstractSearchParser.php b/src/SearchServices/Parsers/AbstractSearchParser.php index 4ee0846a..724bb9d1 100644 --- a/src/SearchServices/Parsers/AbstractSearchParser.php +++ b/src/SearchServices/Parsers/AbstractSearchParser.php @@ -61,6 +61,15 @@ abstract class AbstractSearchParser return new \Szurubooru\SearchServices\Requirements\RequirementSingleValue($text); } + protected function addRequirementFromToken($filter, $token, $type, $flags) + { + $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); + $requirement->setType($type, $flags); + $requirement->setValue($this->createRequirementValue($token->getValue(), $flags)); + $requirement->setNegated($token->isNegated()); + $filter->addRequirement($requirement); + } + private function getOrder($query) { $order = []; diff --git a/src/SearchServices/Parsers/PostSearchParser.php b/src/SearchServices/Parsers/PostSearchParser.php index f1812eac..3e0347e3 100644 --- a/src/SearchServices/Parsers/PostSearchParser.php +++ b/src/SearchServices/Parsers/PostSearchParser.php @@ -34,6 +34,9 @@ class PostSearchParser extends AbstractSearchParser elseif ($token->getKey() === 'fav_count') $this->addFavCountRequirement($filter, $token); + elseif ($token->getKey() === 'score') + $this->addScoreRequirement($filter, $token); + else throw new \BadMethodCallException('Not supported'); } @@ -63,19 +66,20 @@ class PostSearchParser extends AbstractSearchParser private function addIdRequirement($filter, $token) { - $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); - $requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_ID); - $requirement->setValue($this->createRequirementValue($token->getValue(), self::ALLOW_COMPOSITE | self::ALLOW_RANGES)); - $requirement->setNegated($token->isNegated()); - $filter->addRequirement($requirement); + $this->addRequirementFromToken( + $filter, + $token, + \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_ID, + self::ALLOW_COMPOSITE | self::ALLOW_RANGES); } private function addHashRequirement($filter, $token) { - $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); - $requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_HASH); - $requirement->setValue($this->createRequirementValue($token->getValue(), self::ALLOW_COMPOSITE)); - $filter->addRequirement($requirement); + $this->addRequirementFromToken( + $filter, + $token, + \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_HASH, + self::ALLOW_COMPOSITE); } private function addDateRequirement($filter, $token) @@ -99,27 +103,39 @@ class PostSearchParser extends AbstractSearchParser if ($timeMax) $finalString .= date('c', $timeMax); - $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); - $requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_DATE); - $requirement->setValue($this->createRequirementValue($finalString, self::ALLOW_RANGES)); - $requirement->setNegated($token->isNegated()); - $filter->addRequirement($requirement); + $token->setValue($finalString); + $this->addRequirementFromToken( + $filter, + $token, + \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_DATE, + self::ALLOW_RANGES); } private function addTagCountRequirement($filter, $token) { - $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); - $requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_TAG_COUNT); - $requirement->setValue($this->createRequirementValue($token->getValue(), self::ALLOW_COMPOSITE | self::ALLOW_RANGES)); - $filter->addRequirement($requirement); + $this->addRequirementFromToken( + $filter, + $token, + \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_TAG_COUNT, + self::ALLOW_COMPOSITE | self::ALLOW_RANGES); } private function addFavCountRequirement($filter, $token) { - $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); - $requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_FAV_COUNT); - $requirement->setValue($this->createRequirementValue($token->getValue(), self::ALLOW_COMPOSITE | self::ALLOW_RANGES)); - $filter->addRequirement($requirement); + $this->addRequirementFromToken( + $filter, + $token, + \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_FAV_COUNT, + self::ALLOW_COMPOSITE | self::ALLOW_RANGES); + } + + private function addScoreRequirement($filter, $token) + { + $this->addRequirementFromToken( + $filter, + $token, + \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_SCORE, + self::ALLOW_COMPOSITE | self::ALLOW_RANGES); } private function dateToTime($value)