feat: Add Conditionable trait to BuildsQueries (#6855)

This commit is contained in:
Deeka Wong 2024-06-11 19:40:41 +08:00 committed by GitHub
parent 37550281c0
commit d7a5856a75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 41 deletions

View File

@ -9,6 +9,10 @@
- [#6845](https://github.com/hyperf/hyperf/pull/6845) Added method `Hyperf\Database\Schema::getTables()`. - [#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]`. - [#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 # v3.1.25.1 - 2024-06-07
## Added ## Added

View File

@ -15,6 +15,7 @@ namespace Hyperf\Database\Concerns;
use Closure; use Closure;
use Hyperf\Collection\Collection as BaseCollection; use Hyperf\Collection\Collection as BaseCollection;
use Hyperf\Collection\LazyCollection; use Hyperf\Collection\LazyCollection;
use Hyperf\Conditionable\Conditionable;
use Hyperf\Context\ApplicationContext; use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\LengthAwarePaginatorInterface; use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Contract\PaginatorInterface; use Hyperf\Contract\PaginatorInterface;
@ -34,6 +35,8 @@ use function Hyperf\Collection\data_get;
trait BuildsQueries trait BuildsQueries
{ {
use Conditionable;
/** /**
* Chunk the results of the query. * Chunk the results of the query.
* *
@ -201,26 +204,6 @@ trait BuildsQueries
return $this->orderedChunkById($count, $callback, $column, $alias, descending: true); 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. * Pass the query to a given callback.
* *
@ -232,27 +215,6 @@ trait BuildsQueries
return $this->when(true, $callback); 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. * 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 protected function paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options): LengthAwarePaginatorInterface
{ {
/** @var Container $container */
$container = ApplicationContext::getContainer(); $container = ApplicationContext::getContainer();
if (! method_exists($container, 'make')) { if (! method_exists($container, 'make')) {
throw new RuntimeException('The DI container does not support make() method.'); 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 protected function simplePaginator(Collection $items, int $perPage, int $currentPage, array $options): PaginatorInterface
{ {
/** @var Container $container */
$container = ApplicationContext::getContainer(); $container = ApplicationContext::getContainer();
if (! method_exists($container, 'make')) { if (! method_exists($container, 'make')) {
throw new RuntimeException('The DI container does not support make() method.'); throw new RuntimeException('The DI container does not support make() method.');