Merge branch 'created-gambit' of https://github.com/Albert221/core

This commit is contained in:
Toby Zerner 2016-01-13 09:53:24 +10:30
commit 4ec108f28a
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,42 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Core\Search\Discussion\Gambit;
use Flarum\Core\Search\AbstractRegexGambit;
use Flarum\Core\Search\AbstractSearch;
use Flarum\Core\Search\Discussion\DiscussionSearch;
use LogicException;
class CreatedGambit extends AbstractRegexGambit
{
/**
* http://stackoverflow.com/a/8270148/3158312
*
* {@inheritdoc}
*/
protected $pattern = 'created:(\d{4}\-\d\d\-\d\d)(\.\.(\d{4}\-\d\d\-\d\d))?';
/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
if (! $search instanceof DiscussionSearch) {
throw new LogicException('This gambit can only be applied on a DiscussionSearch');
}
if (empty($matches[4])) { // Single date
$search->getQuery()->whereDate('start_time', $negate ? '!=' : '=', $matches[2]);
} else { // Range: date..date
$search->getQuery()->whereBetween('start_time', [$matches[2], $matches[4]], 'and', $negate);
}
}
}

View File

@ -61,6 +61,7 @@ class SearchServiceProvider extends AbstractServiceProvider
$gambits->setFulltextGambit('Flarum\Core\Search\Discussion\Gambit\FulltextGambit');
$gambits->add('Flarum\Core\Search\Discussion\Gambit\AuthorGambit');
$gambits->add('Flarum\Core\Search\Discussion\Gambit\CreatedGambit');
$gambits->add('Flarum\Core\Search\Discussion\Gambit\HiddenGambit');
$gambits->add('Flarum\Core\Search\Discussion\Gambit\UnreadGambit');