GeneratedLink.php 1.3 KB

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