Skip to content

Commit

Permalink
Merge pull request #229 from StApostol/master
Browse files Browse the repository at this point in the history
Added support self object in values for JsonLd
  • Loading branch information
vinicius73 committed Oct 14, 2020
2 parents 5872144 + f261305 commit bad3f02
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/SEOTools/JsonLd.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,68 @@ public function isEmpty()
/**
* {@inheritdoc}
*/
public function generate($minify = false)
public function generate($minify = false): string
{
$generated = [
'@context' => 'https://schema.org',
];
$generated = array_merge(
[
'@context' => 'https://schema.org',
],
$this->convertToArray()
);

if (! empty($this->type)) {
return '<script type="application/ld+json">' . json_encode($generated, JSON_UNESCAPED_UNICODE) . '</script>';
}

/**
* @return string[]|string[][]
*/
public function convertToArray(): array
{
$generated = [];

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

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

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

if ($this->url !== false) {
$generated['url'] = $this->url ?? app('url')->full();
}

if (! empty($this->images)) {
if (!empty($this->images)) {
$generated['image'] = count($this->images) === 1 ? reset($this->images) : $this->images;
}

$generated = array_merge($generated, $this->values);
return self::convertSelfObjectInArray(array_merge($generated, $this->values));
}

return '<script type="application/ld+json">' . json_encode($generated, JSON_UNESCAPED_UNICODE) . '</script>';
/**
* @param mixed[] $values
*
* @return string[]|string[][]
*/
private static function convertSelfObjectInArray(array $values): array
{
foreach ($values as $key => $value) {
if (is_array($value)) {
$values[$key] = self::convertSelfObjectInArray($value);

continue;
}

if ($value instanceof self) {
$values[$key] = $value->convertToArray();
}
}

return $values;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/SEOTools/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ public function test_array_add_value()
$this->setRightAssertion($expected);
}

public function test_self_add_value()
{
$this->jsonLd->addValue('author', new JsonLd([
'type' => 'Organization',
'name' => 'SeoTools',
'url' => 'https://github.com/artesaos/seotools',
]));

$expected = '<html><head><script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"WebPage","name":"Over 9000 Thousand!","description":"For those who helped create the Genki Dama","author":{"@type":"Organization","url":"https:\/\/github.com\/artesaos\/seotools","name":"SeoTools"}}</script></head></html>';

$this->setRightAssertion($expected);
}

public function test_self_array_add_value()
{
$this->jsonLd->addValue('author', [new JsonLd([
'type' => 'Organization',
'name' => 'SeoTools',
'url' => 'https://github.com/artesaos/seotools',
])]);

$expected = '<html><head><script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"WebPage","name":"Over 9000 Thousand!","description":"For those who helped create the Genki Dama","author":[{"@type":"Organization","url":"https:\/\/github.com\/artesaos\/seotools","name":"SeoTools"}]}</script></head></html>';

$this->setRightAssertion($expected);
}

public function test_add_values()
{
$this->jsonLd->addValues([
Expand Down

0 comments on commit bad3f02

Please sign in to comment.