MarkupGenerator.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. private $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. return preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $text, $this->match);
  32. }
  33. /**
  34. * @param $url
  35. * @return mixed
  36. */
  37. public function parseMarkup($url) {
  38. $url = strpos($url, 'http://') ? 'http://' . $url : $url;
  39. $this->parsedMarkup = Embed::create($url);
  40. return true;
  41. }
  42. /**
  43. * @param $url
  44. * @return mixed
  45. */
  46. public function generatePreview() {
  47. if (!$this->parsedMarkup) {
  48. return null;
  49. }
  50. $templateCreator = new TemplateCreator();
  51. $templateCreator->generateTitle($this->parsedMarkup->title);
  52. $templateCreator->generateDescription($this->parsedMarkup->description);
  53. $templateCreator->generateImage($this->parsedMarkup->image);
  54. return $templateCreator->getPreview();
  55. }
  56. /**
  57. * @return mixed
  58. */
  59. public function getImages() {
  60. return $this->parsedMarkup->images;
  61. }
  62. /**
  63. * @return mixed
  64. */
  65. public function getImage() {
  66. return $this->parsedMarkup->image;
  67. }
  68. /**
  69. * @return mixed
  70. */
  71. public function getTitle() {
  72. return $this->parsedMarkup->title;
  73. }
  74. /**
  75. * @return mixed
  76. */
  77. public function getDescription() {
  78. return $this->parsedMarkup->description;
  79. }
  80. /**
  81. * @return mixed
  82. */
  83. public function getTags() {
  84. return $this->parsedMarkup->tags;
  85. }
  86. }