Skip to content

Commit

Permalink
Unify the Implementation of when and unless methods in \Hyperf\Sc…
Browse files Browse the repository at this point in the history
…out\Builder (#6856)
  • Loading branch information
zds-s authored Jun 12, 2024
1 parent ddcf756 commit 0dcaabc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require": {
"php": ">=8.1",
"hyperf/command": "~3.1.0",
"hyperf/conditionable": "~3.1.0",
"hyperf/contract": "~3.1.0",
"hyperf/database": "~3.1.0",
"hyperf/event": "~3.1.0",
Expand Down
22 changes: 2 additions & 20 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Closure;
use Hyperf\Collection\Collection as BaseCollection;
use Hyperf\Conditionable\Conditionable;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Model;
use Hyperf\Macroable\Macroable;
Expand All @@ -26,6 +27,7 @@
class Builder
{
use Macroable;
use Conditionable;

/**
* Optional callback before model query execution.
Expand Down Expand Up @@ -127,26 +129,6 @@ public function orderBy(string $column, ?string $direction = 'asc'): static
return $this;
}

/**
* 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.
*/
Expand Down

0 comments on commit 0dcaabc

Please sign in to comment.