| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use BackedEnum; |
| 6 |
use Illuminate\Contracts\Support\DeferringDisplayableValue; |
| 7 |
use Illuminate\Contracts\Support\Htmlable; |
| 8 |
|
| 9 |
class EncodedHtmlString extends HtmlString |
| 10 |
{ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
protected $html; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected static $encodeUsingFactory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function __construct($html = '', protected bool $doubleEncode = true) |
| 32 |
{ |
| 33 |
parent::__construct($html); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static function convert($value, bool $withQuote = true, bool $doubleEncode = true) |
| 47 |
{ |
| 48 |
$flag = $withQuote ? ENT_QUOTES : ENT_NOQUOTES; |
| 49 |
|
| 50 |
return htmlspecialchars($value ?? '', $flag | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[\Override] |
| 59 |
public function toHtml() |
| 60 |
{ |
| 61 |
$value = $this->html; |
| 62 |
|
| 63 |
if ($value instanceof DeferringDisplayableValue) { |
| 64 |
$value = $value->resolveDisplayableValue(); |
| 65 |
} |
| 66 |
|
| 67 |
if ($value instanceof Htmlable) { |
| 68 |
return $value->toHtml(); |
| 69 |
} |
| 70 |
|
| 71 |
if ($value instanceof BackedEnum) { |
| 72 |
$value = $value->value; |
| 73 |
} |
| 74 |
|
| 75 |
return (static::$encodeUsingFactory ?? function ($value, $doubleEncode) { |
| 76 |
return static::convert($value, doubleEncode: $doubleEncode); |
| 77 |
})($value, $this->doubleEncode); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static function encodeUsing(?callable $factory = null) |
| 87 |
{ |
| 88 |
static::$encodeUsingFactory = $factory; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static function flushState() |
| 97 |
{ |
| 98 |
static::$encodeUsingFactory = null; |
| 99 |
} |
| 100 |
} |