Repository navigation
Expand file tree
/
Copy pathEncodedHtmlString.php
More file actions
100 lines (86 loc) · 2.51 KB
/
Copy pathEncodedHtmlString.php
File metadata and controls
100 lines (86 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Illuminate\Support;
use BackedEnum;
use Illuminate\Contracts\Support\DeferringDisplayableValue;
use Illuminate\Contracts\Support\Htmlable;
class EncodedHtmlString extends HtmlString
{
/**
* The HTML string.
*
* @var \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null
*/
protected $html;
/**
* The callback that should be used to encode the HTML strings.
*
* @var callable|null
*/
protected static $encodeUsingFactory;
/**
* Create a new encoded HTML string instance.
*
* @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null $html
* @param bool $doubleEncode
*/
public function __construct($html = '', protected bool $doubleEncode = true)
{
parent::__construct($html);
}
/**
* Convert the special characters in the given value.
*
* @internal
*
* @param string|null $value
* @param bool $withQuote
* @param bool $doubleEncode
* @return string
*/
public static function convert($value, bool $withQuote = true, bool $doubleEncode = true)
{
$flag = $withQuote ? ENT_QUOTES : ENT_NOQUOTES;
return htmlspecialchars($value ?? '', $flag | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
}
/**
* Get the HTML string.
*
* @return string
*/
#[\Override]
public function toHtml()
{
$value = $this->html;
if ($value instanceof DeferringDisplayableValue) {
$value = $value->resolveDisplayableValue();
}
if ($value instanceof Htmlable) {
return $value->toHtml();
}
if ($value instanceof BackedEnum) {
$value = $value->value;
}
return (static::$encodeUsingFactory ?? function ($value, $doubleEncode) {
return static::convert($value, doubleEncode: $doubleEncode);
})($value, $this->doubleEncode);
}
/**
* Set the callable that will be used to encode the HTML strings.
*
* @param callable|null $factory
* @return void
*/
public static function encodeUsing(?callable $factory = null)
{
static::$encodeUsingFactory = $factory;
}
/**
* Flush the class's global state.
*
* @return void
*/
public static function flushState()
{
static::$encodeUsingFactory = null;
}
}