-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
CreatedFilter.php
55 lines (45 loc) · 1.61 KB
/
CreatedFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Discussion\Search\Filter;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
/**
* @implements FilterInterface<DatabaseSearchState>
*/
class CreatedFilter implements FilterInterface
{
use ValidateFilterTrait;
public function getFilterKey(): string
{
return 'created';
}
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$value = $this->asString($value);
preg_match('/^(\d{4}-\d{2}-\d{2})(?:\.\.(\d{4}-\d{2}-\d{2}))?$/', $value, $matches);
$from = Arr::get($matches, 1);
$to = Arr::get($matches, 2);
$this->constrain($state->getQuery(), $from, $to, $negate);
}
public function constrain(Builder $query, ?string $from, ?string $to, bool $negate): void
{
// If we've just been provided with a single YYYY-MM-DD date, then find
// discussions that were started on that exact date. But if we've been
// provided with a YYYY-MM-DD..YYYY-MM-DD range, then find discussions
// that were started during that period.
if (empty($to)) {
$query->whereDate('created_at', $negate ? '!=' : '=', $from);
} else {
$query->whereBetween('created_at', [$from, $to], 'and', $negate);
}
}
}