8000 [12.x] Add $preserveFalsy flag to value() method to retain falsy values by lokeshrangani · Pull Request #55499 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add $preserveFalsy flag to value() method to retain falsy values #55499

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 1 commit
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
Prev Previous commit
Add $preserveFalsy flag to value() method to retain falsy values
  • Loading branch information
lokeshrangani committed Apr 22, 2025
commit aad40a5b17deb285d11258b043730f57f3236796
40 changes: 8 additions & 32 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,43 +341,19 @@ public function firstWhere($key, $operator = null, $value = null)
* @param TValueDefault|(\Closure(): TValueDefault) $default
* @return TValue|TValueDefault
*/
public function value($key, $default = null)
public function value($key, $default = null, $preserveFalsy = false)
{
if ($value = $this->firstWhere($key)) {
return data_get($value, $key, $default);
if ($preserveFalsy === true) {
$value = $this->first();
} else {
$value = $this->firstWhere($key);
}

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};
if ($value) {
return data_get($value, $key, $default);
}

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

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/Support/ValuePreservingFalsyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function test_zero_is_preserved()
['name' => 'John', 'balance' => 200],
]);

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

// Test when the value is 0.0 (float)
Expand All @@ -25,7 +25,7 @@ public function test_float_zero_is_preserved()
['name' => 'John', 'balance' => 200.5],
]);

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

// Test when the value is false (boolean)
Expand All @@ -36,7 +36,7 @@ public function test_false_is_preserved()
['name' => 'Tim', 'vegetarian' => false],
]);

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

// Test when the value is an empty string
Expand All @@ -47,7 +47,7 @@ public function test_empty_string_is_preserved()
['name' => 'John', 'status' => 'active'],
]);

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

// Test when the value is null
Expand All @@ -58,7 +58,7 @@ public function test_null_is_preserved()
['name' => 'John', 'age' => 30],
]);

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

// Test when the value is an empty array
Expand All @@ -69,7 +69,7 @@ public function test_empty_array_is_preserved()
['name' => 'John', 'tags' => ['admin']],
]);

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

// Test when the value is '0' (string zero)
Expand All @@ -80,7 +80,7 @@ public function test_string_zero_is_preserved()
['name' => 'John', 'balance' => '100'],
]);

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

// Test when a missing key is provided
Expand All @@ -91,7 +91,7 @@ public function test_missing_key_returns_default()
['name' => 'John', 'balance' => 200],
]);

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

// Test when a falsy value in a subsequent item is returned (e.g. first item is falsy, second is not)
Expand All @@ -102,7 +102,7 @@ public function test_first_falsy_value_is_preserved()
['name' => 'John', 'status' => 'active'],
]);

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

// Test when a falsy value in a subsequent item is returned (string '0' case)
Expand All @@ -113,6 +113,6 @@ public function test_first_item_with_string_zero_is_preserved()
['name' => 'John', 'status' => 'active'],
]);

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