blob: 710e3d910a540d68431c50dbd14822c7b414ae27 (
plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<?php
namespace ipl\Html;
use Exception;
/**
* A text node
*
* Primitive element that renders text to HTML while automatically escaping its content.
* If the passed content is already escaped, see {@link setEscaped()} to indicate this.
*/
class Text implements ValidHtml
{
/** @var string */
protected $content;
/** @var bool Whether the content is already escaped */
protected $escaped = false;
/**
* Create a new text node
*
* @param string $content
*/
public function __construct($content)
{
$this->setContent($content);
}
/**
* Create a new text node
*
* @param string $content
*
* @return static
*/
public static function create($content)
{
return new static($content);
}
/**
* Get the content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set the content
*
* @param string $content
*
* @return $this
*/
public function setContent($content)
{
$this->content = (string) $content;
return $this;
}
/**
* Get whether the content promises to be already escaped
*
* @return bool
*/
public function isEscaped()
{
return $this->escaped;
}
/**
* Set whether the content is already escaped
*
* @param bool $escaped
*
* @return $this
*/
public function setEscaped($escaped = true)
{
$this->escaped = $escaped;
return $this;
}
/**
* Render text to HTML when treated like a string
*
* Calls {@link render()} internally in order to render the text to HTML.
* Exceptions will be automatically caught and returned as HTML string as well using {@link Error::render()}.
*
* @return string
*/
public function __toString()
{
try {
return $this->render();
} catch (Exception $e) {
return Error::render($e);
}
}
public function render()
{
if ($this->escaped) {
return $this->content;
} else {
return Html::escape($this->content);
}
}
}
|