GeneratedLink.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Drupal\Core;
  3. use Drupal\Component\Render\MarkupInterface;
  4. use Drupal\Component\Utility\Unicode;
  5. use Drupal\Core\Render\BubbleableMetadata;
  6. /**
  7. * Used to return generated links, along with associated cacheability metadata.
  8. *
  9. * Note: not to be confused with \Drupal\Core\Link, which is for passing around
  10. * ungenerated links (typically link text + route name + route parameters).
  11. */
  12. class GeneratedLink extends BubbleableMetadata implements MarkupInterface, \Countable {
  13. /**
  14. * HTML tag to use when building the link.
  15. */
  16. const TAG = 'a';
  17. /**
  18. * The HTML string value containing a link.
  19. *
  20. * @var string
  21. */
  22. protected $generatedLink = '';
  23. /**
  24. * Gets the generated link.
  25. *
  26. * @return string
  27. */
  28. public function getGeneratedLink() {
  29. return $this->generatedLink;
  30. }
  31. /**
  32. * Sets the generated link.
  33. *
  34. * @param string $generated_link
  35. * The generated link.
  36. *
  37. * @return $this
  38. */
  39. public function setGeneratedLink($generated_link) {
  40. $this->generatedLink = $generated_link;
  41. return $this;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function __toString() {
  47. return (string) $this->generatedLink;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function jsonSerialize() {
  53. return $this->__toString();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function count() {
  59. return Unicode::strlen($this->__toString());
  60. }
  61. }