/
AbstractSearcher.php
75 lines (59 loc) · 1.95 KB
/
AbstractSearcher.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?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\Search;
use Flarum\Query\ApplyQueryParametersTrait;
use Flarum\Query\QueryCriteria;
use Flarum\Query\QueryResults;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
abstract class AbstractSearcher
{
use ApplyQueryParametersTrait;
/**
* @var GambitManager
*/
protected $gambits;
/**
* @var array
*/
protected $searchMutators;
public function __construct(GambitManager $gambits, array $searchMutators)
{
$this->gambits = $gambits;
$this->searchMutators = $searchMutators;
}
abstract protected function getQuery(User $actor): Builder;
/**
* @param QueryCriteria $criteria
* @param int|null $limit
* @param int $offset
*
* @return QueryResults
*/
public function search(QueryCriteria $criteria, $limit = null, $offset = 0): QueryResults
{
$actor = $criteria->actor;
$query = $this->getQuery($actor);
$search = new SearchState($query->getQuery(), $actor);
$this->gambits->apply($search, $criteria->query['q']);
$this->applySort($search, $criteria->sort, $criteria->sortIsDefault);
$this->applyOffset($search, $offset);
$this->applyLimit($search, $limit + 1);
foreach ($this->searchMutators as $mutator) {
$mutator($search, $criteria);
}
// Execute the search query and retrieve the results. We get one more
// results than the user asked for, so that we can say if there are more
// results. If there are, we will get rid of that extra result.
$results = $query->get();
if ($areMoreResults = $limit > 0 && $results->count() > $limit) {
$results->pop();
}
return new QueryResults($results, $areMoreResults);
}
}