Added helpful messages for invalid search orders

This commit is contained in:
rr- 2015-08-04 19:47:18 +02:00
parent 8c87a93774
commit 6b933132a5
5 changed files with 49 additions and 62 deletions

View file

@ -1,6 +1,7 @@
<?php <?php
namespace Szurubooru\Search\Parsers; namespace Szurubooru\Search\Parsers;
use Szurubooru\Helpers\InputReader; use Szurubooru\Helpers\InputReader;
use Szurubooru\NotSupportedException;
use Szurubooru\Search\Filters\IFilter; use Szurubooru\Search\Filters\IFilter;
use Szurubooru\Search\Requirements\Requirement; use Szurubooru\Search\Requirements\Requirement;
use Szurubooru\Search\Requirements\RequirementCompositeValue; use Szurubooru\Search\Requirements\RequirementCompositeValue;
@ -51,7 +52,7 @@ abstract class AbstractSearchParser
protected abstract function decorateFilterFromNamedToken(IFilter $filter, NamedSearchToken $namedToken); protected abstract function decorateFilterFromNamedToken(IFilter $filter, NamedSearchToken $namedToken);
protected abstract function getOrderColumn($tokenText); protected abstract function getOrderColumnMap();
protected function createRequirementValue($text, $flags = 0, callable $valueDecorator = null) protected function createRequirementValue($text, $flags = 0, callable $valueDecorator = null)
{ {
@ -95,6 +96,22 @@ abstract class AbstractSearchParser
$filter->addRequirement($requirement); $filter->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) private function getOrder($query, $negated)
{ {
$order = []; $order = [];

View file

@ -113,45 +113,23 @@ class PostSearchParser extends AbstractSearchParser
. join(', ', array_map(function($term) { return join('/', $term[0]); }, $map))); . join(', ', array_map(function($term) { return join('/', $term[0]); }, $map)));
} }
protected function getOrderColumn($tokenText) protected function getOrderColumnMap()
{ {
if ($this->matches($tokenText, ['random'])) return
return PostFilter::ORDER_RANDOM; [
[['id'], PostFilter::ORDER_ID],
if ($this->matches($tokenText, ['id'])) [['random'], PostFilter::ORDER_RANDOM],
return PostFilter::ORDER_ID; [['time', 'date'], PostFilter::ORDER_LAST_EDIT_TIME],
[['score'], PostFilter::ORDER_SCORE],
if ($this->matches($tokenText, ['time', 'date'])) [['file_size'], PostFilter::ORDER_FILE_SIZE],
return PostFilter::ORDER_LAST_EDIT_TIME; [['tag_count', 'tags', 'tag'], PostFilter::ORDER_TAG_COUNT],
[['fav_count', 'fags', 'fav'], PostFilter::ORDER_FAV_COUNT],
if ($this->matches($tokenText, ['score'])) [['comment_count', 'comments', 'comment'], PostFilter::ORDER_COMMENT_COUNT],
return PostFilter::ORDER_SCORE; [['note_count', 'notes', 'note'], PostFilter::ORDER_NOTE_COUNT],
[['fav_time', 'fav_date'], PostFilter::ORDER_LAST_FAV_TIME],
if ($this->matches($tokenText, ['file_size'])) [['comment_time', 'comment_date'], PostFilter::ORDER_LAST_COMMENT_TIME],
return PostFilter::ORDER_FILE_SIZE; [['feature_time', 'feature_date', 'featured', 'feature'], PostFilter::ORDER_LAST_FEATURE_TIME],
];
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();
} }
private function addOwnLikedRequirement($filter, $token) private function addOwnLikedRequirement($filter, $token)

View file

@ -41,7 +41,7 @@ class SnapshotSearchParser extends AbstractSearchParser
throw new NotSupportedException(); throw new NotSupportedException();
} }
protected function getOrderColumn($tokenText) protected function getOrderColumnMap()
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }

View file

@ -38,20 +38,14 @@ class TagSearchParser extends AbstractSearchParser
throw new NotSupportedException(); throw new NotSupportedException();
} }
protected function getOrderColumn($tokenText) protected function getOrderColumnMap()
{ {
if ($this->matches($tokenText, ['id'])) return
return TagFilter::ORDER_ID; [
[['id'], TagFilter::ORDER_ID],
if ($this->matches($tokenText, ['name'])) [['name'], TagFilter::ORDER_NAME],
return TagFilter::ORDER_NAME; [['creation_time', 'creation_date'], TagFilter::ORDER_CREATION_TIME],
[['usage_count', 'usages'], TagFilter::ORDER_USAGE_COUNT],
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();
} }
} }

View file

@ -23,14 +23,12 @@ class UserSearchParser extends AbstractSearchParser
throw new NotSupportedException(); throw new NotSupportedException();
} }
protected function getOrderColumn($tokenText) protected function getOrderColumnMap()
{ {
if ($this->matches($tokenText, ['name'])) return
return UserFilter::ORDER_NAME; [
[['name'], UserFilter::ORDER_NAME],
if ($this->matches($tokenText, ['registration_time', 'registration_date'])) [['registration_time', 'registration_date'], UserFilter::ORDER_REGISTRATION_TIME],
return UserFilter::ORDER_REGISTRATION_TIME; ];
throw new NotSupportedException();
} }
} }