Skip to content

Commit

Permalink
Add query buider 'unless' method (inverse of 'when') (#19738)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio authored and taylorotwell committed Jun 23, 2017
1 parent 43bda84 commit d043f49
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public function when($value, $callback, $default = null)
return $this;
}

/**
* Apply the callback's query changes if the given "value" is false.
*
* @param mixed $value
* @param callable $callback
* @param callable $default
* @return mixed
*/
public function unless($value, $callback, $default = null)
{
if (! $value) {
return $callback($this, $value) ?: $this;
} elseif ($default) {
return $default($this, $value) ?: $this;
}

return $this;
}

/**
* Create a new length-aware paginator instance.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,65 @@ public function testWhenCallbackWithDefault()
$this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
}

public function testUnlessCallback()
{
$callback = function ($query, $condition) {
$this->assertFalse($condition);

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

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

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

public function testUnlessCallbackWithReturn()
{
$callback = function ($query, $condition) {
$this->assertFalse($condition);

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

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

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

public function testUnlessCallbackWithDefault()
{
$callback = function ($query, $condition) {
$this->assertEquals($condition, 0);

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

$default = function ($query, $condition) {
$this->assertEquals($condition, 'truthy');

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

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless(0, $callback, $default)->where('email', 'foo');
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless('truthy', $callback, $default)->where('email', 'foo');
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
$this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
}

public function testTapCallback()
{
$callback = function ($query) {
Expand Down

0 comments on commit d043f49

Please sign in to comment.