diff --git a/src/Core/Search/Discussion/Gambit/CreatedGambit.php b/src/Core/Search/Discussion/Gambit/CreatedGambit.php new file mode 100644 index 000000000..3696d180e --- /dev/null +++ b/src/Core/Search/Discussion/Gambit/CreatedGambit.php @@ -0,0 +1,42 @@ + + * + * 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); + } + } +} diff --git a/src/Core/Search/SearchServiceProvider.php b/src/Core/Search/SearchServiceProvider.php index 0b9260cf0..221bf30c0 100644 --- a/src/Core/Search/SearchServiceProvider.php +++ b/src/Core/Search/SearchServiceProvider.php @@ -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');