From 898035f60fecc38f1407de91bb9f299a48028e28 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Tue, 13 Aug 2019 15:31:51 +0300 Subject: [PATCH 1/6] #167: add `JsonLd::setName()` --- src/SEOTools/Contracts/JsonLd.php | 9 +++++++++ src/SEOTools/JsonLd.php | 18 +++++++++++++----- src/SEOTools/SEOTools.php | 2 +- tests/SEOTools/JsonLdTest.php | 12 ++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/SEOTools/Contracts/JsonLd.php b/src/SEOTools/Contracts/JsonLd.php index 3b428a8..7019313 100644 --- a/src/SEOTools/Contracts/JsonLd.php +++ b/src/SEOTools/Contracts/JsonLd.php @@ -34,6 +34,15 @@ public function addValue($key, $value); public function setType($type); /** + * @param string $name + * + * @return static + */ + public function setName($name); + + /** + * Alias of {@see setName()}. + * * @param string $title * * @return static diff --git a/src/SEOTools/JsonLd.php b/src/SEOTools/JsonLd.php index 6c005ce..b523565 100644 --- a/src/SEOTools/JsonLd.php +++ b/src/SEOTools/JsonLd.php @@ -19,7 +19,7 @@ class JsonLd implements JsonLdContract /** * @var string */ - protected $title = ''; + protected $name = ''; /** * @var string @@ -73,8 +73,8 @@ public function generate($minify = false) } - if (!empty($this->title)) { - $generated['name'] = $this->title; + if (!empty($this->name)) { + $generated['name'] = $this->name; } @@ -118,13 +118,21 @@ public function setType($type) /** * {@inheritdoc} */ - public function setTitle($title) + public function setName($name) { - $this->title = $title; + $this->name = $name; return $this; } + /** + * {@inheritdoc} + */ + public function setTitle($title) + { + return $this->setName($title); + } + /** * {@inheritdoc} */ diff --git a/src/SEOTools/SEOTools.php b/src/SEOTools/SEOTools.php index e9ff511..f83e81c 100644 --- a/src/SEOTools/SEOTools.php +++ b/src/SEOTools/SEOTools.php @@ -46,7 +46,7 @@ public function setTitle($title, $appendDefault = true) $this->metatags()->setTitle($title, $appendDefault); $this->opengraph()->setTitle($title); $this->twitter()->setTitle($title); - $this->jsonLd()->setTitle($title); + $this->jsonLd()->setName($title); return $this; } diff --git a/tests/SEOTools/JsonLdTest.php b/tests/SEOTools/JsonLdTest.php index c4b6225..7431130 100644 --- a/tests/SEOTools/JsonLdTest.php +++ b/tests/SEOTools/JsonLdTest.php @@ -24,6 +24,18 @@ protected function setUp(): void $this->jsonLd = $this->app->make('seotools.json-ld'); } + public function test_set_name() + { + $this->jsonLd->setName('Kamehamehaaaaaaaa'); + + $expected = ''; + + $this->setRightAssertion($expected); + } + + /** + * @depends test_set_name + */ public function test_set_title() { $this->jsonLd->setTitle('Kamehamehaaaaaaaa'); From 30a103cc2c6a09761ccba84eba8be233110c7dde Mon Sep 17 00:00:00 2001 From: J-Brk Date: Sat, 12 Feb 2022 14:40:06 +0100 Subject: [PATCH 2/6] Update JsonLd.php --- src/SEOTools/Contracts/JsonLd.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SEOTools/Contracts/JsonLd.php b/src/SEOTools/Contracts/JsonLd.php index 9faafe0..1c19895 100644 --- a/src/SEOTools/Contracts/JsonLd.php +++ b/src/SEOTools/Contracts/JsonLd.php @@ -13,7 +13,7 @@ * $jsonLd = new JsonLd(); * * // specify JSON data - * $jsonLd->setTitle('Home'); + * $jsonLd->setName('Home'); * $jsonLd->setDescription('This is my page description'); * $jsonLd->addValue('author', [ * '@type' => 'Organization', @@ -31,7 +31,7 @@ * use Artesaos\SEOTools\Facades\JsonLd; * * // specify JSON data - * JsonLd::setTitle('Homepage'); + * JsonLd::setName('Homepage'); * JsonLd::setDescription('This is my page description'); * JsonLd::addValue('author', [ * '@type' => 'Organization', From 1ce8deadf98d549b501a358e3127fe4762fd7d9c Mon Sep 17 00:00:00 2001 From: J-Brk Date: Sat, 12 Feb 2022 14:43:25 +0100 Subject: [PATCH 3/6] Adjustments for switch to setName --- src/SEOTools/JsonLd.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/SEOTools/JsonLd.php b/src/SEOTools/JsonLd.php index 6011082..20ffdff 100644 --- a/src/SEOTools/JsonLd.php +++ b/src/SEOTools/JsonLd.php @@ -41,6 +41,12 @@ class JsonLd implements JsonLdContract */ public function __construct(array $defaults = []) { + if (key_exists('name', $defaults)) { + $this->setName($defaults['name']); + unset($defaults['name']); + } + + // Backwards compatibility if (key_exists('title', $defaults)) { $this->setTitle($defaults['title']); unset($defaults['title']); @@ -76,7 +82,7 @@ public function isEmpty() { return empty($this->values) && empty($this->type) - && empty($this->title) + && empty($this->name) && empty($this->description) && empty($this->url) && empty($this->images); From 49105ae6eba5e007822c04f401ac6616e1492d79 Mon Sep 17 00:00:00 2001 From: J-Brk Date: Sat, 12 Feb 2022 14:50:56 +0100 Subject: [PATCH 4/6] Added support for setName --- src/SEOTools/JsonLdMulti.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/SEOTools/JsonLdMulti.php b/src/SEOTools/JsonLdMulti.php index 1350020..eb66266 100644 --- a/src/SEOTools/JsonLdMulti.php +++ b/src/SEOTools/JsonLdMulti.php @@ -116,12 +116,22 @@ public function setType($type) return $this; } + /** + * {@inheritdoc} + */ + public function setName($name) + { + $this->list[$this->index]->setName($name); + + return $this; + } + /** * {@inheritdoc} */ public function setTitle($title) { - $this->list[$this->index]->setTitle($title); + $this->list[$this->index]->setName($title); return $this; } From 8e97a1305dc6d217fa70faef3c37e869f313dda5 Mon Sep 17 00:00:00 2001 From: J-Brk Date: Sat, 12 Feb 2022 14:52:49 +0100 Subject: [PATCH 5/6] JsonLdMulti.php - Add support setName --- src/SEOTools/Contracts/JsonLdMulti.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/SEOTools/Contracts/JsonLdMulti.php b/src/SEOTools/Contracts/JsonLdMulti.php index 38eef08..332aa71 100644 --- a/src/SEOTools/Contracts/JsonLdMulti.php +++ b/src/SEOTools/Contracts/JsonLdMulti.php @@ -9,14 +9,14 @@ * use Artesaos\SEOTools\JsonLdMulti; // implements `Artesaos\SEOTools\Contracts\JsonLdMulti` * $jsonLdMulti = new JsonLdMulti(); * // specify JSON data - * $jsonLdMulti->setTitle('Home'); + * $jsonLdMulti->setName('Home'); * $jsonLdMulti->setDescription('This is my page description'); * $jsonLdMulti->addValue('author', [ * '@type' => 'Organization', * 'name' => 'Artesaos', * ])); * $jsonLdMulti->newJsonLd(); - * $jsonLdMulti->setTitle('Homepage'); + * $jsonLdMulti->setName('Homepage'); * $jsonLdMulti->setType('Product'); * // render HTML, it should be placed within 'head' HTML tag * echo $jsonLd->generate(); @@ -26,14 +26,14 @@ * ```php * use Artesaos\SEOTools\Facades\JsonLdMulti; * // specify JSON data - * JsonLdMulti::setTitle('Home'); + * JsonLdMulti::setName('Home'); * JsonLdMulti::setDescription('This is my page description'); * JsonLdMulti::addValue('author', [ * '@type' => 'Organization', * 'name' => 'Artesaos', * ])); * JsonLdMulti::newJsonLd(); - * JsonLdMulti::setTitle('Homepage'); + * JsonLdMulti::setName('Homepage'); * JsonLdMulti::setType('Product'); * // render HTML, it should be placed within 'head' HTML tag * echo JsonLdMulti::generate(); @@ -106,6 +106,13 @@ public function addValues(array $values); */ public function setType($type); + /** + * @param string $name + * + * @return static + */ + public function setName($name); + /** * @param string $title * From 2b3fcb39e702bfe8c4bbc8e1710f87e6877e1b6a Mon Sep 17 00:00:00 2001 From: J-Brk Date: Sat, 12 Feb 2022 14:54:40 +0100 Subject: [PATCH 6/6] JsonLdMulti - Add unit test for setName --- tests/SEOTools/JsonLdMultiTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/SEOTools/JsonLdMultiTest.php b/tests/SEOTools/JsonLdMultiTest.php index 3957d0c..a6c95f0 100644 --- a/tests/SEOTools/JsonLdMultiTest.php +++ b/tests/SEOTools/JsonLdMultiTest.php @@ -29,6 +29,16 @@ protected function setUp(): void $this->jsonLdMulti = $this->app->make('seotools.json-ld-multi'); $this->jsonLdMulti->newJsonLd(); } + + public function test_set_name() + { + $this->jsonLdMulti->setName('Kamehamehaaaaaaaa'); + + $expected = '' . $this->defaultJsonLdHtml + . ''; + + $this->setRightAssertion($expected); + } public function test_set_title() {