From 6b933132a577e3438acb31972f503815f198764c Mon Sep 17 00:00:00 2001 From: rr- Date: Tue, 4 Aug 2015 19:47:18 +0200 Subject: [PATCH] Added helpful messages for invalid search orders --- src/Search/Parsers/AbstractSearchParser.php | 19 +++++++- src/Search/Parsers/PostSearchParser.php | 54 ++++++--------------- src/Search/Parsers/SnapshotSearchParser.php | 2 +- src/Search/Parsers/TagSearchParser.php | 22 +++------ src/Search/Parsers/UserSearchParser.php | 14 +++--- 5 files changed, 49 insertions(+), 62 deletions(-) diff --git a/src/Search/Parsers/AbstractSearchParser.php b/src/Search/Parsers/AbstractSearchParser.php index 79c8fccb..b5b64108 100644 --- a/src/Search/Parsers/AbstractSearchParser.php +++ b/src/Search/Parsers/AbstractSearchParser.php @@ -1,6 +1,7 @@ addRequirement($requirement); } + private function getOrderColumn($tokenText) + { + $map = $this->getOrderColumnMap(); + + foreach ($map as $item) + { + list ($aliases, $value) = $item; + if ($this->matches($tokenText, $aliases)) + return $value; + } + + throw new NotSupportedException('Unknown order term: ' . $tokenText + . '. Possible order terms: ' + . join(', ', array_map(function($term) { return join('/', $term[0]); }, $map))); + } + private function getOrder($query, $negated) { $order = []; diff --git a/src/Search/Parsers/PostSearchParser.php b/src/Search/Parsers/PostSearchParser.php index 95544814..e200e4be 100644 --- a/src/Search/Parsers/PostSearchParser.php +++ b/src/Search/Parsers/PostSearchParser.php @@ -113,45 +113,23 @@ class PostSearchParser extends AbstractSearchParser . join(', ', array_map(function($term) { return join('/', $term[0]); }, $map))); } - protected function getOrderColumn($tokenText) + protected function getOrderColumnMap() { - if ($this->matches($tokenText, ['random'])) - return PostFilter::ORDER_RANDOM; - - if ($this->matches($tokenText, ['id'])) - return PostFilter::ORDER_ID; - - if ($this->matches($tokenText, ['time', 'date'])) - return PostFilter::ORDER_LAST_EDIT_TIME; - - if ($this->matches($tokenText, ['score'])) - return PostFilter::ORDER_SCORE; - - if ($this->matches($tokenText, ['file_size'])) - return PostFilter::ORDER_FILE_SIZE; - - if ($this->matches($tokenText, ['tag_count', 'tags', 'tag'])) - return PostFilter::ORDER_TAG_COUNT; - - if ($this->matches($tokenText, ['fav_count', 'fags', 'fav'])) - return PostFilter::ORDER_FAV_COUNT; - - if ($this->matches($tokenText, ['comment_count', 'comments', 'comment'])) - return PostFilter::ORDER_COMMENT_COUNT; - - if ($this->matches($tokenText, ['note_count', 'notes', 'note'])) - return PostFilter::ORDER_NOTE_COUNT; - - if ($this->matches($tokenText, ['fav_time', 'fav_date'])) - return PostFilter::ORDER_LAST_FAV_TIME; - - if ($this->matches($tokenText, ['comment_time', 'comment_date'])) - return PostFilter::ORDER_LAST_COMMENT_TIME; - - if ($this->matches($tokenText, ['feature_time', 'feature_date', 'featured', 'feature'])) - return PostFilter::ORDER_LAST_FEATURE_TIME; - - throw new NotSupportedException(); + return + [ + [['id'], PostFilter::ORDER_ID], + [['random'], PostFilter::ORDER_RANDOM], + [['time', 'date'], PostFilter::ORDER_LAST_EDIT_TIME], + [['score'], PostFilter::ORDER_SCORE], + [['file_size'], PostFilter::ORDER_FILE_SIZE], + [['tag_count', 'tags', 'tag'], PostFilter::ORDER_TAG_COUNT], + [['fav_count', 'fags', 'fav'], PostFilter::ORDER_FAV_COUNT], + [['comment_count', 'comments', 'comment'], PostFilter::ORDER_COMMENT_COUNT], + [['note_count', 'notes', 'note'], PostFilter::ORDER_NOTE_COUNT], + [['fav_time', 'fav_date'], PostFilter::ORDER_LAST_FAV_TIME], + [['comment_time', 'comment_date'], PostFilter::ORDER_LAST_COMMENT_TIME], + [['feature_time', 'feature_date', 'featured', 'feature'], PostFilter::ORDER_LAST_FEATURE_TIME], + ]; } private function addOwnLikedRequirement($filter, $token) diff --git a/src/Search/Parsers/SnapshotSearchParser.php b/src/Search/Parsers/SnapshotSearchParser.php index caa76532..9d63a0fb 100644 --- a/src/Search/Parsers/SnapshotSearchParser.php +++ b/src/Search/Parsers/SnapshotSearchParser.php @@ -41,7 +41,7 @@ class SnapshotSearchParser extends AbstractSearchParser throw new NotSupportedException(); } - protected function getOrderColumn($tokenText) + protected function getOrderColumnMap() { throw new NotSupportedException(); } diff --git a/src/Search/Parsers/TagSearchParser.php b/src/Search/Parsers/TagSearchParser.php index a271d3dc..f33300f3 100644 --- a/src/Search/Parsers/TagSearchParser.php +++ b/src/Search/Parsers/TagSearchParser.php @@ -38,20 +38,14 @@ class TagSearchParser extends AbstractSearchParser throw new NotSupportedException(); } - protected function getOrderColumn($tokenText) + protected function getOrderColumnMap() { - if ($this->matches($tokenText, ['id'])) - return TagFilter::ORDER_ID; - - if ($this->matches($tokenText, ['name'])) - return TagFilter::ORDER_NAME; - - if ($this->matches($tokenText, ['creation_time', 'creation_date'])) - return TagFilter::ORDER_CREATION_TIME; - - if ($this->matches($tokenText, ['usage_count', 'usages'])) - return TagFilter::ORDER_USAGE_COUNT; - - throw new NotSupportedException(); + return + [ + [['id'], TagFilter::ORDER_ID], + [['name'], TagFilter::ORDER_NAME], + [['creation_time', 'creation_date'], TagFilter::ORDER_CREATION_TIME], + [['usage_count', 'usages'], TagFilter::ORDER_USAGE_COUNT], + ]; } } diff --git a/src/Search/Parsers/UserSearchParser.php b/src/Search/Parsers/UserSearchParser.php index 143a9b4b..34cbf27d 100644 --- a/src/Search/Parsers/UserSearchParser.php +++ b/src/Search/Parsers/UserSearchParser.php @@ -23,14 +23,12 @@ class UserSearchParser extends AbstractSearchParser throw new NotSupportedException(); } - protected function getOrderColumn($tokenText) + protected function getOrderColumnMap() { - if ($this->matches($tokenText, ['name'])) - return UserFilter::ORDER_NAME; - - if ($this->matches($tokenText, ['registration_time', 'registration_date'])) - return UserFilter::ORDER_REGISTRATION_TIME; - - throw new NotSupportedException(); + return + [ + [['name'], UserFilter::ORDER_NAME], + [['registration_time', 'registration_date'], UserFilter::ORDER_REGISTRATION_TIME], + ]; } }