Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Handle arrays in ld+json for date extensions. Fixes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Scott committed Jul 1, 2018
1 parent 0585f6d commit c8dae28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Modules/Extractors/PublishDateExtractor.php
Expand Up @@ -108,7 +108,11 @@ private function getDateFromSchemaOrg(): ?\DateTime {
try {
$json = json_decode($node->text());
if (isset($json->datePublished)) {
$dt = new \DateTime($json->datePublished);
$date = is_array($json->datePublished)
? array_shift($json->datePublished)
: $json->datePublished;

$dt = new \DateTime($date);
break;
}
}
Expand Down Expand Up @@ -202,7 +206,11 @@ private function getDateFromParsely(): ?\DateTime {
try {
$json = json_decode($node->text());
if (isset($json->dateCreated)) {
$dt = new \DateTime($json->dateCreated);
$date = is_array($json->dateCreated)
? array_shift($json->dateCreated)
: $json->dateCreated;

$dt = new \DateTime($date);
break;
}
}
Expand Down
12 changes: 11 additions & 1 deletion tests/Modules/Extractors/PublishDateExtractorTest.php
Expand Up @@ -74,6 +74,11 @@ public function getDateFromSchemaOrgProvider() {
$this->document('<html><head><title>Example Article</title><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","author":"John Smith","datePublished":"2016-05-31"}</script></head></html>'),
'Valid date with JSON-LD and attribute: "datePublished"'
],
[
new \DateTime('2016-05-31'),
$this->document('<html><head><title>Example Article</title><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","author":"John Smith","datePublished":["2016-05-31"]}</script></head></html>'),
'Valid date with JSON-LD and attribute: "datePublished"'
],
[
null,
$this->document('<html><head><title>Example Article</title></head></html>'),
Expand Down Expand Up @@ -197,7 +202,12 @@ public function getDateFromParselyProvider() {
[
new \DateTime('2016-05-31T22:52:11Z'),
$this->document('<html><head><title>Example Article</title><script type="application/ld+json">{"@context":"http://schema.org","@type":"NewsArticle","creator":["John Smith"],"dateCreated":"2016-05-31T22:52:11Z"}</script></head></html>'),
'Valid date with JSON-LD and attribute: "datePublished"'
'Valid date with JSON-LD and attribute: "dateCreated"'
],
[
new \DateTime('2016-05-31T22:52:11Z'),
$this->document('<html><head><title>Example Article</title><script type="application/ld+json">{"@context":"http://schema.org","@type":"NewsArticle","creator":["John Smith"],"dateCreated":["2016-05-31T22:52:11Z"]}</script></head></html>'),
'Valid date with JSON-LD and attribute: "dateCreated"'
],
[
new \DateTime('2016-05-31T22:52:11Z'),
Expand Down

0 comments on commit c8dae28

Please sign in to comment.