Skip to content

Improvement handle falsy values in collections #55512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,14 @@ public function firstWhere($key, $operator = null, $value = null)
*
* @param string $key
* @param TValueDefault|(\Closure(): TValueDefault) $default
* @param bool $allowFalsy
* @return TValue|TValueDefault
*/
public function value($key, $default = null)
public function value($key, $default = null, $allowFalsy = false)
{
if ($value = $this->firstWhere($key)) {
$value = $allowFalsy ? $this->first() : $this->firstWhere($key);

if ($value) {
return data_get($value, $key, $default);
}

Expand Down
118 changes: 118 additions & 0 deletions tests/Support/ValuePreservingFalsyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Tests\Feature;

use PHPUnit\Framework\TestCase;

class ValuePreservingFalsyTest extends TestCase
{
// Test when the value is 0 (integer)
public function test_zero_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'balance' => 0],
['name' => 'John', 'balance' => 200],
]);

$this->assertSame(0, $collection->value('balance', allowFalsy: true));
}

// Test when the value is 0.0 (float)
public function test_float_zero_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'balance' => 0.0],
['name' => 'John', 'balance' => 200.5],
]);

$this->assertSame(0.0, $collection->value('balance', allowFalsy: true));
}

// Test when the value is false (boolean)
public function test_false_is_preserved()
{
$collection = collect([
['name' => 'John', 'vegetarian' => true],
['name' => 'Tim', 'vegetarian' => false],
]);

$this->assertFalse($collection->where('name', 'Tim')->value('vegetarian', allowFalsy: true));
}

// Test when the value is an empty string
public function test_empty_string_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'status' => ''],
['name' => 'John', 'status' => 'active'],
]);

$this->assertSame('', $collection->value('status', allowFalsy: true));
}

// Test when the value is null
public function test_null_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'age' => null],
['name' => 'John', 'age' => 30],
]);

$this->assertNull($collection->value('age', allowFalsy: true));
}

// Test when the value is an empty array
public function test_empty_array_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'tags' => []],
['name' => 'John', 'tags' => ['admin']],
]);

$this->assertSame([], $collection->value('tags', allowFalsy: true));
}

// Test when the value is '0' (string zero)
public function test_string_zero_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'balance' => '0'],
['name' => 'John', 'balance' => '100'],
]);

$this->assertSame('0', $collection->value('balance', allowFalsy: true));
}

// Test when a missing key is provided
public function test_missing_key_returns_default()
{
$collection = collect([
['name' => 'Tim', 'balance' => 0],
['name' => 'John', 'balance' => 200],
]);

$this->assertSame('default_value', $collection->value('missing_key', 'default_value', allowFalsy: true));
}

// Test when a falsy value in a subsequent item is returned (e.g. first item is falsy, second is not)
public function test_first_falsy_value_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'status' => 0],
['name' => 'John', 'status' => 'active'],
]);

$this->assertSame(0, $collection->value('status', allowFalsy: true));
}

// Test when a falsy value in a subsequent item is returned (string '0' case)
public function test_first_item_with_string_zero_is_preserved()
{
$collection = collect([
['name' => 'Tim', 'status' => '0'],
['name' => 'John', 'status' => 'active'],
]);

$this->assertSame('0', $collection->value('status', allowFalsy: true));
}
}