diff --git a/src/MetaTags.php b/src/MetaTags.php index c1615cf..729b9d6 100644 --- a/src/MetaTags.php +++ b/src/MetaTags.php @@ -43,21 +43,23 @@ public function __construct($indentation = null, $order = null) */ public function link($key, $value) { - $attributes = ['rel' => $key]; + if (! empty($value)) { + $attributes = ['rel' => $key]; - if (is_array($value)) { - foreach ($value as $key => $v) { - $attributes[$key] = $v; + if (is_array($value)) { + foreach ($value as $key => $v) { + $attributes[$key] = $v; + } + } else { + $attributes['href'] = $value; } - } else { - $attributes['href'] = $value; - } - $tag = $this->createTag('link', $attributes); + $tag = $this->createTag('link', $attributes); - $this->addToTagsGroup('link', $key, $tag); + $this->addToTagsGroup('link', $key, $tag); - return $tag; + return $tag; + } } /** @@ -70,21 +72,23 @@ public function link($key, $value) */ public function meta($key, $value) { - $attributes = ['name' => $key]; + if (! empty($value)) { + $attributes = ['name' => $key]; - if (is_array($value)) { - foreach ($value as $key => $v) { - $attributes[$key] = $v; + if (is_array($value)) { + foreach ($value as $key => $v) { + $attributes[$key] = $v; + } + } else { + $attributes['content'] = $value; } - } else { - $attributes['content'] = $value; - } - $tag = $this->createTag('meta', $attributes); + $tag = $this->createTag('meta', $attributes); - $this->addToTagsGroup('meta', $key, $tag); + $this->addToTagsGroup('meta', $key, $tag); - return $tag; + return $tag; + } } /** @@ -98,15 +102,17 @@ public function meta($key, $value) */ public function og($key, $value, $prefixed = true) { - $key = $prefixed ? "og:{$key}" : $key; - $tag = $this->createTag('meta', [ - 'property' => $key, - 'content' => $value - ]); + if (! empty($value)) { + $key = $prefixed ? "og:{$key}" : $key; + $tag = $this->createTag('meta', [ + 'property' => $key, + 'content' => $value + ]); - $this->addToTagsGroup('og', $key, $tag); + $this->addToTagsGroup('og', $key, $tag); - return $tag; + return $tag; + } } /** @@ -162,15 +168,17 @@ public function title($value) */ public function twitter($key, $value, $prefixed = true) { - $key = $prefixed ? "twitter:{$key}" : $key; - $tag = $this->createTag('meta', [ - 'name' => $key, - 'content' => $value - ]); + if (! empty($value)) { + $key = $prefixed ? "twitter:{$key}" : $key; + $tag = $this->createTag('meta', [ + 'name' => $key, + 'content' => $value + ]); - $this->addToTagsGroup('twitter', $key, $tag); + $this->addToTagsGroup('twitter', $key, $tag); - return $tag; + return $tag; + } } /** diff --git a/tests/MetaTagsTest.php b/tests/MetaTagsTest.php index 2e5ea45..67c15c3 100644 --- a/tests/MetaTagsTest.php +++ b/tests/MetaTagsTest.php @@ -42,11 +42,12 @@ public function testArrayOfTags() public function testLinkTag() { - $tag = $this->head->link('canonical', 'https://pedroborg.es'); + $tag = $this->head->link('canonical', 'https://pedroborg.es'); $alternate = $this->head->link('alternate', [ 'hreflang' => 'pt-br', 'href' => 'https://br.pedroborg.es' ]); + $empty = $this->head->link('dns-prefetch', ''); $this->assertEquals( '', @@ -57,12 +58,15 @@ public function testLinkTag() '', $alternate ); + + $this->assertEquals('', $empty); } public function testMetaTag() { - $tag = $this->head->meta('description', 'Meta tag test'); + $tag = $this->head->meta('description', 'Meta tag test'); $encoded = $this->head->meta('description', '"Meta tag" test'); + $empty = $this->head->meta('description', ''); $this->assertEquals( '', @@ -73,12 +77,15 @@ public function testMetaTag() '', $encoded ); + + $this->assertEquals('', $empty); } public function testOpenGraphTag() { - $tag = $this->head->og('title', 'Open Graph test'); + $tag = $this->head->og('title', 'Open Graph test'); $preffixed = $this->head->og('og:title', 'Open Graph test', false); + $empty = $this->head->og('og:title', '', false); $this->assertEquals( '', @@ -89,15 +96,19 @@ public function testOpenGraphTag() '', $preffixed ); + + $this->assertEquals('', $empty); } public function testTwitterCardTag() { - $tag = $this->head->twitter('card', 'summary'); + $tag = $this->head->twitter('card', 'summary'); $preffixed = $this->head->twitter('twitter:card', 'summary', false); + $empty = $this->head->twitter('twitter:image', '', false); $this->assertEquals('', $tag); $this->assertEquals('', $preffixed); + $this->assertEquals('', $empty); } public function testLinkedDataTag()