summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/Text.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:38:04 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:38:04 +0000
commit1ff5c35de5dbd70a782875a91dd2232fd01b002b (patch)
tree77d9ce5e1bf78b3e6ef79f8f6e7861e2ced3c09b /vendor/ipl/html/src/Text.php
parentInitial commit. (diff)
downloadicinga-php-library-upstream/0.10.1.tar.xz
icinga-php-library-upstream/0.10.1.zip
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/html/src/Text.php')
-rw-r--r--vendor/ipl/html/src/Text.php116
1 files changed, 116 insertions, 0 deletions
diff --git a/vendor/ipl/html/src/Text.php b/vendor/ipl/html/src/Text.php
new file mode 100644
index 0000000..710e3d9
--- /dev/null
+++ b/vendor/ipl/html/src/Text.php
@@ -0,0 +1,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);
+ }
+ }
+}