Skip to content

Commit

Permalink
Merge pull request #6 from ARCANEDEV/patch-2
Browse files Browse the repository at this point in the history
Adding Meta accessors
  • Loading branch information
arcanedev-maroc authored Mar 7, 2017
2 parents 6bc787f + 2cf9c36 commit c8a0084
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Models/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
*/
class Meta extends AbstractModel
{
/* -----------------------------------------------------------------
| Traits
| -----------------------------------------------------------------
*/
use Presenters\MetaPresenter;

/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
Expand Down
62 changes: 62 additions & 0 deletions src/Models/Presenters/MetaPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php namespace Arcanedev\LaravelSeo\Models\Presenters;

/**
* Class MetaPresenter
*
* @package Arcanedev\LaravelSeo\Models\Presenters
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*
* @property string title
* @property int title_length
* @property string description
* @property int description_length
* @property \Illuminate\Support\Collection keywords
* @property string keywords_string
* @property int keywords_length
*/
trait MetaPresenter
{
/* -----------------------------------------------------------------
| Accessors
| -----------------------------------------------------------------
*/
/**
* Get the `title_length` attribute.
*
* @return int
*/
public function getTitleLengthAttribute()
{
return strlen($this->title);
}

/**
* Get the `description_length` attribute.
*
* @return int
*/
public function getDescriptionLengthAttribute()
{
return strlen($this->description);
}

/**
* Get the `keywords_string` attribute.
*
* @return string
*/
public function getKeywordsStringAttribute()
{
return $this->keywords->implode(', ');
}

/**
* Get the `keywords_length` attribute.
*
* @return int
*/
public function getKeywordsLengthAttribute()
{
return strlen($this->keywords_string);
}
}
20 changes: 20 additions & 0 deletions tests/Models/MetaTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Arcanedev\LaravelSeo\Tests\Models;

use Arcanedev\LaravelSeo\Models\Meta;
use Arcanedev\LaravelSeo\Tests\Stubs\Models\Post;
use Arcanedev\LaravelSeo\Tests\TestCase;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -150,6 +151,25 @@ public function it_can_get_seoable()
$this->assertEquals($post->title, $seoable->title);
}

/** @test */
public function it_can_get_accessors()
{
$meta = new Meta([
'title' => 'Post Title (SEO)',
'description' => 'Post description (SEO)',
'keywords' => ['keyword-1', 'keyword-2', 'keyword-3', 'keyword-4'],
]);

$this->assertSame(16, $meta->title_length);
$this->assertSame(22, $meta->description_length);
$this->assertSame(4, $meta->keywords->count());
$this->assertSame(
'keyword-1, keyword-2, keyword-3, keyword-4',
$meta->keywords_string
);
$this->assertSame(42, $meta->keywords_length);
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit c8a0084

Please sign in to comment.