From 2cf9c36383eec662d1cccc8adecf8a8f6ebaed06 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Tue, 7 Mar 2017 17:40:35 +0000 Subject: [PATCH] Adding Meta accessors --- src/Models/Meta.php | 6 +++ src/Models/Presenters/MetaPresenter.php | 62 +++++++++++++++++++++++++ tests/Models/MetaTest.php | 20 ++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/Models/Presenters/MetaPresenter.php diff --git a/src/Models/Meta.php b/src/Models/Meta.php index 60b75e0..28eeb6d 100644 --- a/src/Models/Meta.php +++ b/src/Models/Meta.php @@ -24,6 +24,12 @@ */ class Meta extends AbstractModel { + /* ----------------------------------------------------------------- + | Traits + | ----------------------------------------------------------------- + */ + use Presenters\MetaPresenter; + /* ----------------------------------------------------------------- | Properties | ----------------------------------------------------------------- diff --git a/src/Models/Presenters/MetaPresenter.php b/src/Models/Presenters/MetaPresenter.php new file mode 100644 index 0000000..6b5c9ac --- /dev/null +++ b/src/Models/Presenters/MetaPresenter.php @@ -0,0 +1,62 @@ + + * + * @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); + } +} diff --git a/tests/Models/MetaTest.php b/tests/Models/MetaTest.php index d9ac60d..be9c14b 100644 --- a/tests/Models/MetaTest.php +++ b/tests/Models/MetaTest.php @@ -1,5 +1,6 @@ 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 | ------------------------------------------------------------------------------------------------