Skip to content

Commit

Permalink
[9.x] Adds support for PHP's BackedEnum to be "rendered" on blade v…
Browse files Browse the repository at this point in the history
…iews. (#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]>
  • Loading branch information
nunomaduro and StyleCIBot authored Oct 4, 2022
1 parent 89db9e5 commit fcdce7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -116,6 +116,10 @@ function e($value, $doubleEncode = true)
return $value->toHtml();
}

if ($value instanceof BackedEnum) {
$value = $value->value;
}

return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/Fixtures/IntBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Tests\Support\Fixtures;

enum IntBackedEnum: int
{
case ROLE_ADMIN = 1;
}
8 changes: 8 additions & 0 deletions tests/Support/Fixtures/StringBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Tests\Support\Fixtures;

enum StringBackedEnum: string
{
case ADMIN_LABEL = 'I am \'admin\'';
}
15 changes: 15 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Support\Env;
use Illuminate\Support\Optional;
use Illuminate\Support\Stringable;
use Illuminate\Tests\Support\Fixtures\IntBackedEnum;
use Illuminate\Tests\Support\Fixtures\StringBackedEnum;
use IteratorAggregate;
use LogicException;
use Mockery as m;
Expand All @@ -29,11 +31,24 @@ public function testE()
{
$str = 'A \'quote\' is <b>bold</b>';
$this->assertSame('A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;', e($str));

$html = m::mock(Htmlable::class);
$html->shouldReceive('toHtml')->andReturn($str);
$this->assertEquals($str, e($html));
}

/**
* @requires PHP >= 8.1
*/
public function testEWithEnums()
{
$enumValue = StringBackedEnum::ADMIN_LABEL;
$this->assertSame('I am &#039;admin&#039;', e($enumValue));

$enumValue = IntBackedEnum::ROLE_ADMIN;
$this->assertSame('1', e($enumValue));
}

public function testBlank()
{
$this->assertTrue(blank(null));
Expand Down

0 comments on commit fcdce7c

Please sign in to comment.