Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #167: add JsonLd::setName() #168

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/SEOTools/Contracts/JsonLd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -92,6 +92,15 @@ public function addValues(array $values);
public function setType($type);

/**
* @param string $name
*
* @return static
*/
public function setName($name);

/**
* Alias of {@see setName()}.
*
* @param string $title
*
* @return static
Expand Down
15 changes: 11 additions & 4 deletions src/SEOTools/Contracts/JsonLdMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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
*
Expand Down
26 changes: 20 additions & 6 deletions src/SEOTools/JsonLd.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class JsonLd implements JsonLdContract
/**
* @var string
*/
protected $title = '';
protected $name = '';
/**
* @var string
*/
Expand All @@ -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']);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -108,8 +114,8 @@ public function convertToArray(): array
$generated['@type'] = $this->type;
}

if (!empty($this->title)) {
$generated['name'] = $this->title;
if (!empty($this->name)) {
$generated['name'] = $this->name;
}

if (!empty($this->description)) {
Expand Down Expand Up @@ -190,13 +196,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}
*/
Expand Down
12 changes: 11 additions & 1 deletion src/SEOTools/JsonLdMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SEOTools/SEOTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/SEOTools/JsonLdMultiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<html><head>' . $this->defaultJsonLdHtml
. '<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"Kamehamehaaaaaaaa","description":"For those who helped create the Genki Dama"}</script></head></html>';

$this->setRightAssertion($expected);
}

public function test_set_title()
{
Expand Down
12 changes: 12 additions & 0 deletions tests/SEOTools/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ protected function setUp(): void
$this->jsonLd = $this->app->make('seotools.json-ld');
}

public function test_set_name()
{
$this->jsonLd->setName('Kamehamehaaaaaaaa');

$expected = '<html><head><script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"WebPage","name":"Kamehamehaaaaaaaa","description":"For those who helped create the Genki Dama"}</script></head></html>';

$this->setRightAssertion($expected);
}

/**
* @depends test_set_name
*/
public function test_set_title()
{
$this->jsonLd->setTitle('Kamehamehaaaaaaaa');
Expand Down