mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
feat: Add Conditionable trait to BuildsQueries (#6855)
This commit is contained in:
parent
37550281c0
commit
d7a5856a75
@ -9,6 +9,10 @@
|
||||
- [#6845](https://github.com/hyperf/hyperf/pull/6845) Added method `Hyperf\Database\Schema::getTables()`.
|
||||
- [#6846](https://github.com/hyperf/hyperf/pull/6846) Added methods `BuildsQueries::[chunkById|chunkByIdDesc]`.
|
||||
|
||||
## Optimized
|
||||
|
||||
- [#6855](https://github.com/hyperf/hyperf/pull/6855) Optimized BuildsQueries to use `Conditionable` instead of `when` and `unless`.
|
||||
|
||||
# v3.1.25.1 - 2024-06-07
|
||||
|
||||
## Added
|
||||
|
@ -15,6 +15,7 @@ namespace Hyperf\Database\Concerns;
|
||||
use Closure;
|
||||
use Hyperf\Collection\Collection as BaseCollection;
|
||||
use Hyperf\Collection\LazyCollection;
|
||||
use Hyperf\Conditionable\Conditionable;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Contract\LengthAwarePaginatorInterface;
|
||||
use Hyperf\Contract\PaginatorInterface;
|
||||
@ -34,6 +35,8 @@ use function Hyperf\Collection\data_get;
|
||||
|
||||
trait BuildsQueries
|
||||
{
|
||||
use Conditionable;
|
||||
|
||||
/**
|
||||
* Chunk the results of the query.
|
||||
*
|
||||
@ -201,26 +204,6 @@ trait BuildsQueries
|
||||
return $this->orderedChunkById($count, $callback, $column, $alias, descending: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the callback's query changes if the given "value" is true.
|
||||
*
|
||||
* @param callable($this, $value): $this $callback
|
||||
* @param callable($this, $value): $this $default
|
||||
* @return $this
|
||||
*/
|
||||
public function when(mixed $value, callable $callback, ?callable $default = null): static
|
||||
{
|
||||
$value = $value instanceof Closure ? $value($this) : $value;
|
||||
|
||||
if ($value) {
|
||||
return $callback($this, $value) ?: $this;
|
||||
}
|
||||
if ($default) {
|
||||
return $default($this, $value) ?: $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass the query to a given callback.
|
||||
*
|
||||
@ -232,27 +215,6 @@ trait BuildsQueries
|
||||
return $this->when(true, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the callback's query changes if the given "value" is false.
|
||||
*
|
||||
* @param callable($this, $value): $this $callback
|
||||
* @param callable($this, $value): $this $default
|
||||
* @return $this
|
||||
*/
|
||||
public function unless(mixed $value, callable $callback, ?callable $default = null): static
|
||||
{
|
||||
$value = $value instanceof Closure ? $value($this) : $value;
|
||||
|
||||
if (! $value) {
|
||||
return $callback($this, $value) ?: $this;
|
||||
}
|
||||
if ($default) {
|
||||
return $default($this, $value) ?: $this;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query lazily, by chunking the results of a query by comparing IDs in a given order.
|
||||
*/
|
||||
@ -300,6 +262,7 @@ trait BuildsQueries
|
||||
*/
|
||||
protected function paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options): LengthAwarePaginatorInterface
|
||||
{
|
||||
/** @var Container $container */
|
||||
$container = ApplicationContext::getContainer();
|
||||
if (! method_exists($container, 'make')) {
|
||||
throw new RuntimeException('The DI container does not support make() method.');
|
||||
@ -312,6 +275,7 @@ trait BuildsQueries
|
||||
*/
|
||||
protected function simplePaginator(Collection $items, int $perPage, int $currentPage, array $options): PaginatorInterface
|
||||
{
|
||||
/** @var Container $container */
|
||||
$container = ApplicationContext::getContainer();
|
||||
if (! method_exists($container, 'make')) {
|
||||
throw new RuntimeException('The DI container does not support make() method.');
|
||||
|
Loading…
Reference in New Issue
Block a user