diff --git a/README.md b/README.md index be8794f..2fdb063 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A PHP library to easily get website information (title, description, image...) f ## Dependencies -* PHP >= 5.3 +* PHP >= 5.4 * Guzzle ## Installation diff --git a/src/LinkPreview/LinkPreview.php b/src/LinkPreview/LinkPreview.php index 41ae42c..f43eb0e 100644 --- a/src/LinkPreview/LinkPreview.php +++ b/src/LinkPreview/LinkPreview.php @@ -7,25 +7,26 @@ use LinkPreview\Parser\ParserInterface; use LinkPreview\Parser\YoutubeParser; +/** + * Class LinkPreview + */ class LinkPreview { - /** - * @var string $url - */ - private $url; - /** * @var ParserInterface[] */ - private $parsers = array(); - + private $parsers = []; /** * @var boolean */ private $propagation = false; + /** + * @var string $url + */ + private $url; /** - * @param string|null $url + * @param string $url */ public function __construct($url = null) { @@ -35,29 +36,68 @@ public function __construct($url = null) } /** - * Set website url to a general model - * - * @param string $url Website url to parse information from + * Add parser to the beginning of parsers list + * @param ParserInterface $parser * @return $this */ - public function setUrl($url) + public function addParser(ParserInterface $parser) { - $this->url = $url; + $this->parsers = [(string) $parser => $parser] + $this->parsers; return $this; } /** - * @return string + * Get parsed model array with parser name as a key + * @return LinkInterface[] */ - public function getUrl() + public function getParsed() { - return $this->url; + $parsed = []; + + $parsers = $this->getParsers(); + if (0 === count($parsers)) { + $this->addDefaultParsers(); + } + + foreach ($this->getParsers() as $name => $parser) { + $parser->getLink()->setUrl($this->getUrl()); + + if ($parser->isValidParser()) { + $parsed[$name] = $parser->parseLink(); + + if (!$this->getPropagation()) { + break; + } + } + } + + return $parsed; + } + + /** + * Get parsers + * @return ParserInterface[] + */ + public function getParsers() + { + return $this->parsers; + } + + /** + * Set parsers + * @param ParserInterface[] $parsers + * @return $this + */ + public function setParsers($parsers) + { + $this->parsers = $parsers; + + return $this; } /** * Get propagation - * * @return boolean */ public function getPropagation() @@ -69,7 +109,6 @@ public function getPropagation() * Set propagation for parsing. * If propagation is set to false, then parsing stops after first successful parsing. * By default it is set as false. - * * @param boolean $propagation * @return $this */ @@ -81,85 +120,39 @@ public function setPropagation($propagation) } /** - * Get parsers - * - * @return ParserInterface[] - */ - public function getParsers() - { - return $this->parsers; - } - - /** - * Set parsers - * - * @param ParserInterface[] $parsers - * @return $this + * @return string */ - public function setParsers($parsers) + public function getUrl() { - $this->parsers = $parsers; - - return $this; + return $this->url; } /** - * Add parser to the beginning of parsers list - * - * @param ParserInterface $parser + * Set website url to a general model + * @param string $url Website url to parse information from * @return $this */ - public function addParser(ParserInterface $parser) + public function setUrl($url) { - $this->parsers = array($parser->__toString() => $parser) + $this->parsers; + $this->url = $url; return $this; } /** * Remove parser from parsers list - * * @param string $name Parser name * @return $this */ public function removeParser($name) { - if (in_array($name, $this->parsers)) { + if (in_array($name, $this->parsers, false)) { unset($this->parsers[$name]); } return $this; } - /** - * Get parsed model array with parser name as a key - * - * @return LinkInterface[] - */ - public function getParsed() - { - $parsed = array(); - - $parsers = $this->getParsers(); - if (empty($parsers)) { - $this->addDefaultParsers(); - } - - foreach ($this->getParsers() as $name => $parser) { - $parser->getLink()->setUrl($this->getUrl()); - - if ($parser->isValidParser()) { - $parsed[$name] = $parser->parseLink(); - - if (!$this->getPropagation()) { - break; - } - } - } - - return $parsed; - } - /** * Add default parsers */ diff --git a/src/LinkPreview/Model/Link.php b/src/LinkPreview/Model/Link.php index 2f6ed95..6b93f77 100644 --- a/src/LinkPreview/Model/Link.php +++ b/src/LinkPreview/Model/Link.php @@ -2,66 +2,64 @@ namespace LinkPreview\Model; +/** + * Class Link + */ class Link implements LinkInterface { /** - * @var string $url - */ - private $url; - - /** - * @var string $realUrl + * @var string $content Website content */ - private $realUrl; - + private $content; /** - * @var string $title Link title + * @var string $contentType Website content type */ - private $title; - + private $contentType; /** * @var string $description Link description */ private $description; - /** * @var string $image Url to image */ private $image; - /** - * @var string $content Website content + * @var string $realUrl */ - private $content; - + private $realUrl; /** - * @var string $contentType Website content type + * @var string $title Link title */ - private $contentType; + private $title; + /** + * @var string $url + */ + private $url; + /** + * @param string $url + */ public function __construct($url = null) { if (null !== $url) { $this->setUrl($url); } - - return $this; } /** * @inheritdoc */ - public function getTitle() + public function getContent() { - return $this->title; + return $this->content; } /** * @inheritdoc */ - public function setTitle($title) + public function setContent($content) { - $this->title = $title; + $this->content = $content; return $this; } @@ -69,17 +67,17 @@ public function setTitle($title) /** * @inheritdoc */ - public function getImage() + public function getContentType() { - return $this->image; + return $this->contentType; } /** * @inheritdoc */ - public function setImage($image) + public function setContentType($contentType) { - $this->image = $image; + $this->contentType = $contentType; return $this; } @@ -105,17 +103,17 @@ public function setDescription($description) /** * @inheritdoc */ - public function getUrl() + public function getImage() { - return $this->url; + return $this->image; } /** * @inheritdoc */ - public function setUrl($url) + public function setImage($image) { - $this->url = $url; + $this->image = $image; return $this; } @@ -141,17 +139,17 @@ public function setRealUrl($realUrl) /** * @inheritdoc */ - public function getContent() + public function getTitle() { - return $this->content; + return $this->title; } /** * @inheritdoc */ - public function setContent($content) + public function setTitle($title) { - $this->content = $content; + $this->title = $title; return $this; } @@ -159,17 +157,17 @@ public function setContent($content) /** * @inheritdoc */ - public function getContentType() + public function getUrl() { - return $this->contentType; + return $this->url; } /** * @inheritdoc */ - public function setContentType($contentType) + public function setUrl($url) { - $this->contentType = $contentType; + $this->url = $url; return $this; } diff --git a/src/LinkPreview/Model/LinkInterface.php b/src/LinkPreview/Model/LinkInterface.php index cac410d..fe84a87 100644 --- a/src/LinkPreview/Model/LinkInterface.php +++ b/src/LinkPreview/Model/LinkInterface.php @@ -2,51 +2,55 @@ namespace LinkPreview\Model; +/** + * Interface LinkInterface + */ interface LinkInterface { /** - * Set website url - * - * @param string $url - * @return $this + * Get source code + * @return string */ - public function setUrl($url); + public function getContent(); /** - * Set real url after all redirects - * - * @param string $realUrl - * @return $this + * Get source content type (example: text/html, image/jpg) + * @return string */ - public function setRealUrl($realUrl); + public function getContentType(); /** - * Set title - * - * @param string $title - * @return $this + * Get description + * @return string */ - public function setTitle($title); + public function getDescription(); /** - * Set description - * - * @param string $description - * @return $this + * Get image url + * @return string */ - public function setDescription($description); + public function getImage(); /** - * Set image url - * - * @param string $image - * @return $this + * Get real url after all redirects + * @return string */ - public function setImage($image); + public function getRealUrl(); + + /** + * Get title + * @return string + */ + public function getTitle(); + + /** + * Get website url + * @return string + */ + public function getUrl(); /** * Set source code - * * @param string $content * @return $this */ @@ -54,58 +58,43 @@ public function setContent($content); /** * Set source content type (example: text/html, image/jpg) - * * @param string $contentType * @return $this */ public function setContentType($contentType); /** - * Get website url - * - * @return string - */ - public function getUrl(); - - /** - * Get real url after all redirects - * - * @return string - */ - public function getRealUrl(); - - /** - * Get title - * - * @return string + * Set description + * @param string $description + * @return $this */ - public function getTitle(); + public function setDescription($description); /** - * Get description - * - * @return string + * Set image url + * @param string $image + * @return $this */ - public function getDescription(); + public function setImage($image); /** - * Get image url - * - * @return string + * Set real url after all redirects + * @param string $realUrl + * @return $this */ - public function getImage(); + public function setRealUrl($realUrl); /** - * Get source code - * - * @return string + * Set title + * @param string $title + * @return $this */ - public function getContent(); + public function setTitle($title); /** - * Get source content type (example: text/html, image/jpg) - * - * @return string + * Set website url + * @param string $url + * @return $this */ - public function getContentType(); -} \ No newline at end of file + public function setUrl($url); +} \ No newline at end of file diff --git a/src/LinkPreview/Model/VideoLink.php b/src/LinkPreview/Model/VideoLink.php index 45691df..a45a1c2 100644 --- a/src/LinkPreview/Model/VideoLink.php +++ b/src/LinkPreview/Model/VideoLink.php @@ -2,18 +2,19 @@ namespace LinkPreview\Model; +/** + * Class VideoLink + */ class VideoLink extends Link { - /** - * @var string $video Url to video - */ - private $video; - /** * @var string $embedCode Video embed code */ private $embedCode; - + /** + * @var string $video Url to video + */ + private $video; /** * @var string $videoId Video identification code */ @@ -22,18 +23,18 @@ class VideoLink extends Link /** * @return string */ - public function getVideo() + public function getEmbedCode() { - return $this->video; + return $this->embedCode; } /** - * @param string $video + * @param string $embedCode * @return $this */ - public function setVideo($video) + public function setEmbedCode($embedCode) { - $this->video = $video; + $this->embedCode = $embedCode; return $this; } @@ -41,18 +42,18 @@ public function setVideo($video) /** * @return string */ - public function getEmbedCode() + public function getVideo() { - return $this->embedCode; + return $this->video; } /** - * @param string $embedCode + * @param string $video * @return $this */ - public function setEmbedCode($embedCode) + public function setVideo($video) { - $this->embedCode = $embedCode; + $this->video = $video; return $this; } @@ -72,6 +73,4 @@ public function setVideoId($videoId) { $this->videoId = $videoId; } - - } \ No newline at end of file diff --git a/src/LinkPreview/Parser/GeneralParser.php b/src/LinkPreview/Parser/GeneralParser.php index adbc2bd..fed5d38 100644 --- a/src/LinkPreview/Parser/GeneralParser.php +++ b/src/LinkPreview/Parser/GeneralParser.php @@ -7,6 +7,9 @@ use LinkPreview\Reader\GeneralReader; use LinkPreview\Reader\ReaderInterface; +/** + * Class GeneralParser + */ class GeneralParser implements ParserInterface { /** @@ -37,6 +40,10 @@ class GeneralParser implements ParserInterface */ private $reader; + /** + * @param ReaderInterface $reader + * @param LinkInterface $link + */ public function __construct(ReaderInterface $reader = null, LinkInterface $link = null) { if (null !== $reader) { @@ -61,40 +68,40 @@ public function __toString() } /** - * @return ReaderInterface + * @inheritdoc */ - public function getReader() + public function getLink() { - return $this->reader; + return $this->link; } /** - * @param ReaderInterface $reader - * @return $this + * @inheritdoc */ - public function setReader(ReaderInterface $reader) + public function setLink(LinkInterface $link) { - $this->reader = $reader; + $this->link = $link; return $this; } /** - * @inheritdoc + * @return ReaderInterface */ - public function setLink(LinkInterface $link) + public function getReader() { - $this->link = $link; - - return $this; + return $this->reader; } /** - * @inheritdoc + * @param ReaderInterface $reader + * @return $this */ - public function getLink() + public function setReader(ReaderInterface $reader) { - return $this->link; + $this->reader = $reader; + + return $this; } /** @@ -113,12 +120,6 @@ public function isValidParser() return $isValid; } - private function readLink() - { - $reader = $this->getReader()->setLink($this->getLink()); - $this->setLink($reader->readLink()); - } - /** * @inheritdoc */ @@ -143,17 +144,16 @@ public function parseLink() /** * Extract required data from html source - * * @param $html * @return array */ protected function parseHtml($html) { - $data = array( + $data = [ 'image' => '', 'title' => '', - 'description' => '', - ); + 'description' => '' + ]; libxml_use_internal_errors(true); $doc = new \DOMDocument(); @@ -161,26 +161,26 @@ protected function parseHtml($html) /** @var \DOMElement $meta */ foreach ($doc->getElementsByTagName('meta') as $meta) { - if ($meta->getAttribute('itemprop') == 'image') { + if ($meta->getAttribute('itemprop') === 'image') { $data['image'] = $meta->getAttribute('content'); - } elseif ($meta->getAttribute('property') == 'og:image') { + } elseif ($meta->getAttribute('property') === 'og:image') { $data['image'] = $meta->getAttribute('content'); - } elseif ($meta->getAttribute('property') == 'twitter:image') { + } elseif ($meta->getAttribute('property') === 'twitter:image') { $data['image'] = $meta->getAttribute('value'); } - if ($meta->getAttribute('itemprop') == 'name') { + if ($meta->getAttribute('itemprop') === 'name') { $data['title'] = $meta->getAttribute('content'); - } elseif ($meta->getAttribute('property') == 'og:title') { + } elseif ($meta->getAttribute('property') === 'og:title') { $data['title'] = $meta->getAttribute('content'); - } elseif ($meta->getAttribute('property') == 'twitter:title') { + } elseif ($meta->getAttribute('property') === 'twitter:title') { $data['title'] = $meta->getAttribute('value'); } - if ($meta->getAttribute('itemprop') == 'description') { + if ($meta->getAttribute('itemprop') === 'description') { $data['title'] = $meta->getAttribute('content'); } - if ($meta->getAttribute('property') == 'og:description') { + if ($meta->getAttribute('property') === 'og:description') { $data['description'] = $meta->getAttribute('content'); } } @@ -194,7 +194,7 @@ protected function parseHtml($html) if (empty($data['description'])) { foreach ($doc->getElementsByTagName('meta') as $meta) { - if ($meta->getAttribute('name') == 'description') { + if ($meta->getAttribute('name') === 'description') { $data['description'] = $meta->getAttribute('content'); } } @@ -202,4 +202,13 @@ protected function parseHtml($html) return $data; } + + /** + * Read link + */ + private function readLink() + { + $reader = $this->getReader()->setLink($this->getLink()); + $this->setLink($reader->readLink()); + } } \ No newline at end of file diff --git a/src/LinkPreview/Parser/ParserInterface.php b/src/LinkPreview/Parser/ParserInterface.php index 106c17d..181ce99 100644 --- a/src/LinkPreview/Parser/ParserInterface.php +++ b/src/LinkPreview/Parser/ParserInterface.php @@ -5,65 +5,59 @@ use LinkPreview\Model\LinkInterface; use LinkPreview\Reader\ReaderInterface; +/** + * Interface ParserInterface + */ interface ParserInterface { /** * Set default reader and model - * - * * @param ReaderInterface $reader - * @param LinkInterface $link + * @param LinkInterface $link */ public function __construct(ReaderInterface $reader = null, LinkInterface $link = null); /** * Parser name - * * @return string */ public function __toString(); /** - * Set reader - * - * @param ReaderInterface $reader - * @return $this + * Get model + * @return LinkInterface */ - public function setReader(ReaderInterface $reader); + public function getLink(); /** * Get reader - * * @return ReaderInterface */ public function getReader(); /** - * Set model - * - * @param LinkInterface $link - * @return $this + * Check if parser is valid to parse for a given link + * @return boolean */ - public function setLink(LinkInterface $link); + public function isValidParser(); /** - * Get model - * + * Parse link * @return LinkInterface */ - public function getLink(); + public function parseLink(); /** - * Check if parser is valid to parse for a given link - * - * @return boolean + * Set model + * @param LinkInterface $link + * @return $this */ - public function isValidParser(); + public function setLink(LinkInterface $link); /** - * Parse link - * - * @return LinkInterface + * Set reader + * @param ReaderInterface $reader + * @return $this */ - public function parseLink(); -} \ No newline at end of file + public function setReader(ReaderInterface $reader); +} \ No newline at end of file diff --git a/src/LinkPreview/Parser/YoutubeParser.php b/src/LinkPreview/Parser/YoutubeParser.php index e8f6ac8..aaab6bb 100644 --- a/src/LinkPreview/Parser/YoutubeParser.php +++ b/src/LinkPreview/Parser/YoutubeParser.php @@ -7,6 +7,9 @@ use LinkPreview\Reader\GeneralReader; use LinkPreview\Reader\ReaderInterface; +/** + * Class YoutubeParser + */ class YoutubeParser implements ParserInterface { /** @@ -14,8 +17,6 @@ class YoutubeParser implements ParserInterface */ const PATTERN = '/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/'; - const YOUTUBE_API_URL = 'http://gdata.youtube.com/feeds/api/videos/[id]?v=2&alt=jsonc'; - /** * @var VideoLink $link */ @@ -26,6 +27,10 @@ class YoutubeParser implements ParserInterface */ private $reader; + /** + * @param ReaderInterface $reader + * @param LinkInterface $link + */ public function __construct(ReaderInterface $reader = null, LinkInterface $link = null) { if (null !== $reader) { @@ -50,40 +55,40 @@ public function __toString() } /** - * @return ReaderInterface + * @inheritdoc */ - public function getReader() + public function getLink() { - return $this->reader; + return $this->link; } /** - * @param ReaderInterface $reader - * @return $this + * @inheritdoc */ - public function setReader(ReaderInterface $reader) + public function setLink(LinkInterface $link) { - $this->reader = $reader; + $this->link = $link; return $this; } /** - * @inheritdoc + * @return ReaderInterface */ - public function setLink(LinkInterface $link) + public function getReader() { - $this->link = $link; - - return $this; + return $this->reader; } /** - * @inheritdoc + * @param ReaderInterface $reader + * @return $this */ - public function getLink() + public function setReader(ReaderInterface $reader) { - return $this->link; + $this->reader = $reader; + + return $this; } /** @@ -104,38 +109,66 @@ public function isValidParser() return $isValid; } - private function readLink() - { - $link = $this->getLink(); - $originalUrl = $link->getUrl(); - // change url to api link - $link->setUrl(str_replace('[id]', $link->getVideoId(), self::YOUTUBE_API_URL)); - - $reader = $this->getReader()->setLink($link); - $link = $reader->readLink(); - // change to original url after reading - $link->setUrl($originalUrl); - - $this->setLink($link); - } - /** * @inheritdoc */ public function parseLink() { $this->readLink(); - $link = $this->getLink(); - $data = json_decode($link->getContent()); + $htmlData = $this->parseHtml($link->getContent()); - $link->setTitle($data->data->title) - ->setDescription($data->data->description) - ->setImage($data->data->thumbnail->hqDefault) + $link->setTitle($htmlData['title']) + ->setDescription($htmlData['description']) + ->setImage($htmlData['image']) ->setEmbedCode( - '