Skip to content

Commit

Permalink
Merge pull request #5 from ARCANEDEV/patch-1
Browse files Browse the repository at this point in the history
Updating the keywords cast type and the SEO Trait
  • Loading branch information
arcanedev-maroc committed Mar 7, 2017
2 parents e03510a + 783aaaa commit 6bc787f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
26 changes: 25 additions & 1 deletion src/Models/Meta.php
@@ -1,6 +1,7 @@
<?php namespace Arcanedev\LaravelSeo\Models;

use Arcanedev\LaravelSeo\Seo;
use Illuminate\Support\Arr;

/**
* Class Meta
Expand All @@ -13,7 +14,7 @@
* @property string seoable_type
* @property string title
* @property string description
* @property string keywords
* @property \Illuminate\Support\Collection keywords
* @property \Illuminate\Support\Collection metas
* @property boolean noindex
* @property \Carbon\Carbon created_at
Expand Down Expand Up @@ -42,6 +43,7 @@ class Meta extends AbstractModel
protected $casts = [
'id' => 'integer',
'seoable_id' => 'integer',
'keywords' => 'collection',
'metas' => 'collection',
'noindex' => 'boolean',
];
Expand Down Expand Up @@ -73,4 +75,26 @@ public function seoable()
{
return $this->morphTo();
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/
/**
* Prepare the attributes.
*
* @param array $attributes
*
* @return array
*/
public static function prepareAttributes(array $attributes)
{
return [
'title' => Arr::get($attributes, 'title'),
'description' => Arr::get($attributes, 'description'),
'keywords' => Arr::get($attributes, 'keywords', []),
'metas' => Arr::get($attributes, 'metas', []),
'noindex' => Arr::get($attributes, 'noindex', false),
];
}
}
8 changes: 5 additions & 3 deletions src/Traits/Seoable.php
Expand Up @@ -33,19 +33,21 @@ public function seo()
| ------------------------------------------------------------------------------------------------
*/
/**
* Create a seo metas.
* Create a SEO Meta.
*
* @param array $attributes
*
* @return \Arcanedev\LaravelSeo\Models\Meta
*/
public function createSeo(array $attributes)
{
return $this->seo()->create($attributes);
return $this->seo()->create(
Meta::prepareAttributes($attributes)
);
}

/**
* Update a seo.
* Update a SEO Meta.
*
* @param array $attributes
*
Expand Down
8 changes: 7 additions & 1 deletion tests/Models/MetaTest.php
Expand Up @@ -37,6 +37,7 @@ public function it_can_create()
$post->createSeo([
'title' => 'Post Title (SEO)',
'description' => 'Post description (SEO)',
'keywords' => ['keyword-1', 'keyword-2', 'keyword-3', 'keyword-4'],
'metas' => [
'twitter:title' => 'Post title for twitter card',
'twitter:description' => 'Post description for twitter card',
Expand All @@ -55,6 +56,7 @@ public function it_can_create()
'seoable_type' => Post::class,
'title' => 'Post Title (SEO)',
'description' => 'Post description (SEO)',
'keywords' => json_encode(['keyword-1', 'keyword-2', 'keyword-3', 'keyword-4']),
'noindex' => 0
]);

Expand Down Expand Up @@ -86,14 +88,18 @@ public function it_can_update()

// Update
$post->updateSeo([
'title' => 'Updated Post Title (SEO)',
'title' => 'Updated Post Title (SEO)',
'keywords' => ['keyword-1', 'keyword-2', 'keyword-3', 'keyword-4'],
]);

$post = $post->fresh();

$this->assertTrue($post->hasSeo());
$this->assertSame('Updated Post Title (SEO)', $post->seo->title);
$this->assertSame('Post description (SEO)', $post->seo->description);
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $post->seo->keywords);
$this->assertCount(4, $post->seo->keywords);
$this->assertSame(['keyword-1', 'keyword-2', 'keyword-3', 'keyword-4'], $post->seo->keywords->toArray());
}

/** @test */
Expand Down

0 comments on commit 6bc787f

Please sign in to comment.