From 92cd12ae3f2d10a2543d49eceb3b343caea25149 Mon Sep 17 00:00:00 2001 From: lukeraymonddowning Date: Thu, 11 Apr 2024 20:08:56 +0100 Subject: [PATCH 1/4] [11.x] Adds support for enums on `mapInto` collection method. --- .../Collections/Traits/EnumeratesValues.php | 5 ++++ tests/Support/Enums.php | 6 +++++ ...php => SupportCollectionTestIntBacked.php} | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+) rename tests/Support/{SupportCollectionTest.php => SupportCollectionTestIntBacked.php} (99%) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index 041f80c080a7..a13b39f5ef55 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -2,6 +2,7 @@ namespace Illuminate\Support\Traits; +use BackedEnum; use CachingIterator; use Closure; use Exception; @@ -410,6 +411,10 @@ public function flatMap(callable $callback) */ public function mapInto($class) { + if (enum_exists($class)) { + return $this->map(fn ($value, $key) => $class::from($value)); + } + return $this->map(fn ($value, $key) => new $class($value, $key)); } diff --git a/tests/Support/Enums.php b/tests/Support/Enums.php index 45fc07da52f5..3d31e55321c6 100644 --- a/tests/Support/Enums.php +++ b/tests/Support/Enums.php @@ -12,3 +12,9 @@ enum TestBackedEnum: int case A = 1; case B = 2; } + +enum TestStringBackedEnum: string +{ + case A = 'A'; + case B = 'B'; +} diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTestIntBacked.php similarity index 99% rename from tests/Support/SupportCollectionTest.php rename to tests/Support/SupportCollectionTestIntBacked.php index 3edff0c35f96..ff124c09e013 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTestIntBacked.php @@ -2961,6 +2961,32 @@ public function testMapInto($collection) $this->assertSame('second', $data->get(1)->value); } + #[DataProvider('collectionClassProvider')] + public function testMapIntoWithIntBackedEnums($collection) + { + $data = new $collection([ + 1, 2 + ]); + + $data = $data->mapInto(TestBackedEnum::class); + + $this->assertSame(TestBackedEnum::A, $data->get(0)); + $this->assertSame(TestBackedEnum::B, $data->get(1)); + } + + #[DataProvider('collectionClassProvider')] + public function testMapIntoWithStringBackedEnums($collection) + { + $data = new $collection([ + 'A', 'B' + ]); + + $data = $data->mapInto(TestStringBackedEnum::class); + + $this->assertSame(TestStringBackedEnum::A, $data->get(0)); + $this->assertSame(TestStringBackedEnum::B, $data->get(1)); + } + #[DataProvider('collectionClassProvider')] public function testNth($collection) { From d97bd54162404018e51a6f34e3d45a1c4b30af3f Mon Sep 17 00:00:00 2001 From: lukeraymonddowning Date: Thu, 11 Apr 2024 20:19:12 +0100 Subject: [PATCH 2/4] [11.x] Adds support for enums on `mapInto` collection method. --- src/Illuminate/Collections/Traits/EnumeratesValues.php | 1 - tests/Support/SupportCollectionTestIntBacked.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index a13b39f5ef55..ccd128caf9fb 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -2,7 +2,6 @@ namespace Illuminate\Support\Traits; -use BackedEnum; use CachingIterator; use Closure; use Exception; diff --git a/tests/Support/SupportCollectionTestIntBacked.php b/tests/Support/SupportCollectionTestIntBacked.php index ff124c09e013..7e2565044c72 100755 --- a/tests/Support/SupportCollectionTestIntBacked.php +++ b/tests/Support/SupportCollectionTestIntBacked.php @@ -2965,7 +2965,7 @@ public function testMapInto($collection) public function testMapIntoWithIntBackedEnums($collection) { $data = new $collection([ - 1, 2 + 1, 2, ]); $data = $data->mapInto(TestBackedEnum::class); @@ -2978,7 +2978,7 @@ public function testMapIntoWithIntBackedEnums($collection) public function testMapIntoWithStringBackedEnums($collection) { $data = new $collection([ - 'A', 'B' + 'A', 'B', ]); $data = $data->mapInto(TestStringBackedEnum::class); From 06e3c373b471bae52347781b6156368076affeea Mon Sep 17 00:00:00 2001 From: lukeraymonddowning Date: Thu, 11 Apr 2024 20:33:16 +0100 Subject: [PATCH 3/4] [11.x] Adds support for enums on `mapInto` collection method. --- ...pportCollectionTestIntBacked.php => SupportCollectionTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/Support/{SupportCollectionTestIntBacked.php => SupportCollectionTest.php} (100%) diff --git a/tests/Support/SupportCollectionTestIntBacked.php b/tests/Support/SupportCollectionTest.php similarity index 100% rename from tests/Support/SupportCollectionTestIntBacked.php rename to tests/Support/SupportCollectionTest.php From db2bb943149e011bbe2f5696f0db8169f226ace9 Mon Sep 17 00:00:00 2001 From: lukeraymonddowning Date: Thu, 11 Apr 2024 20:45:55 +0100 Subject: [PATCH 4/4] [11.x] Adds support for enums on `mapInto` collection method. --- src/Illuminate/Collections/Traits/EnumeratesValues.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index ccd128caf9fb..1b0f63fdf048 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -2,6 +2,7 @@ namespace Illuminate\Support\Traits; +use BackedEnum; use CachingIterator; use Closure; use Exception; @@ -410,7 +411,7 @@ public function flatMap(callable $callback) */ public function mapInto($class) { - if (enum_exists($class)) { + if (is_subclass_of($class, BackedEnum::class)) { return $this->map(fn ($value, $key) => $class::from($value)); }