Clean up gambit

This commit is contained in:
Franz Liedke 2017-01-03 21:22:19 +01:00
parent 204f63fa29
commit c89def0d78
2 changed files with 15 additions and 3 deletions

View File

@ -45,6 +45,7 @@ class SearchServiceProvider extends AbstractServiceProvider
$gambits->setFulltextGambit('Flarum\Core\Search\User\Gambit\FulltextGambit');
$gambits->add('Flarum\Core\Search\User\Gambit\EmailGambit');
$gambits->add('Flarum\Core\Search\User\Gambit\GroupGambit');
$app->make('events')->fire(
new ConfigureUserGambits($gambits)
);

View File

@ -46,11 +46,11 @@ class GroupGambit extends AbstractRegexGambit
throw new LogicException('This gambit can only be applied on a UserSearch');
}
$groupName = trim($matches[1], '"');
$groupName = explode(',', $groupName);
$groupNames = $this->extractGroupNames($matches);
// TODO: Use a JOIN instead (and don't forget to remove the findByName() method again)
$ids = [];
foreach ($groupName as $name) {
foreach ($groupNames as $name) {
$group = $this->groups->findByName($name);
if ($group && count($group->users)) {
$ids = array_merge($ids, $group->users->pluck('id')->all());
@ -59,4 +59,15 @@ class GroupGambit extends AbstractRegexGambit
$search->getQuery()->whereIn('id', $ids, 'and', $negate);
}
/**
* Extract the group names from the pattern match.
*
* @param array $matches
* @return array
*/
protected function extractGroupNames(array $matches)
{
return explode(',', trim($matches[1], '"'));
}
}