From caa5b8b8fdb5002ff529fbd9322d1a6d9b4943f5 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 3 Oct 2022 19:57:52 +0100 Subject: [PATCH 1/5] Allows encode HTML special characters in a enum value --- src/Illuminate/Support/helpers.php | 5 +++++ tests/Support/SupportHelpersTest.php | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 65d69c91a6a1..b632f49305c4 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -6,6 +6,7 @@ use Illuminate\Support\Env; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Optional; +use Illuminate\Support\Reflector; use Illuminate\Support\Str; if (! function_exists('append_config')) { @@ -116,6 +117,10 @@ function e($value, $doubleEncode = true) return $value->toHtml(); } + if (is_object($value) && enum_exists($value::class)) { + $value = $value->value; + } + return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode); } } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 0b0352be81bf..8dc04a7827ef 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -29,9 +29,13 @@ public function testE() { $str = 'A \'quote\' is bold'; $this->assertSame('A 'quote' is <b>bold</b>', e($str)); + $html = m::mock(Htmlable::class); $html->shouldReceive('toHtml')->andReturn($str); $this->assertEquals($str, e($html)); + + $enumValue = SupportTestEnum::ADMIN; + $this->assertSame('Admin', e($enumValue)); } public function testBlank() @@ -935,6 +939,11 @@ public function getIterator(): Traversable } } +enum SupportTestEnum: string +{ + case ADMIN = 'Admin'; +} + class SupportTestCountable implements Countable { public function count(): int From 03325360426e584041cc2699bd9fcd753f77202b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 3 Oct 2022 18:58:09 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- src/Illuminate/Support/helpers.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index b632f49305c4..1c280a25e4e2 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -6,7 +6,6 @@ use Illuminate\Support\Env; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Optional; -use Illuminate\Support\Reflector; use Illuminate\Support\Str; if (! function_exists('append_config')) { From e4f2557b0ef0effc2c117fe73d017aadde7fca12 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 4 Oct 2022 08:46:47 +0100 Subject: [PATCH 3/5] Improves condition --- src/Illuminate/Support/helpers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 1c280a25e4e2..2e62e1490677 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -102,7 +102,7 @@ function class_uses_recursive($class) /** * Encode HTML special characters in a string. * - * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|string|null $value + * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|null $value * @param bool $doubleEncode * @return string */ @@ -116,7 +116,7 @@ function e($value, $doubleEncode = true) return $value->toHtml(); } - if (is_object($value) && enum_exists($value::class)) { + if ($value instanceof BackedEnum) { $value = $value->value; } From 89fe7edcb6903664ed5ccefd203015d6e56328fa Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 4 Oct 2022 08:56:38 +0100 Subject: [PATCH 4/5] Adds more tests --- tests/Support/SupportHelpersTest.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 8dc04a7827ef..7d967ad6eef8 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -34,8 +34,11 @@ public function testE() $html->shouldReceive('toHtml')->andReturn($str); $this->assertEquals($str, e($html)); - $enumValue = SupportTestEnum::ADMIN; - $this->assertSame('Admin', e($enumValue)); + $enumValue = SupportTestStringEnum::ADMIN_LABEL; + $this->assertSame('I am 'admin'', e($enumValue)); + + $enumValue = SupportTestIntEnum::ROLE_ADMIN; + $this->assertSame('1', e($enumValue)); } public function testBlank() @@ -939,9 +942,14 @@ public function getIterator(): Traversable } } -enum SupportTestEnum: string +enum SupportTestStringEnum: string +{ + case ADMIN_LABEL = 'I am \'admin\''; +} + +enum SupportTestIntEnum: int { - case ADMIN = 'Admin'; + case ROLE_ADMIN = 1; } class SupportTestCountable implements Countable From 28ac9f6808f2a480eced17b55b1ba2c8db5405ba Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 4 Oct 2022 09:14:50 +0100 Subject: [PATCH 5/5] Updates tests for PHP 8.1 --- tests/Support/Fixtures/IntBackedEnum.php | 8 ++++++++ tests/Support/Fixtures/StringBackedEnum.php | 8 ++++++++ tests/Support/SupportHelpersTest.php | 22 ++++++++++----------- 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 tests/Support/Fixtures/IntBackedEnum.php create mode 100644 tests/Support/Fixtures/StringBackedEnum.php diff --git a/tests/Support/Fixtures/IntBackedEnum.php b/tests/Support/Fixtures/IntBackedEnum.php new file mode 100644 index 000000000000..dc1edc083c08 --- /dev/null +++ b/tests/Support/Fixtures/IntBackedEnum.php @@ -0,0 +1,8 @@ +shouldReceive('toHtml')->andReturn($str); $this->assertEquals($str, e($html)); + } - $enumValue = SupportTestStringEnum::ADMIN_LABEL; + /** + * @requires PHP >= 8.1 + */ + public function testEWithEnums() + { + $enumValue = StringBackedEnum::ADMIN_LABEL; $this->assertSame('I am 'admin'', e($enumValue)); - $enumValue = SupportTestIntEnum::ROLE_ADMIN; + $enumValue = IntBackedEnum::ROLE_ADMIN; $this->assertSame('1', e($enumValue)); } @@ -942,16 +950,6 @@ public function getIterator(): Traversable } } -enum SupportTestStringEnum: string -{ - case ADMIN_LABEL = 'I am \'admin\''; -} - -enum SupportTestIntEnum: int -{ - case ROLE_ADMIN = 1; -} - class SupportTestCountable implements Countable { public function count(): int