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
namespace Szurubooru\Search\Parsers;
use Szurubooru\Helpers\InputReader;
use Szurubooru\NotSupportedException;
use Szurubooru\Search\Filters\IFilter;
use Szurubooru\Search\Requirements\Requirement;
use Szurubooru\Search\Requirements\RequirementCompositeValue;
@ -51,7 +52,7 @@ abstract class AbstractSearchParser
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)
{
@ -95,6 +96,22 @@ abstract class AbstractSearchParser
$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)
{
$order = [];

View file

@ -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)

View file

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

View file

@ -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],
];
}
}

View file

@ -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],
];
}
}