Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from kasp3r/youtube-fix
Browse files Browse the repository at this point in the history
Fix youtube and some code formatting
  • Loading branch information
kasp3r committed Jun 29, 2015
2 parents d97c572 + f92ea6e commit 61b6c96
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 377 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
137 changes: 65 additions & 72 deletions src/LinkPreview/LinkPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down
Loading

0 comments on commit 61b6c96

Please sign in to comment.