Skip to content

Commit fcdce7c

Browse files
[9.x] Adds support for PHP's BackedEnum to be "rendered" on blade views. (#44445)
* Allows encode HTML special characters in a enum value * Apply fixes from StyleCI * Improves condition * Adds more tests * Updates tests for PHP 8.1 Co-authored-by: StyleCI Bot <[email protected]>
1 parent 89db9e5 commit fcdce7c

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/Illuminate/Support/helpers.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function class_uses_recursive($class)
102102
/**
103103
* Encode HTML special characters in a string.
104104
*
105-
* @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|string|null $value
105+
* @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|null $value
106106
* @param bool $doubleEncode
107107
* @return string
108108
*/
@@ -116,6 +116,10 @@ function e($value, $doubleEncode = true)
116116
return $value->toHtml();
117117
}
118118

119+
if ($value instanceof BackedEnum) {
120+
$value = $value->value;
121+
}
122+
119123
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
120124
}
121125
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support\Fixtures;
4+
5+
enum IntBackedEnum: int
6+
{
7+
case ROLE_ADMIN = 1;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support\Fixtures;
4+
5+
enum StringBackedEnum: string
6+
{
7+
case ADMIN_LABEL = 'I am \'admin\'';
8+
}

tests/Support/SupportHelpersTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Illuminate\Support\Env;
1010
use Illuminate\Support\Optional;
1111
use Illuminate\Support\Stringable;
12+
use Illuminate\Tests\Support\Fixtures\IntBackedEnum;
13+
use Illuminate\Tests\Support\Fixtures\StringBackedEnum;
1214
use IteratorAggregate;
1315
use LogicException;
1416
use Mockery as m;
@@ -29,11 +31,24 @@ public function testE()
2931
{
3032
$str = 'A \'quote\' is <b>bold</b>';
3133
$this->assertSame('A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;', e($str));
34+
3235
$html = m::mock(Htmlable::class);
3336
$html->shouldReceive('toHtml')->andReturn($str);
3437
$this->assertEquals($str, e($html));
3538
}
3639

40+
/**
41+
* @requires PHP >= 8.1
42+
*/
43+
public function testEWithEnums()
44+
{
45+
$enumValue = StringBackedEnum::ADMIN_LABEL;
46+
$this->assertSame('I am &#039;admin&#039;', e($enumValue));
47+
48+
$enumValue = IntBackedEnum::ROLE_ADMIN;
49+
$this->assertSame('1', e($enumValue));
50+
}
51+
3752
public function testBlank()
3853
{
3954
$this->assertTrue(blank(null));

0 commit comments

Comments
 (0)