Skip to content

Commit

Permalink
Optimized the implementation of When Method.
Browse files Browse the repository at this point in the history
  • Loading branch information
assert6 committed May 9, 2024
1 parent 97b7ac1 commit 6064063
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ public function first($columns = ['*'])
/**
* Apply the callback's query changes if the given "value" is true.
*
* @param callable $callback
* @param callable $default
* @param mixed $value
* @return $this|mixed
* @param callable($this, $value): $this $callback
* @param callable($this, $value): $this $default
* @return $this
*/
public function when($value, $callback, $default = null)
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;
}

Expand All @@ -123,13 +123,14 @@ public function tap($callback)
/**
* Apply the callback's query changes if the given "value" is false.
*
* @param callable $callback
* @param callable $default
* @param mixed $value
* @return $this|mixed
* @param callable($this, $value): $this $callback
* @param callable($this, $value): $this $default
* @return $this
*/
public function unless($value, $callback, $default = null)
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;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,29 @@ public function testWhenCallback(): void
$this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
}

public function testWhenValueOfCallback(): void
{
$callback = function (Builder $query, $condition) {
$this->assertTrue($condition);

$query->where('id', '=', 1);
};

$builder = $this->getBuilder();
$builder->select('*')
->from('users')
->when(fn (Builder $query) => true, $callback)
->where('email', 'foo');
$this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')
->from('users')
->when(fn (Builder $query) => false, $callback)
->where('email', 'foo');
$this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
}

public function testWhenCallbackWithReturn(): void
{
$callback = function ($query, $condition) {
Expand Down Expand Up @@ -304,6 +327,29 @@ public function testUnlessCallback(): void
$this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
}

public function testUnlessValueOfCallback(): void
{
$callback = function (Builder $query, $condition) {
$this->assertFalse($condition);

$query->where('id', '=', 1);
};

$builder = $this->getBuilder();
$builder->select('*')
->from('users')
->unless(fn (Builder $query) => true, $callback)
->where('email', 'foo');
$this->assertSame('select * from "users" where "email" = ?', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')
->from('users')
->unless(fn (Builder $query) => false, $callback)
->where('email', 'foo');
$this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
}

public function testUnlessCallbackWithReturn(): void
{
$callback = function ($query, $condition) {
Expand Down

0 comments on commit 6064063

Please sign in to comment.