-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Improves
Collection
support for enums using firstWhere()
a…
…nd `value()` (#53777) * [11.x] Improves `Collection` support for enums using `firstWhere()` and `value()` Fixes #53676 Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
8b889c7
commit b8bb5e9
Showing
6 changed files
with
68 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"Illuminate\\Support\\": "" | ||
}, | ||
"files": [ | ||
"functions.php", | ||
"helpers.php" | ||
] | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Illuminate\Support; | ||
|
||
if (! function_exists('Illuminate\Support\enum_value')) { | ||
/** | ||
* Return a scalar value for the given value that might be an enum. | ||
* | ||
* @internal | ||
* | ||
* @template TValue | ||
* @template TDefault | ||
* | ||
* @param TValue $value | ||
* @param TDefault|callable(TValue): TDefault $default | ||
* @return ($value is empty ? TDefault : mixed) | ||
*/ | ||
function enum_value($value, $default = null) | ||
{ | ||
return transform($value, fn ($value) => match (true) { | ||
$value instanceof \BackedEnum => $value->value, | ||
$value instanceof \UnitEnum => $value->name, | ||
|
||
default => $value, | ||
}, $default ?? $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters