Skip to content

Commit

Permalink
Merge pull request #13 from ARCANEDEV/patch-twitter_card
Browse files Browse the repository at this point in the history
Updating Twitter Card + Refactoring
  • Loading branch information
arcanedev-maroc authored Jul 20, 2016
2 parents 2006e39 + 48ba231 commit 2912e58
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
20 changes: 20 additions & 0 deletions src/Contracts/Entities/TwitterCardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,24 @@
*/
interface TwitterCardInterface extends Renderable
{
/* ------------------------------------------------------------------------------------------------
| Constants
| ------------------------------------------------------------------------------------------------
*/
const TYPE_APP = 'app';
const TYPE_GALLERY = 'gallery';
const TYPE_PHOTO = 'photo';
const TYPE_PLAYER = 'player';
const TYPE_PRODUCT = 'product';
const TYPE_SUMMARY = 'summary';
const TYPE_SUMMARY_LARGE_IMAGE = 'summary_large_image';

/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Set the card type.
*
* @param string $type
*
Expand Down Expand Up @@ -77,6 +90,13 @@ public function addMetas(array $metas);
*/
public function addMeta($name, $content);

/**
* Get all supported card types.
*
* @return array
*/
public function types();

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand Down
41 changes: 23 additions & 18 deletions src/Entities/Twitter/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Card implements TwitterCardInterface
*
* @var string
*/
protected $type = 'summary';
protected $type;

/**
* Card meta collection.
Expand Down Expand Up @@ -55,7 +55,7 @@ class Card implements TwitterCardInterface
public function __construct(array $configs = [])
{
$this->setConfigs($configs);
$this->metas = new MetaCollection;
$this->metas = new MetaCollection;

$this->init();
}
Expand All @@ -68,7 +68,7 @@ public function __construct(array $configs = [])
private function init()
{
$this->setPrefix($this->getConfig('prefix', 'twitter:'));
$this->setType($this->getConfig('card', ''));
$this->setType($this->getConfig('card', static::TYPE_SUMMARY));
$this->setSite($this->getConfig('site', ''));
$this->setTitle($this->getConfig('title', ''));
$this->addMetas($this->getConfig('metas', []));
Expand All @@ -95,6 +95,7 @@ private function setPrefix($prefix)
}

/**
* Set the card type.
*
* @param string $type
*
Expand Down Expand Up @@ -204,6 +205,24 @@ public function addMeta($name, $content)
return $this;
}

/**
* Get all supported card types.
*
* @return array
*/
public function types()
{
return [
static::TYPE_APP,
static::TYPE_GALLERY,
static::TYPE_PHOTO,
static::TYPE_PLAYER,
static::TYPE_PRODUCT,
static::TYPE_SUMMARY,
static::TYPE_SUMMARY_LARGE_IMAGE,
];
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -265,20 +284,6 @@ public function __toString()
| Check Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Check if type is supported.
*
* @param string $type
*
* @return bool
*/
private function isSupportedType($type)
{
return in_array($type, [
'app', 'gallery', 'photo', 'player', 'product', 'summary', 'summary_large_image'
]);
}

/**
* Check the card type.
*
Expand All @@ -296,7 +301,7 @@ private function checkType(&$type)

$type = strtolower(trim($type));

if ( ! $this->isSupportedType($type)) {
if ( ! in_array($type, $this->types())) {
throw new InvalidTwitterCardException(
"The Twitter card type [$type] is not supported."
);
Expand Down

0 comments on commit 2912e58

Please sign in to comment.