Skip to content

Commit

Permalink
Merge pull request #32 from ARCANEDEV/patch-og_locale_alternate
Browse files Browse the repository at this point in the history
Adding the ability to render mutliple metas with the same property name
  • Loading branch information
arcanedev-maroc authored Nov 13, 2017
2 parents 067abc1 + b4f7786 commit 9dd4212
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 77 deletions.
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ sudo: false
php:
- 7.0
- 7.1
- 7.2
- nightly

matrix:
allow_failures:
- php: 7.2
- php: nightly

env:
- TESTBENCH_VERSION=3.5.*

before_script:
- travis_retry composer self-update
- travis_retry composer require --prefer-source --no-interaction --dev "orchestra/testbench:${TESTBENCH_VERSION}"
- travis_retry composer install --prefer-source --no-interaction --dev

script:
- composer validate
- mkdir -p build/logs
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- if [ "$TRAVIS_PHP_VERSION" != "7.2" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "7.2" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
34 changes: 26 additions & 8 deletions _docs/4-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,8 @@ interface TwitterCard extends Renderable
/**
* Add a meta to the card.
*
* @param string $name
* @param string $content
* @param string $name
* @param string|array $content
*
* @return self
*/
Expand Down Expand Up @@ -707,10 +707,10 @@ interface Meta extends Renderable
/**
* Make Meta instance.
*
* @param string $name
* @param string $content
* @param string $propertyName
* @param string $prefix
* @param string $name
* @param string|array $content
* @param string $propertyName
* @param string $prefix
*
* @return self
*/
Expand Down Expand Up @@ -1174,6 +1174,24 @@ interface SeoOpenGraph extends Renderable
*/
public function setSiteName($siteName);

/**
* Set the locale.
*
* @param string $locale
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
public function setLocale($locale);

/**
* Set the alternative locales.
*
* @param array $locales
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
public function setAlternativeLocales(array $locales);

/**
* Add many open graph properties.
*
Expand All @@ -1186,8 +1204,8 @@ interface SeoOpenGraph extends Renderable
/**
* Add an open graph property.
*
* @param string $property
* @param string $content
* @param string $property
* @param string|array $content
*
* @return self
*/
Expand Down
8 changes: 3 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"arcanedev/support": "~4.0"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
"phpunit/phpcov": "~4.0"
"orchestra/testbench": "~3.5.0",
"phpunit/phpunit": "~6.0",
"phpunit/phpcov": "~4.0"
},
"autoload": {
"psr-4": {
Expand All @@ -35,9 +36,6 @@
"Arcanedev\\SeoHelper\\Tests\\": "tests/"
}
},
"scripts": {
"testbench": "composer require --dev \"orchestra/testbench=~3.0\""
},
"extra": {
"laravel": {
"providers": [
Expand Down
8 changes: 4 additions & 4 deletions src/Bases/MetaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public function addMany(array $metas)
/**
* Add a meta to collection.
*
* @param string $name
* @param string $content
* @param string $name
* @param string|array $content
*
* @return \Arcanedev\SeoHelper\Bases\MetaCollection
*/
Expand All @@ -105,8 +105,8 @@ public function add($name, $content)
/**
* Make a meta and add it to collection.
*
* @param string $name
* @param string $content
* @param string $name
* @param string|array $content
*
* @return \Arcanedev\SeoHelper\Bases\MetaCollection
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Entities/MetaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ interface MetaCollection extends Renderable
/**
* Add a meta to collection.
*
* @param string $name
* @param string $content
* @param string $name
* @param string|array $content
*
* @return self
*/
Expand Down
22 changes: 20 additions & 2 deletions src/Contracts/Entities/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ public function setImage($image);
*/
public function setSiteName($siteName);

/**
* Set the locale.
*
* @param string $locale
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
public function setLocale($locale);

/**
* Set the alternative locales.
*
* @param array $locales
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
public function setAlternativeLocales(array $locales);

/**
* Add many open graph properties.
*
Expand All @@ -89,8 +107,8 @@ public function addProperties(array $properties);
/**
* Add an open graph property.
*
* @param string $property
* @param string $content
* @param string $property
* @param string|array $content
*
* @return self
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Entities/TwitterCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public function addMetas(array $metas);
/**
* Add a meta to the card.
*
* @param string $name
* @param string $content
* @param string $name
* @param string|array $content
*
* @return self
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Contracts/Helpers/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function setNameProperty($nameProperty);
/**
* Make Meta instance.
*
* @param string $name
* @param string $content
* @param string $propertyName
* @param string $prefix
* @param string $name
* @param string|array $content
* @param string $propertyName
* @param string $prefix
*
* @return self
*/
Expand Down
28 changes: 26 additions & 2 deletions src/Entities/OpenGraph/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,30 @@ public function setSiteName($siteName)
return $this->addProperty('site_name', $siteName);
}

/**
* Set the locale.
*
* @param string $locale
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
public function setLocale($locale)
{
return $this->addProperty('locale', $locale);
}

/**
* Set the alternative locales.
*
* @param array $locales
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
public function setAlternativeLocales(array $locales)
{
return $this->addProperty('locale:alternate', $locales);
}

/**
* Add many open graph properties.
*
Expand All @@ -169,8 +193,8 @@ public function addProperties(array $properties)
/**
* Add an open graph property.
*
* @param string $property
* @param string $content
* @param string $property
* @param string|array $content
*
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/Twitter/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ public function addMetas(array $metas)
/**
* Add a meta to the card.
*
* @param string $name
* @param string $content
* @param string $name
* @param string|array $content
*
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card
*/
Expand Down
50 changes: 26 additions & 24 deletions src/Helpers/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Arcanedev\SeoHelper\Contracts\Helpers\Meta as MetaContract;
use Arcanedev\SeoHelper\Exceptions\InvalidArgumentException;
use Illuminate\Support\Arr;

/**
* Class Meta
Expand Down Expand Up @@ -40,9 +41,9 @@ class Meta implements MetaContract
/**
* Meta content.
*
* @var string
* @var string|array
*/
protected $content = '';
protected $content;

/* -----------------------------------------------------------------
| Constructor
Expand All @@ -52,10 +53,10 @@ class Meta implements MetaContract
/**
* Make Meta instance.
*
* @param string $name
* @param string $content
* @param string $prefix
* @param string $propertyName
* @param string $name
* @param string|array $content
* @param string $prefix
* @param string $propertyName
*/
public function __construct($name, $content, $propertyName = 'name', $prefix = '')
{
Expand Down Expand Up @@ -141,25 +142,28 @@ private function setName($name)
/**
* Get the meta content.
*
* @return string
* @return string|array
*/
private function getContent()
{
return $this->clean($this->content);
return is_array($this->content)
? array_map(function ($content) { return $this->clean($content); }, $this->content)
: $this->clean($this->content);
}

/**
* Set the meta content.
*
* @param string $content
* @param string|array $content
*
* @return \Arcanedev\SeoHelper\Helpers\Meta
*/
private function setContent($content)
{
if (is_string($content)) {
if (is_array($content))
$this->content = $content;
elseif (is_string($content))
$this->content = trim($content);
}

return $this;
}
Expand All @@ -171,10 +175,10 @@ private function setContent($content)
/**
* Make Meta instance.
*
* @param string $name
* @param string $content
* @param string $propertyName
* @param string $prefix
* @param string $name
* @param string|array $content
* @param string $propertyName
* @param string $prefix
*
* @return \Arcanedev\SeoHelper\Helpers\Meta
*/
Expand All @@ -190,9 +194,7 @@ public static function make($name, $content, $propertyName = 'name', $prefix = '
*/
public function render()
{
return $this->isLink()
? $this->renderLink()
: $this->renderMeta();
return $this->isLink() ? $this->renderLink() : $this->renderMeta();
}

/**
Expand All @@ -212,11 +214,11 @@ private function renderLink()
*/
private function renderMeta()
{
$output = [];
$output[] = $this->nameProperty.'="'.$this->getName().'"';
$output[] = 'content="'.$this->getContent().'"';
$content = Arr::wrap($this->getContent());

return '<meta '.implode(' ', $output).'>';
return implode(PHP_EOL, array_map(function ($content) {
return "<meta {$this->nameProperty}=\"{$this->getName()}\" content=\"{$content}\">";
}, $content));
}

/**
Expand Down Expand Up @@ -268,7 +270,7 @@ private function checkNameProperty(&$nameProperty)
{
if ( ! is_string($nameProperty)) {
throw new InvalidArgumentException(
'The meta name property is must be a string value, ' . gettype($nameProperty) . ' is given.'
'The meta name property is must be a string value, '.gettype($nameProperty).' is given.'
);
}

Expand All @@ -278,7 +280,7 @@ private function checkNameProperty(&$nameProperty)
if ( ! in_array($name, $allowed)) {
throw new InvalidArgumentException(
"The meta name property [$name] is not supported, ".
"the allowed name properties are ['". implode("', '", $allowed) . "']."
"the allowed name properties are ['".implode("', '", $allowed)."']."
);
}

Expand Down
Loading

0 comments on commit 9dd4212

Please sign in to comment.