mirror of
https://github.com/flarum/framework.git
synced 2025-01-30 22:38:43 +08:00
Query Namespace (#2645)
Move shared classes in search and filter namespaces to a new query namespace
This commit is contained in:
parent
185abf05b7
commit
5813f2522c
|
@ -14,7 +14,7 @@ use Flarum\Discussion\Discussion;
|
|||
use Flarum\Discussion\Filter\DiscussionFilterer;
|
||||
use Flarum\Discussion\Search\DiscussionSearcher;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
|
@ -93,7 +93,7 @@ class ListDiscussionsController extends AbstractListController
|
|||
$offset = $this->extractOffset($request);
|
||||
$include = array_merge($this->extractInclude($request), ['state']);
|
||||
|
||||
$criteria = new SearchCriteria($actor, $filters, $sort);
|
||||
$criteria = new QueryCriteria($actor, $filters, $sort);
|
||||
if (array_key_exists('q', $filters)) {
|
||||
$results = $this->searcher->search($criteria, $limit, $offset);
|
||||
} else {
|
||||
|
|
|
@ -13,7 +13,7 @@ use Flarum\Api\Serializer\PostSerializer;
|
|||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Post\Filter\PostFilterer;
|
||||
use Flarum\Post\PostRepository;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Illuminate\Support\Arr;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
@ -83,7 +83,7 @@ class ListPostsController extends AbstractListController
|
|||
$offset = $this->extractOffset($request);
|
||||
$include = $this->extractInclude($request);
|
||||
|
||||
$results = $this->filterer->filter(new SearchCriteria($actor, $filters, $sort), $limit, $offset);
|
||||
$results = $this->filterer->filter(new QueryCriteria($actor, $filters, $sort), $limit, $offset);
|
||||
|
||||
$document->addPaginationLinks(
|
||||
$this->url->to('api')->route('posts.index'),
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace Flarum\Api\Controller;
|
|||
|
||||
use Flarum\Api\Serializer\UserSerializer;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\User\Filter\UserFilterer;
|
||||
use Flarum\User\Search\UserSearcher;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
@ -83,7 +83,7 @@ class ListUsersController extends AbstractListController
|
|||
$offset = $this->extractOffset($request);
|
||||
$include = $this->extractInclude($request);
|
||||
|
||||
$criteria = new SearchCriteria($actor, $filters, $sort);
|
||||
$criteria = new QueryCriteria($actor, $filters, $sort);
|
||||
if (array_key_exists('q', $filters)) {
|
||||
$results = $this->searcher->search($criteria, $limit, $offset);
|
||||
} else {
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
namespace Flarum\Discussion\Event;
|
||||
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\Search\SearchState;
|
||||
|
||||
/**
|
||||
|
@ -23,15 +23,15 @@ class Searching
|
|||
public $search;
|
||||
|
||||
/**
|
||||
* @var \Flarum\Search\SearchCriteria
|
||||
* @var \Flarum\Query\QueryCriteria
|
||||
*/
|
||||
public $criteria;
|
||||
|
||||
/**
|
||||
* @param SearchState $search
|
||||
* @param \Flarum\Search\SearchCriteria $criteria
|
||||
* @param \Flarum\Query\QueryCriteria $criteria
|
||||
*/
|
||||
public function __construct(SearchState $search, SearchCriteria $criteria)
|
||||
public function __construct(SearchState $search, QueryCriteria $criteria)
|
||||
{
|
||||
$this->search = $search;
|
||||
$this->criteria = $criteria;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Filter;
|
||||
namespace Flarum\Discussion\Query;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Filter;
|
||||
namespace Flarum\Discussion\Query;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Filter;
|
||||
namespace Flarum\Discussion\Query;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Discussion\Filter;
|
||||
namespace Flarum\Discussion\Query;
|
||||
|
||||
use Flarum\Discussion\DiscussionRepository;
|
||||
use Flarum\Filter\FilterInterface;
|
|
@ -11,9 +11,9 @@ namespace Flarum\Discussion\Search;
|
|||
|
||||
use Flarum\Discussion\DiscussionRepository;
|
||||
use Flarum\Discussion\Event\Searching;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\Search\AbstractSearcher;
|
||||
use Flarum\Search\GambitManager;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
@ -53,7 +53,7 @@ class DiscussionSearcher extends AbstractSearcher
|
|||
/**
|
||||
* @deprecated along with the Searching event, remove in Beta 17.
|
||||
*/
|
||||
protected function mutateSearch(SearchState $search, SearchCriteria $criteria)
|
||||
protected function mutateSearch(SearchState $search, QueryCriteria $criteria)
|
||||
{
|
||||
parent::mutateSearch($search, $criteria);
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ class Filter implements ExtenderInterface
|
|||
*
|
||||
* The callback can be a closure or an invokable class, and should accept:
|
||||
* - Flarum\Filter\FilterState $filter
|
||||
* - Flarum\Search\SearchCriteria $criteria
|
||||
* - Flarum\Query\QueryCriteria $criteria
|
||||
*/
|
||||
public function addFilterMutator($callback)
|
||||
{
|
||||
|
|
|
@ -61,7 +61,7 @@ class SimpleFlarumSearch implements ExtenderInterface
|
|||
*
|
||||
* The callback can be a closure or an invokable class, and should accept:
|
||||
* - Flarum\Search\SearchState $search
|
||||
* - Flarum\Search\SearchCriteria $criteria
|
||||
* - Flarum\Query\QueryCriteria $criteria
|
||||
*/
|
||||
public function addSearchMutator($callback)
|
||||
{
|
||||
|
|
|
@ -11,9 +11,9 @@ namespace Flarum\Filter;
|
|||
|
||||
use Flarum\Event\ConfigurePostsQuery;
|
||||
use Flarum\Post\Filter\PostFilterer;
|
||||
use Flarum\Search\ApplySearchParametersTrait;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Search\SearchResults;
|
||||
use Flarum\Query\ApplyQueryParametersTrait;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\Query\QueryResults;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
|
@ -21,7 +21,7 @@ use InvalidArgumentException;
|
|||
|
||||
abstract class AbstractFilterer
|
||||
{
|
||||
use ApplySearchParametersTrait;
|
||||
use ApplyQueryParametersTrait;
|
||||
|
||||
protected $filters;
|
||||
|
||||
|
@ -40,14 +40,14 @@ abstract class AbstractFilterer
|
|||
abstract protected function getQuery(User $actor): Builder;
|
||||
|
||||
/**
|
||||
* @param SearchCriteria $criteria
|
||||
* @param QueryCriteria $criteria
|
||||
* @param mixed|null $limit
|
||||
* @param int $offset
|
||||
*
|
||||
* @return SearchResults
|
||||
* @return QueryResults
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function filter(SearchCriteria $criteria, int $limit = null, int $offset = 0): SearchResults
|
||||
public function filter(QueryCriteria $criteria, int $limit = null, int $offset = 0): QueryResults
|
||||
{
|
||||
$actor = $criteria->actor;
|
||||
|
||||
|
@ -89,6 +89,6 @@ abstract class AbstractFilterer
|
|||
$results->pop();
|
||||
}
|
||||
|
||||
return new SearchResults($results, $areMoreResults);
|
||||
return new QueryResults($results, $areMoreResults);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,14 +9,14 @@
|
|||
|
||||
namespace Flarum\Filter;
|
||||
|
||||
use Flarum\Discussion\Filter as DiscussionFilter;
|
||||
use Flarum\Discussion\Filter\DiscussionFilterer;
|
||||
use Flarum\Discussion\Query as DiscussionQuery;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Foundation\ContainerUtil;
|
||||
use Flarum\Post\Filter as PostFilter;
|
||||
use Flarum\Post\Filter\PostFilterer;
|
||||
use Flarum\User\Filter as UserFilter;
|
||||
use Flarum\User\Filter\UserFilterer;
|
||||
use Flarum\User\Query as UserQuery;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class FilterServiceProvider extends AbstractServiceProvider
|
||||
|
@ -31,14 +31,14 @@ class FilterServiceProvider extends AbstractServiceProvider
|
|||
$this->app->singleton('flarum.filter.filters', function () {
|
||||
return [
|
||||
DiscussionFilterer::class => [
|
||||
DiscussionFilter\AuthorFilterGambit::class,
|
||||
DiscussionFilter\CreatedFilterGambit::class,
|
||||
DiscussionFilter\HiddenFilterGambit::class,
|
||||
DiscussionFilter\UnreadFilterGambit::class,
|
||||
DiscussionQuery\AuthorFilterGambit::class,
|
||||
DiscussionQuery\CreatedFilterGambit::class,
|
||||
DiscussionQuery\HiddenFilterGambit::class,
|
||||
DiscussionQuery\UnreadFilterGambit::class,
|
||||
],
|
||||
UserFilterer::class => [
|
||||
UserFilter\EmailFilterGambit::class,
|
||||
UserFilter\GroupFilterGambit::class,
|
||||
UserQuery\EmailFilterGambit::class,
|
||||
UserQuery\GroupFilterGambit::class,
|
||||
],
|
||||
PostFilterer::class => [
|
||||
PostFilter\AuthorFilter::class,
|
||||
|
|
|
@ -9,79 +9,8 @@
|
|||
|
||||
namespace Flarum\Filter;
|
||||
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Flarum\Query\AbstractQueryState;
|
||||
|
||||
class FilterState
|
||||
class FilterState extends AbstractQueryState
|
||||
{
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $actor;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $defaultSort = [];
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param User $actor
|
||||
*/
|
||||
public function __construct(Builder $query, User $actor, $defaultSort = [])
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->actor = $actor;
|
||||
$this->defaultSort = $defaultSort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query builder for the search results query.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user who is performing the search.
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function getActor()
|
||||
{
|
||||
return $this->actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default sort order for the search.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultSort()
|
||||
{
|
||||
return $this->defaultSort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default sort order for the search. This will only be applied if
|
||||
* a sort order has not been specified in the search criteria.
|
||||
*
|
||||
* @param mixed $defaultSort An array of sort-order pairs, where the column
|
||||
* is the key, and the order is the value. The order may be 'asc',
|
||||
* 'desc', or an array of IDs to order by.
|
||||
* Alternatively, a callable may be used.
|
||||
* @return mixed
|
||||
*/
|
||||
public function setDefaultSort($defaultSort)
|
||||
{
|
||||
$this->defaultSort = $defaultSort;
|
||||
}
|
||||
}
|
||||
|
|
87
framework/core/src/Query/AbstractQueryState.php
Normal file
87
framework/core/src/Query/AbstractQueryState.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?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\Query;
|
||||
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
abstract class AbstractQueryState
|
||||
{
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $actor;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $defaultSort = [];
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param User $actor
|
||||
*/
|
||||
public function __construct(Builder $query, User $actor, $defaultSort = [])
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->actor = $actor;
|
||||
$this->defaultSort = $defaultSort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query builder for the search results query.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user who is performing the search.
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function getActor()
|
||||
{
|
||||
return $this->actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default sort order for the search.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultSort()
|
||||
{
|
||||
return $this->defaultSort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default sort order for the search. This will only be applied if
|
||||
* a sort order has not been specified in the search criteria.
|
||||
*
|
||||
* @param mixed $defaultSort An array of sort-order pairs, where the column
|
||||
* is the key, and the order is the value. The order may be 'asc',
|
||||
* 'desc', or an array of IDs to order by.
|
||||
* Alternatively, a callable may be used.
|
||||
* @return mixed
|
||||
*/
|
||||
public function setDefaultSort($defaultSort)
|
||||
{
|
||||
$this->defaultSort = $defaultSort;
|
||||
}
|
||||
}
|
62
framework/core/src/Query/ApplyQueryParametersTrait.php
Normal file
62
framework/core/src/Query/ApplyQueryParametersTrait.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?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\Query;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait ApplyQueryParametersTrait
|
||||
{
|
||||
/**
|
||||
* Apply sort criteria to a discussion query.
|
||||
*
|
||||
* @param AbstractQueryState $query
|
||||
* @param array $sort
|
||||
*/
|
||||
protected function applySort(AbstractQueryState $query, array $sort = null)
|
||||
{
|
||||
$sort = $sort ?: $query->getDefaultSort();
|
||||
|
||||
if (is_callable($sort)) {
|
||||
$sort($query->getQuery());
|
||||
} else {
|
||||
foreach ($sort as $field => $order) {
|
||||
if (is_array($order)) {
|
||||
foreach ($order as $value) {
|
||||
$query->getQuery()->orderByRaw(Str::snake($field).' != ?', [$value]);
|
||||
}
|
||||
} else {
|
||||
$query->getQuery()->orderBy(Str::snake($field), $order);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractQueryState $query
|
||||
* @param int $offset
|
||||
*/
|
||||
protected function applyOffset(AbstractQueryState $query, $offset)
|
||||
{
|
||||
if ($offset > 0) {
|
||||
$query->getQuery()->skip($offset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractQueryState $query
|
||||
* @param int|null $limit
|
||||
*/
|
||||
protected function applyLimit(AbstractQueryState $query, $limit)
|
||||
{
|
||||
if ($limit > 0) {
|
||||
$query->getQuery()->take($limit);
|
||||
}
|
||||
}
|
||||
}
|
58
framework/core/src/Query/QueryCriteria.php
Normal file
58
framework/core/src/Query/QueryCriteria.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?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\Query;
|
||||
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\User\User;
|
||||
|
||||
/**
|
||||
* Represents the criteria that will determine the entire result set of a
|
||||
* query. The limit and offset are not included because they only determine
|
||||
* which part of the entire result set will be returned.
|
||||
*/
|
||||
class QueryCriteria extends SearchCriteria
|
||||
{
|
||||
/**
|
||||
* The user performing the query.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* Query params.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $query;
|
||||
|
||||
/**
|
||||
* An array of sort-order pairs, where the column is the key, and the order
|
||||
* is the value. The order may be 'asc', 'desc', or an array of IDs to
|
||||
* order by.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sort;
|
||||
|
||||
/**
|
||||
* @param User $actor The user performing the query.
|
||||
* @param array $query The query params.
|
||||
* @param array $sort An array of sort-order pairs, where the column is the
|
||||
* key, and the order is the value. The order may be 'asc', 'desc', or
|
||||
* an array of IDs to order by.
|
||||
*/
|
||||
public function __construct(User $actor, $query, array $sort = null)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->query = $query;
|
||||
$this->sort = $sort;
|
||||
}
|
||||
}
|
52
framework/core/src/Query/QueryResults.php
Normal file
52
framework/core/src/Query/QueryResults.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?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\Query;
|
||||
|
||||
use Flarum\Search\SearchResults;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class QueryResults extends SearchResults
|
||||
{
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
protected $results;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $areMoreResults;
|
||||
|
||||
/**
|
||||
* @param Collection $results
|
||||
* @param bool $areMoreResults
|
||||
*/
|
||||
public function __construct(Collection $results, $areMoreResults)
|
||||
{
|
||||
$this->results = $results;
|
||||
$this->areMoreResults = $areMoreResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getResults()
|
||||
{
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function areMoreResults()
|
||||
{
|
||||
return $this->areMoreResults;
|
||||
}
|
||||
}
|
|
@ -9,13 +9,13 @@
|
|||
|
||||
namespace Flarum\Search;
|
||||
|
||||
use Flarum\Filter\FilterState;
|
||||
use Flarum\Query\AbstractQueryState;
|
||||
|
||||
/**
|
||||
* @deprecated, use SearchState instead.
|
||||
* These methods should be transferred over to SearchState in beta 17.
|
||||
*/
|
||||
class AbstractSearch extends FilterState
|
||||
class AbstractSearch extends AbstractQueryState
|
||||
{
|
||||
/**
|
||||
* @var GambitInterface[]
|
||||
|
|
|
@ -9,12 +9,15 @@
|
|||
|
||||
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 ApplySearchParametersTrait;
|
||||
use ApplyQueryParametersTrait;
|
||||
|
||||
/**
|
||||
* @var GambitManager
|
||||
|
@ -34,7 +37,7 @@ abstract class AbstractSearcher
|
|||
|
||||
abstract protected function getQuery(User $actor): Builder;
|
||||
|
||||
protected function mutateSearch(SearchState $search, SearchCriteria $criteria)
|
||||
protected function mutateSearch(SearchState $search, QueryCriteria $criteria)
|
||||
{
|
||||
foreach ($this->searchMutators as $mutator) {
|
||||
$mutator($search, $criteria);
|
||||
|
@ -42,14 +45,14 @@ abstract class AbstractSearcher
|
|||
}
|
||||
|
||||
/**
|
||||
* @param SearchCriteria $criteria
|
||||
* @param QueryCriteria $criteria
|
||||
* @param int|null $limit
|
||||
* @param int $offset
|
||||
*
|
||||
* @return SearchResults
|
||||
* @return QueryResults
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function search(SearchCriteria $criteria, $limit = null, $offset = 0): SearchResults
|
||||
public function search(QueryCriteria $criteria, $limit = null, $offset = 0): QueryResults
|
||||
{
|
||||
$actor = $criteria->actor;
|
||||
|
||||
|
@ -73,6 +76,6 @@ abstract class AbstractSearcher
|
|||
$results->pop();
|
||||
}
|
||||
|
||||
return new SearchResults($results, $areMoreResults);
|
||||
return new QueryResults($results, $areMoreResults);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
<?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\Filter\FilterState;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait ApplySearchParametersTrait
|
||||
{
|
||||
/**
|
||||
* Apply sort criteria to a discussion search.
|
||||
*
|
||||
* @param FilterState $search
|
||||
* @param array $sort
|
||||
*/
|
||||
protected function applySort(FilterState $search, array $sort = null)
|
||||
{
|
||||
$sort = $sort ?: $search->getDefaultSort();
|
||||
|
||||
if (is_callable($sort)) {
|
||||
$sort($search->getQuery());
|
||||
} else {
|
||||
foreach ($sort as $field => $order) {
|
||||
if (is_array($order)) {
|
||||
foreach ($order as $value) {
|
||||
$search->getQuery()->orderByRaw(Str::snake($field).' != ?', [$value]);
|
||||
}
|
||||
} else {
|
||||
$search->getQuery()->orderBy(Str::snake($field), $order);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FilterState $search
|
||||
* @param int $offset
|
||||
*/
|
||||
protected function applyOffset(FilterState $search, $offset)
|
||||
{
|
||||
if ($offset > 0) {
|
||||
$search->getQuery()->skip($offset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FilterState $search
|
||||
* @param int|null $limit
|
||||
*/
|
||||
protected function applyLimit(FilterState $search, $limit)
|
||||
{
|
||||
if ($limit > 0) {
|
||||
$search->getQuery()->take($limit);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,49 +9,9 @@
|
|||
|
||||
namespace Flarum\Search;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
/**
|
||||
* Represents the criteria that will determine the entire result set of a
|
||||
* query. The limit and offset are not included because they only determine
|
||||
* which part of the entire result set will be returned.
|
||||
* @deprecated beta 16, remove beta 17
|
||||
*/
|
||||
class SearchCriteria
|
||||
{
|
||||
/**
|
||||
* The user performing the query.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* Query params.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $query;
|
||||
|
||||
/**
|
||||
* An array of sort-order pairs, where the column is the key, and the order
|
||||
* is the value. The order may be 'asc', 'desc', or an array of IDs to
|
||||
* order by.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sort;
|
||||
|
||||
/**
|
||||
* @param User $actor The user performing the query.
|
||||
* @param array $query The query params.
|
||||
* @param array $sort An array of sort-order pairs, where the column is the
|
||||
* key, and the order is the value. The order may be 'asc', 'desc', or
|
||||
* an array of IDs to order by.
|
||||
*/
|
||||
public function __construct(User $actor, $query, array $sort = null)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->query = $query;
|
||||
$this->sort = $sort;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,43 +9,9 @@
|
|||
|
||||
namespace Flarum\Search;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
/**
|
||||
* @deprecated beta 16, remove beta 17
|
||||
*/
|
||||
class SearchResults
|
||||
{
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
protected $results;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $areMoreResults;
|
||||
|
||||
/**
|
||||
* @param Collection $results
|
||||
* @param bool $areMoreResults
|
||||
*/
|
||||
public function __construct(Collection $results, $areMoreResults)
|
||||
{
|
||||
$this->results = $results;
|
||||
$this->areMoreResults = $areMoreResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getResults()
|
||||
{
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function areMoreResults()
|
||||
{
|
||||
return $this->areMoreResults;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,18 +9,14 @@
|
|||
|
||||
namespace Flarum\Search;
|
||||
|
||||
use Flarum\Discussion\Filter\AuthorFilterGambit;
|
||||
use Flarum\Discussion\Filter\CreatedFilterGambit;
|
||||
use Flarum\Discussion\Filter\HiddenFilterGambit;
|
||||
use Flarum\Discussion\Filter\UnreadFilterGambit;
|
||||
use Flarum\Discussion\Query as DiscussionQuery;
|
||||
use Flarum\Discussion\Search\DiscussionSearcher;
|
||||
use Flarum\Discussion\Search\Gambit\FulltextGambit as DiscussionFulltextGambit;
|
||||
use Flarum\Event\ConfigureDiscussionGambits;
|
||||
use Flarum\Event\ConfigureUserGambits;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Foundation\ContainerUtil;
|
||||
use Flarum\User\Filter\EmailFilterGambit;
|
||||
use Flarum\User\Filter\GroupFilterGambit;
|
||||
use Flarum\User\Query as UserQuery;
|
||||
use Flarum\User\Search\Gambit\FulltextGambit as UserFulltextGambit;
|
||||
use Flarum\User\Search\UserSearcher;
|
||||
use Illuminate\Support\Arr;
|
||||
|
@ -42,14 +38,14 @@ class SearchServiceProvider extends AbstractServiceProvider
|
|||
$this->app->singleton('flarum.simple_search.gambits', function () {
|
||||
return [
|
||||
DiscussionSearcher::class => [
|
||||
AuthorFilterGambit::class,
|
||||
CreatedFilterGambit::class,
|
||||
HiddenFilterGambit::class,
|
||||
UnreadFilterGambit::class
|
||||
DiscussionQuery\AuthorFilterGambit::class,
|
||||
DiscussionQuery\CreatedFilterGambit::class,
|
||||
DiscussionQuery\HiddenFilterGambit::class,
|
||||
DiscussionQuery\UnreadFilterGambit::class,
|
||||
],
|
||||
UserSearcher::class => [
|
||||
EmailFilterGambit::class,
|
||||
GroupFilterGambit::class
|
||||
UserQuery\EmailFilterGambit::class,
|
||||
UserQuery\GroupFilterGambit::class,
|
||||
]
|
||||
];
|
||||
});
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
namespace Flarum\User\Event;
|
||||
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\Search\SearchState;
|
||||
|
||||
/**
|
||||
|
@ -23,15 +23,15 @@ class Searching
|
|||
public $search;
|
||||
|
||||
/**
|
||||
* @var SearchCriteria
|
||||
* @var QueryCriteria
|
||||
*/
|
||||
public $criteria;
|
||||
|
||||
/**
|
||||
* @param SearchState $search
|
||||
* @param SearchCriteria $criteria
|
||||
* @param QueryCriteria $criteria
|
||||
*/
|
||||
public function __construct(SearchState $search, SearchCriteria $criteria)
|
||||
public function __construct(SearchState $search, QueryCriteria $criteria)
|
||||
{
|
||||
$this->search = $search;
|
||||
$this->criteria = $criteria;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\User\Filter;
|
||||
namespace Flarum\User\Query;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\User\Filter;
|
||||
namespace Flarum\User\Query;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
namespace Flarum\User\Search;
|
||||
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\Search\AbstractSearcher;
|
||||
use Flarum\Search\GambitManager;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\User\Event\Searching;
|
||||
use Flarum\User\User;
|
||||
|
@ -19,10 +19,6 @@ use Flarum\User\UserRepository;
|
|||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* Takes a UserSearchCriteria object, performs a search using gambits,
|
||||
* and spits out a UserSearchResults object.
|
||||
*/
|
||||
class UserSearcher extends AbstractSearcher
|
||||
{
|
||||
/**
|
||||
|
@ -57,7 +53,7 @@ class UserSearcher extends AbstractSearcher
|
|||
/**
|
||||
* @deprecated along with the Searching event, remove in Beta 17.
|
||||
*/
|
||||
protected function mutateSearch(SearchState $search, SearchCriteria $criteria)
|
||||
protected function mutateSearch(SearchState $search, QueryCriteria $criteria)
|
||||
{
|
||||
parent::mutateSearch($search, $criteria);
|
||||
|
||||
|
|
|
@ -12,9 +12,9 @@ namespace Flarum\Tests\integration\extenders;
|
|||
use Carbon\Carbon;
|
||||
use Flarum\Discussion\Search\DiscussionSearcher;
|
||||
use Flarum\Extend;
|
||||
use Flarum\Query\QueryCriteria;
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\GambitInterface;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
|
@ -64,7 +64,7 @@ class SimpleFlarumSearchTest extends TestCase
|
|||
|
||||
$actor = User::find(1);
|
||||
|
||||
$criteria = new SearchCriteria($actor, ['q' => $query]);
|
||||
$criteria = new QueryCriteria($actor, ['q' => $query]);
|
||||
|
||||
return $this->app()->getContainer()->make(DiscussionSearcher::class)->search($criteria, $limit)->getResults();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user