diff --git a/.travis.yml b/.travis.yml index d2aeec1..e343f39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,13 +3,13 @@ language: php dist: precise php: - - 5.3 - - 5.6 - - 7.2 + - 7.1 - 7.3 before_script: - curl -s http://getcomposer.org/installer | php - php composer.phar install --dev + - php composer.phar require "phpunit/phpunit:~6" -script: phpunit +script: + - ./vendor/bin/phpunit diff --git a/README.md b/README.md index cc333ab..60ff3c3 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Library that assists in building Open Graph meta tags. Add `chriskonnertz/open-graph` to `composer.json` with a text editor: ``` -"chriskonnertz/open-graph": "~1" +"chriskonnertz/open-graph": "~2" ``` Or via a console: @@ -23,7 +23,7 @@ composer require chriskonnertz/open-graph In the future use `composer update` to update to the latest version of Open Graph Builder. -> This library supports PHP >=5.3. +> This library requires PHP >=7.0. ### Framework Support @@ -32,10 +32,10 @@ In Laravel 5.0-5.4 you have to edit your `config/app.php` config file. You can either add an alias to the object so you can create a new instance via `new OpenGraph()` ... ```php -'aliases' => array( +'aliases' => [ ... 'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraph', -), +], ``` ...or an alias to the facade (this is what happens in Laravel >=5.5 via package auto-discovery) so you @@ -43,17 +43,17 @@ do not have to create the instance by yourself but you can access it via pseudo- If you choose this path you also have to add the service provider to the config file: ```php -'aliases' => array( +'aliases' => [ ... 'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraphFacade', -), +], ... -'providers' => array( +'providers' => [ ... 'ChrisKonnertz\OpenGraph\OpenGraphServiceProvider', -), +], ``` > If you need to reset the underlying instance of the facade (the `OpenGraph` object), call `OpenGraph::clear()`. @@ -76,8 +76,6 @@ Render these tags in a template as follows: {!! $og->renderTags() !!} ``` -> In Laravel 4 you have to use ``{{ ... }}`` tags to avoid escaping. - Providing Open Graph tags enriches web pages. The downside is some extra time to spend, because every model has its own way to generate these tags. It's also important to follow the [official protocol](http://ogp.me/). Read the documentation to learn more about the tags that are available and the values they support or [check out examples](https://github.com/niallkennedy/open-graph-protocol-examples). Please note that this implementation sticks to the specification of OGP.me and does not support the enhancements created by Facebook. ## Add Tags And Attributes diff --git a/composer.json b/composer.json index f0ecf0b..ad2e948 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "chriskonnertz/open-graph", "description": "Class that assists in building Open Graph meta tags", - "keywords": ["opengraph", "open graph", "ogp", "laravel", "laravel4", "laravel5"], + "keywords": ["opengraph", "open graph", "ogp", "laravel", "laravel5"], "license": "MIT", "authors": [ { @@ -9,10 +9,10 @@ } ], "require": { - "php": ">=5.3.7" + "php": ">=7.0" }, "require-dev": { - "nesbot/carbon": "~1.14" + "nesbot/carbon": "~1" }, "autoload": { "psr-0": { diff --git a/src/ChrisKonnertz/OpenGraph/OpenGraph.php b/src/ChrisKonnertz/OpenGraph/OpenGraph.php index 6a83680..6189750 100644 --- a/src/ChrisKonnertz/OpenGraph/OpenGraph.php +++ b/src/ChrisKonnertz/OpenGraph/OpenGraph.php @@ -6,6 +6,7 @@ use DateTime; /** + * Library that assists in building Open Graph meta tags. * Open Graph protocol official docs: http://ogp.me/ */ class OpenGraph @@ -14,7 +15,7 @@ class OpenGraph /** * The version number */ - const VERSION = '1.0.7'; + const VERSION = '2.0.0'; /** * Define a prefix for tag names @@ -47,21 +48,21 @@ class OpenGraph * * @param bool $validate Enable validation? */ - public function __construct($validate = false) + public function __construct(bool $validate = false) { - $this->tags = array(); + $this->tags = []; $this->validate = $validate; } /** * Creates and returns a new open graph tag object. * - * @param string $name The name of the tag - * @param mixed $value The value of the tag - * @param bool $prefixed Add the "og"-prefix? + * @param string $name The name of the tag + * @param mixed $value The value of the tag + * @param bool $prefixed Add the "og"-prefix? * @return OpenGraphTag */ - protected function createTag($name, $value, $prefixed = true) + protected function createTag(string $name, $value, bool $prefixed = true) : OpenGraphTag { return new OpenGraphTag($name, $value, $prefixed); } @@ -71,7 +72,7 @@ protected function createTag($name, $value, $prefixed = true) * * @return bool */ - public function valid() + public function valid() : bool { return $this->validate; } @@ -79,10 +80,10 @@ public function valid() /** * Setter for the validation mode. * - * @param bool $validate + * @param bool $validate * @return OpenGraph */ - public function validate($validate = true) + public function validate(bool $validate = true) : self { $this->validate = $validate; @@ -94,7 +95,7 @@ public function validate($validate = true) * * @return OpenGraphTag[] */ - public function tags() + public function tags() : array { return $this->tags; } @@ -103,10 +104,10 @@ public function tags() * True if at least one tag with the given name exists. * It's possible that a tag has multiple values. * - * @param string $name + * @param string $name * @return bool */ - public function has($name) + public function has(string $name) : bool { foreach ($this->tags as $tag) { if ($tag->name == $name) { @@ -123,7 +124,7 @@ public function has($name) * @param string $name * @return OpenGraph */ - public function forget($name) + public function forget(string $name) : self { foreach ($this->tags as $key => $tag) { if ($tag->name == $name) { @@ -139,9 +140,9 @@ public function forget($name) * * @return OpenGraph */ - public function clear() + public function clear() : self { - $this->tags = array(); + $this->tags = []; return $this; } @@ -149,12 +150,12 @@ public function clear() /** * Adds a custom tag to the list of tags * - * @param string $name The name of the tag - * @param string $value The value of the tag - * @param bool $prefixed Add the "og"-prefix? + * @param string $name The name of the tag + * @param mixed $value The value of the tag + * @param bool $prefixed Add the "og"-prefix? * @return OpenGraph */ - public function tag($name, $value, $prefixed = true) + public function tag(string $name, $value, bool $prefixed = true) : self { $value = $this->convertDate($value); @@ -166,13 +167,17 @@ public function tag($name, $value, $prefixed = true) /** * Adds attribute tags to the list of tags * - * @param string $tagName The name of the base tag - * @param array $attributes Array with attributes (pairs of name and value) - * @param string[] $valid Array with names of valid attributes - * @param bool $prefixed Add the "og"-prefix? + * @param string $tagName The name of the base tag + * @param array $attributes Array with attributes (pairs of name and value) + * @param string[] $valid Array with names of valid attributes + * @param bool $prefixed Add the "og"-prefix? * @return OpenGraph */ - public function attributes($tagName, $attributes = array(), $valid = array(), $prefixed = true) + public function attributesstring ( + string $tagName, + array $attributes = [], + array $valid = [], + bool $prefixed = true) : self { foreach ($attributes as $name => $value) { if ($this->validate and sizeof($valid) > 0) { @@ -192,12 +197,12 @@ public function attributes($tagName, $attributes = array(), $valid = array(), $p /** * Shortcut for attributes() with $prefixed = false * - * @param string $tagName The name of the base tag - * @param array $attributes Array with attributes (pairs of name and value) - * @param string[] $valid Array with names of valid attributes + * @param string $tagName The name of the base tag + * @param array $attributes Array with attributes (pairs of name and value) + * @param string[] $valid Array with names of valid attributes * @return OpenGraph */ - public function unprefixedAttributes($tagName, $attributes = array(), $valid = array()) + public function unprefixedAttributes(string $tagName, array $attributes = [], array $valid = []) : self { return $this->attributes($tagName, $attributes, $valid, false); } @@ -208,7 +213,7 @@ public function unprefixedAttributes($tagName, $attributes = array(), $valid = a * @param string $title * @return OpenGraph */ - public function title($title) + public function title(string $title) : self { $title = trim($title); @@ -229,9 +234,9 @@ public function title($title) * @param string $type * @return OpenGraph */ - public function type($type) + public function type(string $type) : self { - $types = array( + $types = [ 'music.song', 'music.album', 'music.playlist', @@ -244,7 +249,7 @@ public function type($type) 'book', 'profile', 'website', - ); + ]; if ($this->validate and ! in_array($type, $types)) { throw new Exception("Open Graph: Invalid type '{$type}' (unknown type)"); @@ -261,11 +266,11 @@ public function type($type) * Adds an image tag. * If the URL is relative it's converted to an absolute one. * - * @param string $imageFile The URL of the image file - * @param array $attributes Array with additional attributes (pairs of name and value) + * @param string $imageFile The URL of the image file + * @param array|null $attributes Array with additional attributes (pairs of name and value) * @return OpenGraph */ - public function image($imageFile, $attributes = null) + public function image(string $imageFile, array $attributes = null) : self { if ($this->validate and ! $imageFile) { throw new Exception("Open Graph: Invalid image URL (empty)"); @@ -282,12 +287,12 @@ public function image($imageFile, $attributes = null) $this->tags[] = $this->createTag('image', $imageFile); if ($attributes) { - $valid = array( + $valid = [ 'secure_url', 'type', 'width', 'height', - ); + ]; $this->attributes('image', $attributes, $valid); } @@ -298,11 +303,11 @@ public function image($imageFile, $attributes = null) /** * Adds a description tag * - * @param string $description The description text - * @param int $description If the text is longer than this it is shortened + * @param string $description The description text + * @param int $description If the text is longer than this it is shortened * @return OpenGraph */ - public function description($description, $maxLength = 250) + public function description(string $description, int $maxLength = 250) : self { $description = trim(strip_tags($description)); $description = preg_replace("/\r|\n/", '', $description); @@ -328,7 +333,7 @@ public function description($description, $maxLength = 250) * @param string $url * @return OpenGraph */ - public function url($url = null) + public function url(string $url = null) : self { if (! $url) { $url = null; @@ -372,7 +377,7 @@ public function url($url = null) * @param string $locale * @return OpenGraph */ - public function locale($locale) + public function locale(string $locale) : self { if ($this->validate and ! $locale) { throw new Exception("Open Graph: Invalid locale (none set)"); @@ -391,7 +396,7 @@ public function locale($locale) * @param string[] $locales An array of alternative locales * @return OpenGraph */ - public function localeAlternate($locales = array()) + public function localeAlternate(array $locales = []) : self { if (is_string($locales)) { $locales = (array) $locales; @@ -414,7 +419,7 @@ public function localeAlternate($locales = array()) * @param string $siteName * @return OpenGraph */ - public function siteName($siteName) + public function siteName(string $siteName) : self { if ($this->validate and ! $siteName) { throw new Exception("Open Graph: Invalid site_name (empty)"); @@ -430,18 +435,18 @@ public function siteName($siteName) /** * Adds a determiner tag. * - * @param string $locale + * @param string $determiner * @return OpenGraph */ - public function determiner($determiner = '') + public function determiner(string $determiner = '') : self { - $enum = array( + $enum = [ 'a', 'an', 'the', 'auto', '' - ); + ]; if ($this->validate and ! in_array($determiner, $enum)) { throw new Exception("Open Graph: Invalid determiner '{$determiner}' (unkown value)"); @@ -453,14 +458,14 @@ public function determiner($determiner = '') } /** - * Adds an audio tag + * Adds an audio tag. * If the URL is relative its converted to an absolute one. * - * @param string $audioFile The URL of the video file - * @param array $attributes Array with additional attributes (pairs of name and value) + * @param string $audioFile The URL of the video file + * @param array|null $attributes Array with additional attributes (pairs of name and value) * @return OpenGraph */ - public function audio($audioFile, $attributes = null) + public function audio(string $audioFile, array $attributes = null) : self { if ($this->validate and ! $audioFile) { throw new Exception("Open Graph: Invalid audio URL (empty)"); @@ -477,48 +482,48 @@ public function audio($audioFile, $attributes = null) $this->tags[] = $this->createTag('audio', $audioFile); if ($attributes) { - $valid = array( + $valid = [ 'secure_url', 'type', - ); + ]; $tag = $this->lastTag('type'); - $specialValid = array(); + $specialValid = []; if ($tag and $tag->name == 'music.song') { - $specialValid = array( + $specialValid = [ 'duration', 'album', 'album:disc', 'album:track', 'musician', - ); + ]; } if ($tag and $tag->name == 'music.album') { - $specialValid = array( + $specialValid = [ 'song', 'song:disc', 'song:track', 'musician', 'release_date', - ); + ]; } if ($tag and $tag->name == 'music.playlist') { - $specialValid = array( + $specialValid = [ 'song', 'song:disc', 'song:track', 'creator', - ); + ]; } if ($tag and $tag->name == 'music.radio_station') { - $specialValid = array( + $specialValid = [ 'creator', - ); + ]; } $valid = array_merge($valid, $specialValid); @@ -533,11 +538,11 @@ public function audio($audioFile, $attributes = null) * Adds a video tag * If the URL is relative its converted to an absolute one. * - * @param string $videoFile The URL of the video file - * @param array $attributes Array with additional attributes (pairs of name and value) + * @param string $videoFile The URL of the video file + * @param array|null $attributes Array with additional attributes (pairs of name and value) * @return OpenGraph */ - public function video($videoFile, $attributes = null) + public function video(string $videoFile, array $attributes = null) : self { if ($this->validate and ! $videoFile) { throw new Exception("Open Graph: Invalid video URL (empty)"); @@ -554,16 +559,16 @@ public function video($videoFile, $attributes = null) $this->tags[] = $this->createTag('video', $videoFile); if ($attributes) { - $valid = array( + $valid = [ 'secure_url', 'type', 'width', 'height', - ); + ]; $tag = $this->lastTag('type'); if ($tag and starts_with($tag->value, 'video.')) { - $specialValid = array( + $specialValid = [ 'actor', 'role', 'director', @@ -571,7 +576,7 @@ public function video($videoFile, $attributes = null) 'duration', 'release_date', 'tag', - ); + ]; if ($tag->value == 'video.episode') { $specialValid[] = 'video:series'; @@ -589,24 +594,24 @@ public function video($videoFile, $attributes = null) /** * Adds article attributes * - * @param array $attributes Array with attributes (pairs of name and value) + * @param array $attributes Array with attributes (pairs of name and value) * @return OpenGraph */ - public function article($attributes = array()) + public function article(array $attributes = []) : self { $tag = $this->lastTag('type'); if (! $tag or $tag->value != 'article') { throw new Exception("Open Graph: Type has to be 'article' to add article attributes"); } - $valid = array( + $valid = [ 'published_time', 'modified_time', 'expiration_time', 'author', 'section', 'tag', - ); + ]; $this->unprefixedAttributes('article', $attributes, $valid); @@ -616,22 +621,22 @@ public function article($attributes = array()) /** * Adds book attributes * - * @param array $attributes Array with attributes (pairs of name and value) + * @param array $attributes Array with attributes (pairs of name and value) * @return OpenGraph */ - public function book($attributes = array()) + public function book(array $attributes = []) : self { $tag = $this->lastTag('type'); if (! $tag or $tag->value != 'book') { throw new Exception("Open Graph: Type has to be 'book' to add book attributes"); } - $valid = array( + $valid = [ 'author', 'isbn', 'release_date', 'tag', - ); + ]; $this->unprefixedAttributes('book', $attributes); @@ -644,19 +649,19 @@ public function book($attributes = array()) * @param array $attributes Array with attributes (pairs of name and value) * @return OpenGraph */ - public function profile($attributes = array()) + public function profile(array $attributes = []) : self { $tag = $this->lastTag('type'); if (! $tag or $tag->value != 'profile') { throw new Exception("Open Graph: Type has to be 'profile' to add profile attributes"); } - $valid = array( + $valid = [ 'first_name', 'last_name', 'username', 'gender', - ); + ]; $this->unprefixedAttributes('profile', $attributes); @@ -669,7 +674,7 @@ public function profile($attributes = array()) * @param string $template The template string * @return OpenGraph */ - public function template($template) + public function template(string $template) : self { $this->template = $template; @@ -681,10 +686,10 @@ public function template($template) * * @return string */ - public function renderTags() + public function renderTags() : string { $output = ''; - $vars = array('{{name}}', '{{value}}'); + $vars = ['{{name}}', '{{value}}']; foreach ($this->tags as $tag) { $name = $tag->name; @@ -692,7 +697,7 @@ public function renderTags() $name = self::NAME_PREFIX.$name; } - $output .= str_replace($vars, array($name, $tag->value), $this->template); + $output .= str_replace($vars, [$name, $tag->value], $this->template); } return $output; @@ -711,10 +716,10 @@ public function __toString() /** * Returns the last tag in the lists of tags with matching name * - * @param string $name The name of the tag - * @return OpenGraphTag|null Returns the tag object or null + * @param string $name The name of the tag + * @return OpenGraphTag|null Returns the tag object or null */ - public function lastTag($name) + public function lastTag(string $name) { $lastTag = null; @@ -733,7 +738,7 @@ public function lastTag($name) * @param string|DateTime $date The date (string or DateTime) * @return string */ - protected function convertDate($date) + protected function convertDate($date) : string { if (is_a($date, 'DateTime')) { return (string) $date->format(DateTime::ISO8601); diff --git a/src/ChrisKonnertz/OpenGraph/OpenGraphFacade.php b/src/ChrisKonnertz/OpenGraph/OpenGraphFacade.php index f034157..c53c2a7 100644 --- a/src/ChrisKonnertz/OpenGraph/OpenGraphFacade.php +++ b/src/ChrisKonnertz/OpenGraph/OpenGraphFacade.php @@ -4,6 +4,9 @@ use Illuminate\Support\Facades\Facade; +/** + * Facade for Laravel integration + */ class OpenGraphFacade extends Facade { diff --git a/src/ChrisKonnertz/OpenGraph/OpenGraphServiceProvider.php b/src/ChrisKonnertz/OpenGraph/OpenGraphServiceProvider.php index 76f2686..4ab0c3d 100644 --- a/src/ChrisKonnertz/OpenGraph/OpenGraphServiceProvider.php +++ b/src/ChrisKonnertz/OpenGraph/OpenGraphServiceProvider.php @@ -5,6 +5,9 @@ use ChrisKonnertz\OpenGraph\OpenGraph; use Illuminate\Support\ServiceProvider; +/** + * Service provider class for Laravel integration + */ class OpenGraphServiceProvider extends ServiceProvider { diff --git a/src/ChrisKonnertz/OpenGraph/OpenGraphTag.php b/src/ChrisKonnertz/OpenGraph/OpenGraphTag.php index 1c2c7ca..d14c53a 100644 --- a/src/ChrisKonnertz/OpenGraph/OpenGraphTag.php +++ b/src/ChrisKonnertz/OpenGraph/OpenGraphTag.php @@ -4,6 +4,9 @@ use Exception; +/** + * Class that represents n open graph tag + */ class OpenGraphTag { /** @@ -30,11 +33,11 @@ class OpenGraphTag { /** * Constructor call. * - * @param string $name The name of the tag - * @param mixed $value The value of the tag - * @param bool $prefixed Add the "og"-prefix? + * @param string $name The name of the tag + * @param mixed $value The value of the tag + * @param bool $prefixed Add the "og"-prefix? */ - public function __construct($name, $value, $prefixed = true) + public function __construct(string $name, $value, bool $prefixed = true) { $this->setAttribute('name', $name); $this->setAttribute('value', $value); @@ -58,12 +61,12 @@ public function __set($name, $value) } /** - * Sets an objet attribute to a value. + * Sets an object attribute to a value. * * @param string $name The name of the object attribute * @param mixed $value The value of the object attribute */ - protected function setAttribute($name, $value) + protected function setAttribute(string $name, $value) : void { // Convert values switch ($name) { diff --git a/tests/OpenGraphTests.php b/tests/OpenGraphTests.php index 834e489..d51902b 100644 --- a/tests/OpenGraphTests.php +++ b/tests/OpenGraphTests.php @@ -1,12 +1,5 @@