1
0
mirror of https://github.com/flarum/core.git synced 2025-07-19 15:51:16 +02:00

Query Namespace (#2645)

Move shared classes in search and filter namespaces to a new query namespace
This commit is contained in:
Alexander Skvortsov
2021-03-02 09:57:40 -05:00
committed by GitHub
parent e37fdef709
commit a9526917b8
29 changed files with 328 additions and 282 deletions

View 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;
}
}

View 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);
}
}
}

View 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;
}
}

View 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;
}
}