From 368ca1ae3f55eb8616a8c7ecb126d710eca86b5a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 19 Oct 2014 15:51:57 +0200 Subject: [PATCH] Added tag filtering by category --- src/Dao/TagDao.php | 7 +++ src/SearchServices/Filters/TagFilter.php | 1 + .../Parsers/TagSearchParser.php | 12 ++++- tests/Dao/TagDaoFilterTest.php | 49 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/Dao/TagDaoFilterTest.php diff --git a/src/Dao/TagDao.php b/src/Dao/TagDao.php index 12824575..56248260 100644 --- a/src/Dao/TagDao.php +++ b/src/Dao/TagDao.php @@ -158,6 +158,13 @@ class TagDao extends AbstractDao implements ICrudDao return; } + elseif ($requirement->getType() === TagFilter::REQUIREMENT_CATEGORY) + { + $sql = 'IFNULL(category, \'default\') = ?'; + $requirement->setType($sql); + return parent::decorateQueryFromRequirement($query, $requirement); + } + parent::decorateQueryFromRequirement($query, $requirement); } diff --git a/src/SearchServices/Filters/TagFilter.php b/src/SearchServices/Filters/TagFilter.php index cb04d136..92b91169 100644 --- a/src/SearchServices/Filters/TagFilter.php +++ b/src/SearchServices/Filters/TagFilter.php @@ -9,6 +9,7 @@ class TagFilter extends BasicFilter implements IFilter const ORDER_USAGE_COUNT = 'usages'; const REQUIREMENT_PARTIAL_TAG_NAME = 'partialTagName'; + const REQUIREMENT_CATEGORY = 'category'; public function __construct() { diff --git a/src/SearchServices/Parsers/TagSearchParser.php b/src/SearchServices/Parsers/TagSearchParser.php index ad56d234..64d6e36c 100644 --- a/src/SearchServices/Parsers/TagSearchParser.php +++ b/src/SearchServices/Parsers/TagSearchParser.php @@ -26,7 +26,17 @@ class TagSearchParser extends AbstractSearchParser protected function decorateFilterFromNamedToken(IFilter $filter, NamedSearchToken $namedToken) { - throw new NotSupportedException(); + if ($namedToken->getKey() === 'category') + { + $this->addRequirementFromToken( + $filter, + $namedToken, + TagFilter::REQUIREMENT_CATEGORY, + self::ALLOW_COMPOSITE); + } + + else + throw new NotSupportedException(); } protected function getOrderColumn($tokenText) diff --git a/tests/Dao/TagDaoFilterTest.php b/tests/Dao/TagDaoFilterTest.php new file mode 100644 index 00000000..6d89ec61 --- /dev/null +++ b/tests/Dao/TagDaoFilterTest.php @@ -0,0 +1,49 @@ +getTestTag('test 1'); + $tag2 = $this->getTestTag('test 2'); + $tag3 = $this->getTestTag('test 3'); + $tag1->setCategory(null); + $tag2->setCategory('misc'); + $tag3->setCategory('other'); + $tagDao = $this->getTagDao(); + $tagDao->save($tag1); + $tagDao->save($tag2); + $tagDao->save($tag3); + + $searchFilter = new TagFilter(); + $requirement = new Requirement(); + $requirement->setType(TagFilter::REQUIREMENT_CATEGORY); + $requirement->setValue(new RequirementSingleValue('misc')); + $requirement->setNegated(true); + $searchFilter->addRequirement($requirement); + $result = $tagDao->findFiltered($searchFilter); + $this->assertEquals(2, $result->getTotalRecords()); + $this->assertEntitiesEqual([$tag3, $tag1], array_values($result->getEntities())); + } + + private function getTagDao() + { + return new TagDao($this->databaseConnection); + } + + private function getTestTag($name) + { + $tag = new Tag(); + $tag->setName($name); + $tag->setCreationTime(date('c')); + return $tag; + } +}