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(
- ''
+ ''
);
return $link;
}
+
+ /**
+ * Extract required data from html source
+ * @param $html
+ * @return array
+ */
+ protected function parseHtml($html)
+ {
+ $data = [
+ 'image' => '',
+ 'title' => '',
+ 'description' => ''
+ ];
+
+ libxml_use_internal_errors(true);
+ $doc = new \DOMDocument();
+ $doc->loadHTML($html);
+
+ /** @var \DOMElement $meta */
+ foreach ($doc->getElementsByTagName('meta') as $meta) {
+ if ($meta->getAttribute('property') === 'og:image') {
+ $data['image'] = $meta->getAttribute('content');
+ }
+
+ if ($meta->getAttribute('property') === 'og:title') {
+ $data['title'] = $meta->getAttribute('content');
+ }
+
+ if ($meta->getAttribute('property') === 'og:description') {
+ $data['description'] = $meta->getAttribute('content');
+ }
+ }
+
+ 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/Reader/GeneralReader.php b/src/LinkPreview/Reader/GeneralReader.php
index 7f42dc6..dc90cc3 100644
--- a/src/LinkPreview/Reader/GeneralReader.php
+++ b/src/LinkPreview/Reader/GeneralReader.php
@@ -5,35 +5,19 @@
use Guzzle\Http\Client;
use LinkPreview\Model\LinkInterface;
+/**
+ * Class GeneralReader
+ */
class GeneralReader implements ReaderInterface
{
- /**
- * @inheritdoc
- */
- private $link;
-
/**
* @var Client $client
*/
private $client;
-
- /**
- * @inheritdoc
- */
- public function setLink(LinkInterface $link)
- {
- $this->link = $link;
-
- return $this;
- }
-
/**
* @inheritdoc
*/
- public function getLink()
- {
- return $this->link;
- }
+ private $link;
/**
* @return Client
@@ -55,6 +39,24 @@ public function setClient($client)
$this->client = $client;
}
+ /**
+ * @inheritdoc
+ */
+ public function getLink()
+ {
+ return $this->link;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setLink(LinkInterface $link)
+ {
+ $this->link = $link;
+
+ return $this;
+ }
+
/**
* @inheritdoc
*/
@@ -72,4 +74,4 @@ public function readLink()
return $link;
}
-}
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/LinkPreview/Reader/ReaderInterface.php b/src/LinkPreview/Reader/ReaderInterface.php
index ab41d7b..3a51d9d 100644
--- a/src/LinkPreview/Reader/ReaderInterface.php
+++ b/src/LinkPreview/Reader/ReaderInterface.php
@@ -4,27 +4,27 @@
use LinkPreview\Model\LinkInterface;
+/**
+ * Interface ReaderInterface
+ */
interface ReaderInterface
{
- /**
- * Set model
- *
- * @param LinkInterface $link
- * @return $this
- */
- public function setLink(LinkInterface $link);
-
/**
* Get model
- *
* @return LinkInterface
*/
public function getLink();
/**
* Read and update model
- *
* @return LinkInterface
*/
public function readLink();
-}
\ No newline at end of file
+
+ /**
+ * Set model
+ * @param LinkInterface $link
+ * @return $this
+ */
+ public function setLink(LinkInterface $link);
+}
\ No newline at end of file
diff --git a/tests/LinkPreview/Tests/LinkPreviewTest.php b/tests/LinkPreview/Tests/LinkPreviewTest.php
index 27a0213..1a0ef8b 100644
--- a/tests/LinkPreview/Tests/LinkPreviewTest.php
+++ b/tests/LinkPreview/Tests/LinkPreviewTest.php
@@ -6,6 +6,14 @@
class LinkPreviewTest extends \PHPUnit_Framework_TestCase
{
+ public function testAddDefaultParsers()
+ {
+ $linkPreview = new LinkPreview();
+ $linkPreview->getParsed();
+
+ self::assertArrayHasKey('general', $linkPreview->getParsers());
+ }
+
public function testAddParser()
{
$generalParserMock = $this->getMock('LinkPreview\Parser\GeneralParser', null);
@@ -16,63 +24,62 @@ public function testAddParser()
// check if parser is added to the list
$linkPreview->addParser($generalParserMock);
$parsers = $linkPreview->getParsers();
- $this->assertContains('general', $parsers);
+ self::assertContains('general', $parsers);
// check if parser added to the beginning of the list
$linkPreview->addParser($youtubeParserMock);
$parsers = $linkPreview->getParsers();
- $this->assertEquals('youtube', key($parsers));
+ self::assertEquals('youtube', key($parsers));
return $linkPreview;
}
- /**
- * @depends testAddParser
- */
- public function testRemoveParser(LinkPreview $linkPreview)
- {
- $linkPreview->removeParser('general');
- $parsers = $linkPreview->getParsers();
- $this->assertNotContains('general', $parsers);
- }
-
public function testGetParsed()
{
$linkMock = $this->getMock('LinkPreview\Model\Link', null);
$generalParserMock = $this->getMock('LinkPreview\Parser\GeneralParser');
- $generalParserMock->expects($this->once())
+ $generalParserMock->expects(self::once())
->method('getLink')
- ->will($this->returnValue($linkMock));
- $generalParserMock->expects($this->once())
+ ->will(self::returnValue($linkMock));
+ $generalParserMock->expects(self::once())
->method('isValidParser')
- ->will($this->returnValue(true));
- $generalParserMock->expects($this->once())
+ ->will(self::returnValue(true));
+ $generalParserMock->expects(self::once())
->method('__toString')
- ->will($this->returnValue('general'));
- $generalParserMock->expects($this->once())
+ ->will(self::returnValue('general'));
+ $generalParserMock->expects(self::once())
->method('parseLink')
- ->will($this->returnValue($linkMock));
+ ->will(self::returnValue($linkMock));
$linkPreview = new LinkPreview();
$linkPreview->setPropagation(false);
$linkPreview->addParser($generalParserMock);
$parsed = $linkPreview->getParsed();
- $this->assertArrayHasKey('general', $parsed);
+ self::assertArrayHasKey('general', $parsed);
}
- public function testAddDefaultParsers()
+ /**
+ * @depends testAddParser
+ */
+ public function testRemoveParser(LinkPreview $linkPreview)
{
- $linkPreview = new LinkPreview();
- $linkPreview->getParsed();
-
- $this->assertArrayHasKey('general', $linkPreview->getParsers());
+ $linkPreview->removeParser('general');
+ $parsers = $linkPreview->getParsers();
+ self::assertNotContains('general', $parsers);
}
public function testSetUrl()
{
$linkPreview = new LinkPreview('http://github.com');
- $this->assertEquals('http://github.com', $linkPreview->getUrl());
+ self::assertEquals('http://github.com', $linkPreview->getUrl());
+ }
+
+ public function testYoutube()
+ {
+ $linkPreview = new LinkPreview('https://www.youtube.com/watch?v=C0DPdy98e4c');
+ $parsedLink = current($linkPreview->getParsed());
+ self::assertInstanceOf('LinkPreview\Model\VideoLink', $parsedLink);
}
}
\ No newline at end of file
diff --git a/tests/LinkPreview/Tests/Parser/GeneralParserTest.php b/tests/LinkPreview/Tests/Parser/GeneralParserTest.php
index 5b9df69..8dc4918 100644
--- a/tests/LinkPreview/Tests/Parser/GeneralParserTest.php
+++ b/tests/LinkPreview/Tests/Parser/GeneralParserTest.php
@@ -6,18 +6,29 @@
class GeneralParserTest extends \PHPUnit_Framework_TestCase
{
- public function testIsValidParser()
+ /**
+ * @dataProvider urlProvider
+ * @param string $url
+ * @param bool $expectedResult
+ */
+ public function testIsValidParser($url, $expectedResult)
{
$linkMock = $this->getMock('LinkPreview\Model\Link', null);
$parser = new GeneralParser();
- $parser->setLink($linkMock->setUrl('http://github.com'));
- $this->assertTrue($parser->isValidParser());
-
- $parser->setLink($linkMock->setUrl('http://trololo'));
- $this->assertFalse($parser->isValidParser());
+ $parser->setLink($linkMock->setUrl($url));
+ self::assertEquals($parser->isValidParser(), $expectedResult);
+ }
- $parser->setLink($linkMock->setUrl('github.com'));
- $this->assertFalse($parser->isValidParser());
+ /**
+ * @return array
+ */
+ public function urlProvider()
+ {
+ return [
+ ['http://github.com', true],
+ ['http://trololo', false],
+ ['github.com', false]
+ ];
}
}
\ No newline at end of file
diff --git a/tests/LinkPreview/Tests/Reader/GeneralReaderTest.php b/tests/LinkPreview/Tests/Reader/GeneralReaderTest.php
index 229359d..76093b4 100644
--- a/tests/LinkPreview/Tests/Reader/GeneralReaderTest.php
+++ b/tests/LinkPreview/Tests/Reader/GeneralReaderTest.php
@@ -10,36 +10,36 @@ public function testReadLink()
{
$responseMock = $this->getMock(
'Guzzle\Http\Message\Response',
- array('getBody', 'getContentType', 'getEffectiveUrl'),
- array(),
+ ['getBody', 'getContentType', 'getEffectiveUrl'],
+ [],
'',
false
);
- $responseMock->expects($this->once())
+ $responseMock->expects(self::once())
->method('getBody')
- ->will($this->returnValue('body'));
- $responseMock->expects($this->once())
+ ->will(self::returnValue('body'));
+ $responseMock->expects(self::once())
->method('getContentType')
- ->will($this->returnValue('text/html'));
- $responseMock->expects($this->once())
+ ->will(self::returnValue('text/html'));
+ $responseMock->expects(self::once())
->method('getEffectiveUrl')
- ->will($this->returnValue('http://github.com'));
+ ->will(self::returnValue('http://github.com'));
$requestMock = $this->getMock(
'Guzzle\Http\Message\Request',
- array('send'),
- array(),
+ ['send'],
+ [],
'',
false
);
- $requestMock->expects($this->once())
+ $requestMock->expects(self::once())
->method('send')
- ->will($this->returnValue($responseMock));
+ ->will(self::returnValue($responseMock));
$clientMock = $this->getMock('Guzzle\Http\Client');
- $clientMock->expects($this->once())
+ $clientMock->expects(self::once())
->method('get')
- ->will($this->returnValue($requestMock));
+ ->will(self::returnValue($requestMock));
$linkMock = $this->getMock('LinkPreview\Model\Link', null);
@@ -48,9 +48,8 @@ public function testReadLink()
$reader->setLink($linkMock);
$link = $reader->readLink();
- $this->assertEquals('body', $link->getContent());
- $this->assertEquals('text/html', $link->getContentType());
- $this->assertEquals('http://github.com', $link->getRealUrl());
+ self::assertEquals('body', $link->getContent());
+ self::assertEquals('text/html', $link->getContentType());
+ self::assertEquals('http://github.com', $link->getRealUrl());
}
}
-
\ No newline at end of file