HtmlEscapedText.php 1016 B

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