Skip to content

Commit

Permalink
Updating SeoMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Oct 13, 2015
1 parent a6f290b commit 58c58cd
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 93 deletions.
39 changes: 39 additions & 0 deletions src/Contracts/SeoMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
124 changes: 45 additions & 79 deletions src/SeoMeta.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace Arcanedev\SeoHelper;

use Arcanedev\SeoHelper\Entities\Description;
use Arcanedev\SeoHelper\Entities\Keywords;
use Arcanedev\SeoHelper\Entities\MiscTags;
use Arcanedev\SeoHelper\Entities\Title;
use Arcanedev\SeoHelper\Contracts\Entities\DescriptionInterface;
use Arcanedev\SeoHelper\Contracts\Entities\KeywordsInterface;
use Arcanedev\SeoHelper\Contracts\Entities\MiscTagsInterface;
use Arcanedev\SeoHelper\Contracts\Entities\TitleInterface;

/**
* Class SeoMeta
Expand All @@ -17,41 +17,41 @@ class SeoMeta implements Contracts\SeoMeta
| Properties
| ------------------------------------------------------------------------------------------------
*/
/**
* Current URL.
*
* @var string
*/
protected $currentUrl = '';

/**
* Title instance.
*
* @var Title
* @var TitleInterface
*/
protected $title;

/**
* Description instance.
*
* @var Description
* @var DescriptionInterface
*/
protected $description;

/**
* Description instance.
*
* @var Keywords
* @var KeywordsInterface
*/
protected $keywords;

/**
* MiscTags instance.
*
* @var MiscTags
* @var MiscTagsInterface
*/
protected $misc;

/**
* Current URL.
*
* @var string
*/
protected $currentUrl = '';

/**
* SEO Helper configs.
*
Expand All @@ -70,19 +70,13 @@ class SeoMeta implements Contracts\SeoMeta
*/
public function __construct(array $configs)
{
$this->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', []));
}

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/* ------------------------------------------------------------------------------------------------
Expand Down
39 changes: 25 additions & 14 deletions tests/SeoMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>' . $title . '</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>' . $title . ' - ' . $siteName . '</title>',
$this->seoMeta->renderTitle()
$this->seoMeta->render()
);

$separator = '|';
$this->seoMeta->setTitle($title, $siteName, $separator);

$this->assertEquals($title, $this->seoMeta->getTitle());
$this->assertEquals(
$this->assertContains(
"<title>$title $separator $siteName</title>",
$this->seoMeta->renderTitle()
$this->seoMeta->render()
);
}

Expand All @@ -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(
'<meta name="description" content="' . $description . '">',
$this->seoMeta->renderDescription()
$this->seoMeta->render()
);
}

Expand All @@ -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(
'<meta name="keywords" content="' . implode(', ', $keywords) . '">',
$this->seoMeta->renderKeywords()
$this->seoMeta->render()
);

$this->seoMeta->setKeywords(implode(',', $keywords));

$this->assertEquals($keywords, $this->seoMeta->getKeywords());
$this->assertEquals(
$this->assertContains(
'<meta name="keywords" content="' . implode(', ', $keywords) . '">',
$this->seoMeta->renderKeywords()
$this->seoMeta->render()
);

$this->seoMeta->setKeywords(null);
$this->assertEmpty($this->seoMeta->renderKeywords());
$this->assertNotContains('name="keywords"', $this->seoMeta->render());
}

/** @test */
Expand All @@ -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();

Expand Down Expand Up @@ -196,5 +196,16 @@ public function it_can_add_remove_and_render_a_misc_tag()
'<meta name="viewport" content="width=device-width, initial-scale=1.0">',
$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());
}
}
}

0 comments on commit 58c58cd

Please sign in to comment.