HtmlEscapedText.php 972 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Drupal\Component\Render;
  3. use Drupal\Component\Utility\Html;
  4. /**
  5. * Escapes HTML syntax characters to HTML entities for display in markup.
  6. *
  7. * This class can be used to provide theme engine-like late escaping
  8. * functionality.
  9. *
  10. * @ingroup sanitization
  11. */
  12. class HtmlEscapedText implements MarkupInterface, \Countable {
  13. /**
  14. * The string to escape.
  15. *
  16. * @var string
  17. */
  18. protected $string;
  19. /**
  20. * Constructs an HtmlEscapedText object.
  21. *
  22. * @param $string
  23. * The string to escape. This value will be cast to a string.
  24. */
  25. public function __construct($string) {
  26. $this->string = (string) $string;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function __toString() {
  32. return Html::escape($this->string);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function count() {
  38. return mb_strlen($this->string);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function jsonSerialize() {
  44. return $this->__toString();
  45. }
  46. }