1
|
|
|
<?php
|
2
|
|
|
declare(strict_types=1);
|
3
|
|
|
|
4
|
|
|
namespace SKien\JsonLD;
|
5
|
|
|
|
6
|
|
|
/**
|
7
|
|
|
* Adding structured data to your news, blog or other info article page.
|
8
|
|
|
*
|
9
|
|
|
* #### Properties:
|
10
|
|
|
*
|
11
|
|
|
* | Name | Description | Necessity |
|
12
|
|
|
* |------------------|-----------------------------------------------|------------|
|
13
|
|
|
* | author | Person or Organization | required |
|
14
|
|
|
* | datePublished | Date and time the article was first published | required |
|
15
|
|
|
* | headline | The headline of the article | required |
|
16
|
|
|
* | image | An image representing the page | required |
|
17
|
|
|
* | publisher | Publisher of the article (organization) | required |
|
18
|
|
|
* | dateModified | Date and time the article was last modified | recomended |
|
19
|
|
|
* | mainEntityOfPage | The canonical URL of the article page | recomended |
|
20
|
|
|
*
|
21
|
|
|
*
|
22
|
|
|
* #### Notes:
|
23
|
|
|
* - Headlines MUST not be longer than 110 characters. The class truncates longer values
|
24
|
|
|
* with ellipsis (...)
|
25
|
|
|
* - The datePublished may not be changed later (sett dateModified instead). It is recommended to
|
26
|
|
|
* include the hour in the time stamp in addition to the day.
|
27
|
|
|
* - The dateModified value must be after the datePublished value.
|
28
|
|
|
*
|
29
|
|
|
* @link https://developers.google.com/search/docs/data-types/article
|
30
|
|
|
* @link https://schema.org/Article
|
31
|
|
|
*
|
32
|
|
|
* @package JsonLD
|
33
|
|
|
* @author Stefanius <[email protected]>
|
34
|
|
|
* @copyright MIT License - see the LICENSE file for details
|
35
|
|
|
*/
|
36
|
|
|
class JsonLDArticle extends JsonLD
|
37
|
|
|
{
|
38
|
|
|
/**
|
39
|
|
|
* Initializes a JsonLD object for article.
|
40
|
|
|
* The main entity of the page is set to the webPage with
|
41
|
|
|
* @id = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
|
42
|
|
|
* @param string $strType possible values 'Article', 'NewsArticle' or 'BlogPosting'
|
43
|
|
|
*/
|
44
|
|
|
public function __construct(string $strType = 'NewsArticle')
|
45
|
|
|
{
|
46
|
|
|
parent::__construct(self::ARTICLE, $strType);
|
47
|
|
|
$strID = $_SERVER['HTTP_HOST'] ?? 'UNKNOWN_HOST';
|
48
|
|
|
$strID .= $_SERVER['REQUEST_URI'] ?? '_UNKNOWN_REQUEST_URI';
|
49
|
|
|
$this->aJsonLD["mainEntityOfPage"] = array(
|
50
|
|
|
"@type" => "WebPage",
|
51
|
|
|
"id" => $strID,
|
52
|
|
|
);
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Set the publisher of the article.
|
57
|
|
|
* Name is mandatory.
|
58
|
|
|
* @param string $strName
|
59
|
|
|
* @param string $strEMail
|
60
|
|
|
* @param string $strPhone
|
61
|
|
|
*/
|
62
|
|
|
public function setPublisher(string $strName, string $strEMail = '', string $strPhone = '') : void
|
63
|
|
|
{
|
64
|
|
|
$strName = $this->validString($strName);
|
65
|
|
|
$strEMail = $this->validEMail($strEMail);
|
66
|
|
|
if (strlen($strName) > 0) {
|
67
|
|
|
if (!isset($this->aJsonLD["publisher"])) {
|
68
|
|
|
$this->aJsonLD["publisher"] = array("@type" => "Organization");
|
69
|
|
|
}
|
70
|
|
|
$this->aJsonLD["publisher"]["name"] = $strName;
|
71
|
|
|
if (strlen($strEMail) > 0) {
|
72
|
|
|
$this->aJsonLD["publisher"]["email"] = $strEMail;
|
73
|
|
|
}
|
74
|
|
|
$strPhone = $this->validString($strPhone);
|
75
|
|
|
if (strlen($strPhone) > 0) {
|
76
|
|
|
$this->aJsonLD["publisher"]["telephone"] = $strPhone;
|
77
|
|
|
}
|
78
|
|
|
}
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* Set the logo of the publisher.
|
83
|
|
|
* @param string $strLogoURL
|
84
|
|
|
*/
|
85
|
|
|
public function setLogo(string $strLogoURL) : void
|
86
|
|
|
{
|
87
|
|
|
$aLogo = $this->buildImageObject($strLogoURL);
|
88
|
|
|
if ($aLogo !== null) {
|
89
|
|
|
if (!isset($this->aJsonLD["publisher"])) {
|
90
|
|
|
$this->aJsonLD["publisher"] = array("@type" => "Organization");
|
91
|
|
|
}
|
92
|
|
|
$this->aJsonLD["publisher"]["logo"] = $aLogo;
|
93
|
|
|
}
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* Set infos to the article on this page.
|
98
|
|
|
* Headlines cannot be longer than 110 characters. Longer text will be truncated with ellipsis (...)
|
99
|
|
|
* @param string $strHeadline
|
100
|
|
|
* @param string $strDescription
|
101
|
|
|
* @param mixed $published can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
|
102
|
|
|
* @param mixed $modified can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
|
103
|
|
|
*/
|
104
|
|
|
public function setInfo(string $strHeadline, string $strDescription, $published, $modified = null) : void
|
105
|
|
|
{
|
106
|
|
|
$strHeadline = $this->validString($strHeadline);
|
107
|
|
|
$strHeadline = $this->strTruncateEllipsis($strHeadline, 110);
|
108
|
|
|
if (strlen($strHeadline) > 0) {
|
109
|
|
|
$this->aJsonLD["headline"] = $strHeadline;
|
110
|
|
|
$this->aJsonLD["description"] = $this->validString($strDescription);
|
111
|
|
|
if ($published != null) {
|
112
|
|
|
$this->aJsonLD["datePublished"] = $this->validDate($published);
|
113
|
|
|
}
|
114
|
|
|
if ($modified != null) {
|
115
|
|
|
$this->aJsonLD["dateModified"] = $this->validDate($modified);
|
116
|
|
|
}
|
117
|
|
|
}
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* Set the authors name
|
122
|
|
|
* @param string $strAuthor
|
123
|
|
|
*/
|
124
|
|
|
public function setAuthor(string $strAuthor) : void
|
125
|
|
|
{
|
126
|
|
|
$strAuthor = $this->validString($strAuthor);
|
127
|
|
|
if (strlen($strAuthor) > 0) {
|
128
|
|
|
$this->aJsonLD["author"] = array(
|
129
|
|
|
"@type" => "Person",
|
130
|
|
|
"name" => $this->validString($strAuthor)
|
131
|
|
|
);
|
132
|
|
|
}
|
133
|
|
|
}
|
134
|
|
|
}
|
135
|
|
|
|