Debian Codegraph @

lib/3rdParty/composer/illuminate/support/EncodedHtmlString.php

raw symbols SCIP
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 * The HTML string.
13 *
14 * @var \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null
15 */
16 protected $html;
17
18 /**
19 * The callback that should be used to encode the HTML strings.
20 *
21 * @var callable|null
22 */
23 protected static $encodeUsingFactory;
24
25 /**
26 * Create a new encoded HTML string instance.
27 *
28 * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null $html
29 * @param bool $doubleEncode
30 */
31 public function __construct($html = '', protected bool $doubleEncode = true)
32 {
33 parent::__construct($html);
34 }
35
36 /**
37 * Convert the special characters in the given value.
38 *
39 * @internal
40 *
41 * @param string|null $value
42 * @param int $withQuote
43 * @param bool $doubleEncode
44 * @return string
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 * Get the HTML string.
55 *
56 * @return string
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 * Set the callable that will be used to encode the HTML strings.
82 *
83 * @param callable|null $factory
84 * @return void
85 */
86 public static function encodeUsing(?callable $factory = null)
87 {
88 static::$encodeUsingFactory = $factory;
89 }
90
91 /**
92 * Flush the class's global state.
93 *
94 * @return void
95 */
96 public static function flushState()
97 {
98 static::$encodeUsingFactory = null;
99 }
100 }