Link.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Page\Medium;
  9. class Link implements RenderableInterface
  10. {
  11. use ParsedownHtmlTrait;
  12. /**
  13. * @var array
  14. */
  15. protected $attributes = [];
  16. protected $source;
  17. /**
  18. * Construct.
  19. * @param array $attributes
  20. * @param Medium $medium
  21. */
  22. public function __construct(array $attributes, Medium $medium)
  23. {
  24. $this->attributes = $attributes;
  25. $this->source = $medium->reset()->thumbnail('auto')->display('thumbnail');
  26. $this->source->linked = true;
  27. }
  28. /**
  29. * Get an element (is array) that can be rendered by the Parsedown engine
  30. *
  31. * @param string $title
  32. * @param string $alt
  33. * @param string $class
  34. * @param string $id
  35. * @param bool $reset
  36. * @return array
  37. */
  38. public function parsedownElement($title = null, $alt = null, $class = null, $id = null, $reset = true)
  39. {
  40. $innerElement = $this->source->parsedownElement($title, $alt, $class, $id, $reset);
  41. return [
  42. 'name' => 'a',
  43. 'attributes' => $this->attributes,
  44. 'handler' => is_string($innerElement) ? 'line' : 'element',
  45. 'text' => $innerElement
  46. ];
  47. }
  48. /**
  49. * Forward the call to the source element
  50. *
  51. * @param string $method
  52. * @param mixed $args
  53. * @return mixed
  54. */
  55. public function __call($method, $args)
  56. {
  57. $this->source = call_user_func_array(array($this->source, $method), $args);
  58. // Don't start nesting links, if user has multiple link calls in his
  59. // actions, we will drop the previous links.
  60. return $this->source instanceof Link ? $this->source : $this;
  61. }
  62. }