diff --git a/README.md b/README.md index c95036d..7536e18 100644 --- a/README.md +++ b/README.md @@ -552,6 +552,8 @@ SEOMeta::addKeyword($keyword); SEOMeta::addMeta($meta, $value = null, $name = 'name'); SEOMeta::addAlternateLanguage($lang, $url); SEOMeta::addAlternateLanguages(array $languages); +SEOMeta::setAlternateLanguage($lang, $url); +SEOMeta::setAlternateLanguages(array $languages); SEOMeta::setTitleSeparator($separator); SEOMeta::setTitle($title); SEOMeta::setTitleDefault($default); diff --git a/src/SEOTools/Contracts/MetaTags.php b/src/SEOTools/Contracts/MetaTags.php index 97a2856..5a59df4 100644 --- a/src/SEOTools/Contracts/MetaTags.php +++ b/src/SEOTools/Contracts/MetaTags.php @@ -187,6 +187,25 @@ public function addAlternateLanguage($lang, $url); */ public function addAlternateLanguages(array $languages); + /** + * Set an alternate language. + * + * @param string $lang language code in format ISO 639-1 + * @param string $url + * + * @return static + */ + public function setAlternateLanguage($lang, $url); + + /** + * Set alternate languages. + * + * @param array $languages + * + * @return static + */ + public function setAlternateLanguages(array $languages); + /** * Get the title formatted for display. * diff --git a/src/SEOTools/Facades/SEOMeta.php b/src/SEOTools/Facades/SEOMeta.php index 8136770..bdf09ca 100644 --- a/src/SEOTools/Facades/SEOMeta.php +++ b/src/SEOTools/Facades/SEOMeta.php @@ -23,6 +23,8 @@ * @method static \Artesaos\SEOTools\Contracts\MetaTags setNext(string $url) * @method static \Artesaos\SEOTools\Contracts\MetaTags addAlternateLanguage(string $lang, string $url) * @method static \Artesaos\SEOTools\Contracts\MetaTags addAlternateLanguages(array $languages) + * @method static \Artesaos\SEOTools\Contracts\MetaTags setAlternateLanguage(string $lang, string $url) + * @method static \Artesaos\SEOTools\Contracts\MetaTags setAlternateLanguages(array $languages) * @method static string getTitle() * @method static string getTitleSession() * @method static string getTitleSeparator() diff --git a/src/SEOTools/SEOMeta.php b/src/SEOTools/SEOMeta.php index f73b0bf..d4c05d6 100644 --- a/src/SEOTools/SEOMeta.php +++ b/src/SEOTools/SEOMeta.php @@ -198,7 +198,9 @@ public function generate($minify = false) } foreach ($languages as $lang) { - $html[] = ""; + if (!empty($lang['lang'] && !empty($lang['url']))) { + $html[] = ""; + } } if ($robots) { @@ -387,6 +389,32 @@ public function addAlternateLanguages(array $languages) return $this; } + /** + * {@inheritdoc} + */ + public function setAlternateLanguage($lang, $url) + { + // Remove language if already existing + $this->alternateLanguages = array_filter($this->alternateLanguages, function ($arr) use ($lang) { + return $arr['lang'] !== $lang; + }); + + // Append (updated) language + $this->alternateLanguages[] = ['lang' => $lang, 'url' => $url]; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function setAlternateLanguages(array $languages) + { + $this->alternateLanguages = $languages; + + return $this; + } + /** * Sets the meta robots. * diff --git a/tests/SEOTools/SEOMetaTest.php b/tests/SEOTools/SEOMetaTest.php index 5dd738c..22f4777 100644 --- a/tests/SEOTools/SEOMetaTest.php +++ b/tests/SEOTools/SEOMetaTest.php @@ -251,7 +251,7 @@ public function test_set_prev() $this->assertEquals($prev, $this->seoMeta->getPrev()); } - public function test_set_alternate_languages() + public function test_add_alternate_languages() { $fullHeader = "It's Over 9000!"; $fullHeader .= ""; @@ -270,6 +270,70 @@ public function test_set_alternate_languages() $this->assertEquals(array_merge($expectedLangs, $expectedLangs), $this->seoMeta->getAlternateLanguages()); } + public function test_set_alternate_languages() + { + $fullHeader = "It's Over 9000!"; + $fullHeader .= ""; + $fullHeader .= ""; + $lang = 'en'; + $langUrl = 'http://domain.com'; + + $expectedLangs = [['lang' => $lang, 'url' => $langUrl]]; + $this->seoMeta->setAlternateLanguage($lang, $langUrl); + + $this->setRightAssertion($fullHeader); + $this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages()); + + $this->seoMeta->setAlternateLanguages($expectedLangs); + + $this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages()); + } + + public function test_set_override_alternate_language() + { + $fullHeader = "It's Over 9000!"; + $fullHeader .= ""; + $fullHeader .= ""; + $lang = 'en'; + $langUrl = 'http://domain.com'; + $langUrlOverridden = 'http://domain.test'; + + $expectedLangs = [['lang' => $lang, 'url' => $langUrl]]; + $expectedLangsOverridden = [['lang' => $lang, 'url' => $langUrlOverridden]]; + + $this->seoMeta->setAlternateLanguage($lang, $langUrl); + $this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages()); + + $this->seoMeta->setAlternateLanguage($lang, $langUrlOverridden); + $this->setRightAssertion($fullHeader); + $this->assertEquals($expectedLangsOverridden, $this->seoMeta->getAlternateLanguages()); + } + + public function test_remove_alternate_language() + { + $fullHeader = "It's Over 9000!"; + $fullHeader .= ""; + $lang = 'en'; + $langUrl = 'http://domain.com'; + + $expectedLangs = [['lang' => $lang, 'url' => false]]; + + $this->seoMeta->setAlternateLanguage($lang, $langUrl); + $this->seoMeta->setAlternateLanguage($lang, false); + $this->setRightAssertion($fullHeader); + $this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages()); + } + + public function test_remove_alternate_languages() + { + $fullHeader = "It's Over 9000!"; + $fullHeader .= ""; + + $this->seoMeta->setAlternateLanguages([]); + $this->setRightAssertion($fullHeader); + $this->assertEquals([], $this->seoMeta->getAlternateLanguages()); + } + public function test_set_reset() { $expected = "It's Over 9000!";