MarkupGenerator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Drupal\statusmessage;
  3. use GuzzleHttp\Client;
  4. use Embed\Embed;
  5. use Drupal\statusmessage\TemplateCreator;
  6. /**
  7. * Class MarkupGenerator.
  8. *
  9. * @package Drupal\statusmessage
  10. */
  11. class MarkupGenerator implements Parser {
  12. public $match;
  13. public $parsedMarkup;
  14. /**
  15. * GuzzleHttp\Client definition.
  16. *
  17. * @var \GuzzleHttp\Client
  18. */
  19. protected $httpClient;
  20. /**
  21. * Constructs a new MarkupGenerator object.
  22. */
  23. // public function __construct(Client $http_client) {
  24. // $this->httpClient = $http_client;
  25. // }
  26. /**
  27. * @param $url
  28. * @return mixed
  29. */
  30. public function validateUrl($text) {
  31. preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $text, $this->match);
  32. return $this->match;
  33. }
  34. /**
  35. * @param $url
  36. * @return mixed
  37. */
  38. public function parseMarkup($url) {
  39. $url = strpos($url, 'http') ? 'http://' . $url : $url;
  40. $url = !is_array($url) ? $url : array_values($url)[0];
  41. $this->parsedMarkup = Embed::create($url);
  42. return true;
  43. }
  44. /**
  45. * @param $url
  46. * @return mixed
  47. */
  48. public function generatePreview() {
  49. if (!$this->parsedMarkup) {
  50. return null;
  51. }
  52. $templateCreator = new TemplateCreator();
  53. $templateCreator->generateTitle($this->parsedMarkup->title);
  54. $templateCreator->generateDescription($this->parsedMarkup->description);
  55. $templateCreator->generateImage($this->parsedMarkup->image);
  56. return $templateCreator->getPreview();
  57. }
  58. /**
  59. * @return mixed
  60. */
  61. public function getImages() {
  62. return $this->parsedMarkup->images;
  63. }
  64. /**
  65. * @return mixed
  66. */
  67. public function getImage() {
  68. return $this->parsedMarkup->image;
  69. }
  70. /**
  71. * @return mixed
  72. */
  73. public function getTitle() {
  74. return $this->parsedMarkup->title;
  75. }
  76. /**
  77. * @return mixed
  78. */
  79. public function getDescription() {
  80. return $this->parsedMarkup->description;
  81. }
  82. /**
  83. * @return mixed
  84. */
  85. public function getTags() {
  86. return $this->parsedMarkup->tags;
  87. }
  88. }