diff --git a/src/Contracts/SeoMeta.php b/src/Contracts/SeoMeta.php index f5e5aa3..6d32df7 100644 --- a/src/Contracts/SeoMeta.php +++ b/src/Contracts/SeoMeta.php @@ -79,4 +79,43 @@ public function addKeyword($keyword); * @return self */ public function setUrl($url); + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Add a meta tag. + * + * @param string $name + * @param string $content + * + * @return self + */ + public function addMeta($name, $content); + + /** + * Add many meta tags. + * + * @param array $metas + * + * @return self + */ + public function addMetas(array $metas); + + /** + * Remove a meta from the meta collection by key. + * + * @param string|array $names + * + * @return self + */ + public function removeMeta($names); + + /** + * Reset the meta collection except the description and keywords metas. + * + * @return self + */ + public function resetMetas(); } diff --git a/src/SeoMeta.php b/src/SeoMeta.php index a78e581..a500fa3 100644 --- a/src/SeoMeta.php +++ b/src/SeoMeta.php @@ -1,9 +1,9 @@ configs = $configs; - $this->init(); - } + $this->configs = $configs; - /** - * Start the engine. - */ - private function init() - { - $this->title = new Title($this->getConfig('title', [])); - $this->description = new Description($this->getConfig('description', [])); - $this->keywords = new Keywords($this->getConfig('keywords', [])); - $this->misc = new MiscTags($this->getConfig('misc', [])); + // Init the entities + $this->title = new Entities\Title($this->getConfig('title', [])); + $this->description = new Entities\Description($this->getConfig('description', [])); + $this->keywords = new Entities\Keywords($this->getConfig('keywords', [])); + $this->misc = new Entities\MiscTags($this->getConfig('misc', [])); } /* ------------------------------------------------------------------------------------------------ @@ -200,6 +194,25 @@ public function setUrl($url) return $this; } + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Render all seo tags. + * + * @return string + */ + public function render() + { + return implode(PHP_EOL, array_filter([ + $this->title->render(), + $this->description->render(), + $this->keywords->render(), + $this->misc->render(), + ])); + } + /** * Add a meta tag. * @@ -243,63 +256,16 @@ public function removeMeta($names) return $this; } - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ - */ - /** - * Render all seo tags. - * - * @return string - */ - public function render() - { - return implode(PHP_EOL, array_filter([ - $this->renderTitle(), - $this->renderDescription(), - $this->renderKeywords(), - $this->renderMisc(), - ])); - } - /** - * Render title tag. + * Reset the meta collection except the description and keywords metas. * - * @return string - */ - public function renderTitle() - { - return $this->title->render(); - } - - /** - * Render description tag. - * - * @return string - */ - public function renderDescription() - { - return $this->description->render(); - } - - /** - * Render keywords tag. - * - * @return string + * @return self */ - public function renderKeywords() + public function resetMetas() { - return $this->keywords->render(); - } + $this->misc->reset(); - /** - * Render Miscellaneous tags. - * - * @return string - */ - public function renderMisc() - { - return $this->misc->render(); + return $this; } /* ------------------------------------------------------------------------------------------------ diff --git a/tests/SeoMetaTest.php b/tests/SeoMetaTest.php index 0a457c5..0c9c3a4 100644 --- a/tests/SeoMetaTest.php +++ b/tests/SeoMetaTest.php @@ -66,27 +66,27 @@ public function it_can_set_and_get_and_render_title() $this->seoMeta->setTitle($title); $this->assertEquals($title, $this->seoMeta->getTitle()); - $this->assertEquals( + $this->assertContains( '' . $title . '', - $this->seoMeta->renderTitle() + $this->seoMeta->render() ); $siteName = 'Company name'; $this->seoMeta->setTitle($title, $siteName); $this->assertEquals($title, $this->seoMeta->getTitle()); - $this->assertEquals( + $this->assertContains( '' . $title . ' - ' . $siteName . '', - $this->seoMeta->renderTitle() + $this->seoMeta->render() ); $separator = '|'; $this->seoMeta->setTitle($title, $siteName, $separator); $this->assertEquals($title, $this->seoMeta->getTitle()); - $this->assertEquals( + $this->assertContains( "$title $separator $siteName", - $this->seoMeta->renderTitle() + $this->seoMeta->render() ); } @@ -98,9 +98,9 @@ public function it_can_set_and_get_and_render_description() $this->seoMeta->setDescription($description); $this->assertEquals($description, $this->seoMeta->getDescription()); - $this->assertEquals( + $this->assertContains( '', - $this->seoMeta->renderDescription() + $this->seoMeta->render() ); } @@ -112,21 +112,21 @@ public function it_can_set_and_get_and_render_keywords() $this->seoMeta->setKeywords($keywords); $this->assertEquals($keywords, $this->seoMeta->getKeywords()); - $this->assertEquals( + $this->assertContains( '', - $this->seoMeta->renderKeywords() + $this->seoMeta->render() ); $this->seoMeta->setKeywords(implode(',', $keywords)); $this->assertEquals($keywords, $this->seoMeta->getKeywords()); - $this->assertEquals( + $this->assertContains( '', - $this->seoMeta->renderKeywords() + $this->seoMeta->render() ); $this->seoMeta->setKeywords(null); - $this->assertEmpty($this->seoMeta->renderKeywords()); + $this->assertNotContains('name="keywords"', $this->seoMeta->render()); } /** @test */ @@ -144,7 +144,7 @@ public function it_can_add_one_keyword() } /** @test */ - public function it_can_add_remove_and_render_a_misc_tag() + public function it_can_add_remove_reset_and_render_a_misc_tag() { $output = $this->seoMeta->render(); @@ -196,5 +196,16 @@ public function it_can_add_remove_and_render_a_misc_tag() '', $this->seoMeta->render() ); + + $this->seoMeta->addMetas([ + 'copyright' => 'ARCANEDEV', + 'expires' => 'never', + ]); + + $this->seoMeta->resetMetas(); + + foreach (['viewport', 'copyright', 'expires'] as $blacklisted) { + $this->assertNotContains($blacklisted, $this->seoMeta->render()); + } } }