Skip to content

Commit

Permalink
Improves "where" modifier value check
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Aug 22, 2024
1 parent 513bc96 commit 2d897e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2921,7 +2921,7 @@ public function where($value, $params)
if (! $opr && Str::contains($key, ':')) {
[$key, $opr] = explode(':', $key);
}
if (! $val) {
if (! $val && $val !== 0) {
$val = $opr;
$opr = '==';
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Modifiers/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,53 @@ public function it_filters_data_using_operator(): void
$this->assertEquals($expected, Arr::pluck($modified, 'title'));
}

#[Test]
public function it_accepts_zero_as_a_parameter()
{
$data = [
[
'name' => 'Fluffy McSparkles',
'whimsy_level' => 42,
],
[
'name' => 'Sir Bubblesworth',
'whimsy_level' => 0,
],
[
'name' => 'Captain Fuzzybeard',
'whimsy_level' => 13,
],
[
'name' => 'Professor Whiskerbottom',
'whimsy_level' => 0,
],
[
'name' => 'Duchess Snugglefluff',
'whimsy_level' => 76,
],
];

$expected = [
'Fluffy McSparkles',
'Captain Fuzzybeard',
'Duchess Snugglefluff',
];

$modified = $this->modify($data, ['whimsy_level', '!=', 0]);
$this->assertEquals($expected, Arr::pluck($modified, 'name'));

$expected = [
'Sir Bubblesworth',
'Professor Whiskerbottom',
];

$modified = $this->modify($data, ['whimsy_level', 0]);
$this->assertEquals($expected, Arr::pluck($modified, 'name'));

$modified = $this->modify($data, ['whimsy_level', '==', 0]);
$this->assertEquals($expected, Arr::pluck($modified, 'name'));
}

private function modify($value, array $params)
{
return Modify::value($value)->where($params)->fetch();
Expand Down

0 comments on commit 2d897e6

Please sign in to comment.