Improved search error messages

This commit is contained in:
Marcin Kurczewski 2015-05-23 10:46:17 +02:00
parent 5412ac14b9
commit 24d8bf5295
5 changed files with 71 additions and 41 deletions

View file

@ -149,7 +149,7 @@ class PostDao extends AbstractDao implements ICrudDao
return;
}
elseif ($requirement->getType() === PostFilter::REQUIREMENT_COMMENT)
elseif ($requirement->getType() === PostFilter::REQUIREMENT_COMMENT_AUTHOR)
{
foreach ($requirement->getValue()->getValues() as $userName)
{

View file

@ -104,7 +104,12 @@ class EnumHelper
$key = trim(strtolower($enumString));
$lowerEnumMap = array_change_key_case($enumMap, \CASE_LOWER);
if (!isset($lowerEnumMap[$key]))
throw new \DomainException('Unrecognized value: ' . $enumString);
{
throw new \DomainException(sprintf(
'Unrecognized value: %s.' . PHP_EOL . 'Possible values: %s',
$enumString,
join(', ', array_keys($lowerEnumMap))));
}
return $lowerEnumMap[$key];
}

View file

@ -3,8 +3,8 @@ namespace Szurubooru;
class NotSupportedException extends \BadMethodCallException
{
public function __construct()
public function __construct($message = null)
{
parent::__construct('Not supported');
parent::__construct($message === null ? 'Not supported' : $message);
}
}

View file

@ -25,7 +25,7 @@ class PostFilter extends BasicFilter implements IFilter
const REQUIREMENT_UPLOADER = 'uploader.name';
const REQUIREMENT_SAFETY = 'safety';
const REQUIREMENT_FAVORITE = 'favoritedBy.name';
const REQUIREMENT_COMMENT = 'commentedBy.name';
const REQUIREMENT_COMMENT_AUTHOR = 'commentedBy.name';
const REQUIREMENT_TYPE = 'contentType';
const REQUIREMENT_USER_SCORE = 'userScore';

View file

@ -43,7 +43,13 @@ class PostSearchParser extends AbstractSearchParser
$tokenKey = $token->getKey();
$tokenValue = $token->getValue();
$countAliases = ['tag_count' => 'tag', 'fav_count' => 'fav', 'score' => 'score'];
$countAliases =
[
'tag_count' => 'tags',
'fav_count' => 'favs',
'score' => 'score',
'comment_count' => 'comments',
];
foreach ($countAliases as $realKey => $baseAlias)
{
if ($this->matches($tokenKey, [$baseAlias . '_min', $baseAlias . '_max']))
@ -60,59 +66,49 @@ class PostSearchParser extends AbstractSearchParser
[['id'], [$this, 'addIdRequirement']],
[['hash', 'name'], [$this, 'addHashRequirement']],
[['date', 'time'], [$this, 'addDateRequirement']],
[['tag_count'], [$this, 'addTagCountRequirement']],
[['fav_count'], [$this, 'addFavCountRequirement']],
[['comment_count'], [$this, 'addCommentCountRequirement']],
[['tag_count', 'tags'], [$this, 'addTagCountRequirement']],
[['fav_count', 'favs'], [$this, 'addFavCountRequirement']],
[['comment_count', 'comments'], [$this, 'addCommentCountRequirement']],
[['score'], [$this, 'addScoreRequirement']],
[['uploader', 'submit', 'up'], [$this, 'addUploaderRequirement']],
[['uploader', 'uploader', 'uploaded', 'submit', 'submitter', 'submitted'], [$this, 'addUploaderRequirement']],
[['safety', 'rating'], [$this, 'addSafetyRequirement']],
[['fav'], [$this, 'addFavRequirement']],
[['type'], [$this, 'addTypeRequirement']],
[['comment'], [$this, 'addCommentRequirement']],
[['comment', 'comment_author', 'commented'], [$this, 'addCommentAuthorRequirement']],
];
foreach ($map as $item)
{
list ($aliases, $callback) = $item;
if ($this->matches($tokenKey, $aliases))
{
return $callback($filter, $token);
}
}
if ($this->matches($tokenKey, ['special']))
{
if ($this->matches($tokenValue, ['liked']))
$specialMap =
[
[['liked'], [$this, 'addOwnLikedRequirement']],
[['disliked'], [$this, 'addOwnDislikedRequirement']],
[['fav'], [$this, 'addOwnFavRequirement']],
];
foreach ($specialMap as $item)
{
$this->privilegeService->assertLoggedIn();
return $this->addUserScoreRequirement(
$filter,
$this->authService->getLoggedInUser()->getName(),
1,
$token->isNegated());
list ($aliases, $callback) = $item;
if ($this->matches($token->getValue(), $aliases))
return $callback($filter, $token);
}
if ($this->matches($tokenValue, ['disliked']))
{
$this->privilegeService->assertLoggedIn();
return $this->addUserScoreRequirement(
$filter,
$this->authService->getLoggedInUser()->getName(),
-1,
$token->isNegated());
}
if ($this->matches($tokenValue, ['fav']))
{
$this->privilegeService->assertLoggedIn();
$token = new NamedSearchToken();
$token->setKey('fav');
$token->setValue($this->authService->getLoggedInUser()->getName());
return $this->decorateFilterFromNamedToken($filter, $token);
}
throw new NotSupportedException(
'Unknown value for special search term: ' . $token->getValue()
. '. Possible search terms: '
. join(', ', array_map(function($term) { return join('/', $term[0]); }, $specialMap)));
}
throw new NotSupportedException();
throw new NotSupportedException('Unknown search term: ' . $token->getKey()
. '. Possible search terms: special, '
. join(', ', array_map(function($term) { return join('/', $term[0]); }, $map)));
}
protected function getOrderColumn($tokenText)
@ -153,6 +149,35 @@ class PostSearchParser extends AbstractSearchParser
throw new NotSupportedException();
}
private function addOwnLikedRequirement($filter, $token)
{
$this->privilegeService->assertLoggedIn();
$this->addUserScoreRequirement(
$filter,
$this->authService->getLoggedInUser()->getName(),
1,
$token->isNegated());
}
private function addOwnDislikedRequirement($filter, $token)
{
$this->privilegeService->assertLoggedIn();
$this->addUserScoreRequirement(
$filter,
$this->authService->getLoggedInUser()->getName(),
-1,
$token->isNegated());
}
private function addOwnFavRequirement($filter, $token)
{
$this->privilegeService->assertLoggedIn();
$token = new NamedSearchToken();
$token->setKey('fav');
$token->setValue($this->authService->getLoggedInUser()->getName());
$this->decorateFilterFromNamedToken($filter, $token);
}
private function addIdRequirement($filter, $token)
{
$this->addRequirementFromToken(
@ -280,12 +305,12 @@ class PostSearchParser extends AbstractSearchParser
});
}
private function addCommentRequirement($filter, $token)
private function addCommentAuthorRequirement($filter, $token)
{
$this->addRequirementFromToken(
$filter,
$token,
PostFilter::REQUIREMENT_COMMENT,
PostFilter::REQUIREMENT_COMMENT_AUTHOR,
self::ALLOW_COMPOSITE);
}