From 2b07803d4ef8e6f35b8acce451d700c1ba79a64c Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 13 Dec 2024 07:35:26 +0800 Subject: [PATCH 1/5] [11.x] Allows `enum_value()` to be use in standalone `illuminate/collections` fixes #53867 Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Collections/functions.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Collections/functions.php b/src/Illuminate/Collections/functions.php index 08cd5ba0a6f7..4e910380ef04 100644 --- a/src/Illuminate/Collections/functions.php +++ b/src/Illuminate/Collections/functions.php @@ -17,11 +17,20 @@ */ function enum_value($value, $default = null) { - return transform($value, fn ($value) => match (true) { + if (function_exists('transform')) { + return transform($value, fn ($value) => match (true) { + $value instanceof \BackedEnum => $value->value, + $value instanceof \UnitEnum => $value->name, + + default => $value, + }, $default ?? $value); + } + + return match (true) { $value instanceof \BackedEnum => $value->value, $value instanceof \UnitEnum => $value->name, default => $value, - }, $default ?? $value); + }; } } From 6ac7abaa904afbf7331b3ff5935a43f80f91b723 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 13 Dec 2024 13:19:07 +0800 Subject: [PATCH 2/5] Update ModelInspector.php --- src/Illuminate/Database/Eloquent/ModelInspector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/ModelInspector.php b/src/Illuminate/Database/Eloquent/ModelInspector.php index c4b6e8952e70..96afe44c187a 100644 --- a/src/Illuminate/Database/Eloquent/ModelInspector.php +++ b/src/Illuminate/Database/Eloquent/ModelInspector.php @@ -393,7 +393,7 @@ protected function getColumnDefault($column, $model) { $attributeDefault = $model->getAttributes()[$column['name']] ?? null; - return enum_value($attributeDefault, $column['default']); + return enum_value($attributeDefault) ?? $column['default']; } /** From 872b52daa9c268b4bc48783b2594edd8d0cb073d Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 13 Dec 2024 13:20:56 +0800 Subject: [PATCH 3/5] Update functions.php --- src/Illuminate/Collections/functions.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/Illuminate/Collections/functions.php b/src/Illuminate/Collections/functions.php index 4e910380ef04..f5bc8b1e5880 100644 --- a/src/Illuminate/Collections/functions.php +++ b/src/Illuminate/Collections/functions.php @@ -8,24 +8,11 @@ * * @internal * - * @template TValue - * @template TDefault - * - * @param TValue $value - * @param TDefault|callable(TValue): TDefault $default - * @return ($value is empty ? TDefault : mixed) + * @param mixed $value + * @return mixed */ - function enum_value($value, $default = null) + function enum_value($value) { - if (function_exists('transform')) { - return transform($value, fn ($value) => match (true) { - $value instanceof \BackedEnum => $value->value, - $value instanceof \UnitEnum => $value->name, - - default => $value, - }, $default ?? $value); - } - return match (true) { $value instanceof \BackedEnum => $value->value, $value instanceof \UnitEnum => $value->name, From ad903280d35c2e0207fd3f24481b2cd0497c6f4e Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 13 Dec 2024 15:06:20 +0800 Subject: [PATCH 4/5] wip Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Collections/functions.php | 12 ++++++++---- tests/Support/SupportEnumValueFunctionTest.php | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Collections/functions.php b/src/Illuminate/Collections/functions.php index f5bc8b1e5880..6ccd9b3ed0b4 100644 --- a/src/Illuminate/Collections/functions.php +++ b/src/Illuminate/Collections/functions.php @@ -8,16 +8,20 @@ * * @internal * - * @param mixed $value - * @return mixed + * @template TValue + * @template TDefault + * + * @param TValue $value + * @param TDefault|callable(TValue): TDefault $default + * @return ($value is empty ? TDefault : mixed) */ - function enum_value($value) + function enum_value($value, $default = null) { return match (true) { $value instanceof \BackedEnum => $value->value, $value instanceof \UnitEnum => $value->name, - default => $value, + default => $value ?? value($default), }; } } diff --git a/tests/Support/SupportEnumValueFunctionTest.php b/tests/Support/SupportEnumValueFunctionTest.php index 7750d5c3b7e6..a2152292a4cf 100644 --- a/tests/Support/SupportEnumValueFunctionTest.php +++ b/tests/Support/SupportEnumValueFunctionTest.php @@ -28,6 +28,12 @@ public function test_it_can_handle_enum_value($given, $expected) $this->assertSame($expected, enum_value($given)); } + public function test_it_can_fallback_to_use_default_if_value_is_null() + { + $this->assertSame('laravel', enum_value(null, 'laravel')); + $this->assertSame('laravel', enum_value(null, fn () => 'laravel')); + } + public static function scalarDataProvider() { yield [null, null]; From b3fe51cb6c9126092e27d4adf65545b77cef7fe0 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 13 Dec 2024 15:18:23 +0800 Subject: [PATCH 5/5] wip Signed-off-by: Mior Muhammad Zaki --- tests/Support/SupportEnumValueFunctionTest.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/Support/SupportEnumValueFunctionTest.php b/tests/Support/SupportEnumValueFunctionTest.php index a2152292a4cf..6a705642c4f7 100644 --- a/tests/Support/SupportEnumValueFunctionTest.php +++ b/tests/Support/SupportEnumValueFunctionTest.php @@ -11,17 +11,6 @@ class SupportEnumValueFunctionTest extends TestCase { - public function test_it_can_handle_enums_value() - { - $this->assertSame('A', enum_value(TestEnum::A)); - - $this->assertSame(1, enum_value(TestBackedEnum::A)); - $this->assertSame(2, enum_value(TestBackedEnum::B)); - - $this->assertSame('A', enum_value(TestStringBackedEnum::A)); - $this->assertSame('B', enum_value(TestStringBackedEnum::B)); - } - #[DataProvider('scalarDataProvider')] public function test_it_can_handle_enum_value($given, $expected) { @@ -36,6 +25,11 @@ public function test_it_can_fallback_to_use_default_if_value_is_null() public static function scalarDataProvider() { + yield [TestEnum::A, 'A']; + yield [TestBackedEnum::A, 1]; + yield [TestBackedEnum::B, 2]; + yield [TestStringBackedEnum::A, 'A']; + yield [TestStringBackedEnum::B, 'B']; yield [null, null]; yield [0, 0]; yield ['0', '0'];