Skip to content

Add valuePreservingFalsy method to handle falsy values in collections #55498

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

Closed
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
30 changes: 30 additions & 0 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,36 @@ public function value($key, $default = null)
return value($default);
}

/**
* Get the value of a given key from the first item in the collection,
* even if the value is a falsy one like 0, false, or an empty string.
*
* This method avoids treating falsy values as null or non-existent.
*
* @param string|int $key The key to retrieve from the first matching item.
* @param mixed|null $default Default value if key not found.
* @return mixed
*/
public function valuePreservingFalsy($key, $default = null)
{
$item = $this->first();

if ($item === null) {
return value($default);
}

if (is_array($item) && array_key_exists($key, $item)) {
return $item[$key];
}

if (is_object($item) && property_exists($item, $key)) {
return $item->{$key};
}

// Fallback to dot notation
return data_get($item, $key, $default);
}

/**
* Ensure that every item in the collection is of the expected type.
*
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->valuePreservingFalsy('balance'));
}

// 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->valuePreservingFalsy('balance'));
}

// 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')->valuePreservingFalsy('vegetarian'));
}

// 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->valuePreservingFalsy('status'));
}

// 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->valuePreservingFalsy('age'));
}

// 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->valuePreservingFalsy('tags'));
}

// 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->valuePreservingFalsy('balance'));
}

// 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->valuePreservingFalsy('missing_key', 'default_value'));
}

// 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->valuePreservingFalsy('status'));
}

// 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->valuePreservingFalsy('status'));
}
}