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 | ------------------------------------------------------------------------------------------------