Passed
Push — master ( 0c72ba...fcb3e8 )
by Wilder
02:18
created

BlogPosting::header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace ElePHPant\StructuredData;
5
6
/**
7
 * Class BlogPosting
8
 * @package ElePHPant\StructuredData
9
 */
10
class BlogPosting extends Schema
11
{
12
    /**
13
     * @var \stdClass
14
     */
15
    private $data;
16
17
    /**
18
     * @see https://schema.org/BlogPosting
19
     */
20
    const TYPE = 'BlogPosting';
21
22
    /**
23
     * BlogPosting constructor.
24
     * @param WebPage $webPage
25
     */
26
    public function __construct(WebPage $webPage)
27
    {
28
        $this->webPageName = $webPage->name();
29
        $this->webPageUrl = $webPage->url();
30
        $this->webPageSameAs = $webPage->sameAs();
31
        $this->data = new \stdClass();
32
        parent::__construct();
33
    }
34
35
    /**
36
     * @return object
37
     */
38
    public function header(): object
39
    {
40
        return $this->data->header;
41
    }
42
43
    /**
44
     * @param string $headline
45
     * @param string $description
46
     * @param string $articleBody
47
     * @param string $datePublished
48
     * @param string|null $dateModified
49
     * @return BlogPosting
50
     */
51
    public function start(
52
        string $headline,
53
        string $description,
54
        string $articleBody,
55
        string $datePublished,
56
        ?string $dateModified = null
57
    ): BlogPosting {
58
        $this->data->header = (object)[
59
            'headline' => $headline,
60
            'description' => $description,
61
            'articleBody' => strip_tags($articleBody),
62
            'datePublished' => $this->setDate($datePublished),
63
            'dateModified' => (!is_null($dateModified) ? $this->setDate($dateModified) : $this->setDate($datePublished))
64
        ];
65
        return $this;
66
    }
67
68
    /**
69
     * @see https://schema.org/mainEntityOfPage
70
     *
71
     * @param string $url
72
     * @param string $type
73
     * @return BlogPosting
74
     */
75
    public function mainEntityOfPage(string $url, string $type = Schema::TYPE): BlogPosting
76
    {
77
        $this->data->mainEntityOfPage = [
78
            '@type' => $type,
79
            '@id' => $url
80
        ];
81
        return $this;
82
    }
83
84
    /**
85
     * @see https://schema.org/image
86
     *
87
     * @param string $url
88
     * @return array
89
     */
90
    public function image(string $url): array
91
    {
92
        return $this->imageObject($url);
93
    }
94
95
    /**
96
     * @see https://schema.org/author
97
     *
98
     * @param string $name
99
     * @param string $image
100
     * @param array|null $sameAs
101
     * @return object
102
     */
103
    public function author(string $name, string $image, array $sameAs = null): object
104
    {
105
        return $this->person($name, $image, $sameAs);
106
    }
107
108
    /**
109
     * @see https://schema.org/publisher
110
     *
111
     * @param string $image
112
     * @return object
113
     */
114
    public function publisher(string $image): object
115
    {
116
        return $this->organization($image, $image);
117
    }
118
119
    /**
120
     * @return object
121
     */
122
    public function data(): object
123
    {
124
        $this->data = (object)[
125
            'header' => $this->header(),
126
            'mainEntityOfPage' => (object)$this->data->mainEntityOfPage,
127
            'image' => (object)$this->imageObject,
128
            'author' => (object)$this->person,
129
            'publisher' => (object)$this->organization
130
        ];
131
        return $this->data;
132
    }
133
134
    /**
135
     * @param bool $tag
136
     * @return string
137
     */
138
    public function render(bool $tag = false): string
139
    {
140
        $render = [
141
            '@context' => self::CONTEXT,
142
            '@type' => self::TYPE,
143
            'headline' => $this->data->header->headline,
144
            'description' => $this->data->header->description,
145
            'datePublished' => $this->data->header->datePublished,
146
            'dateModified' => $this->data->header->dateModified,
147
            'articleBody' => $this->data->header->articleBody,
148
            'mainEntityOfPage' => $this->data->mainEntityOfPage,
149
            'image' => $this->imageObject,
150
            'author' => $this->person,
151
            'publisher' => [$this->organization]
152
        ];
153
154
        if ($tag) {
155
            return '<script type="application/ld+json">' . $this->json($render) . '</script>';
156
        }
157
158
        return $this->json($render);
159
    }
160
161
    /**
162
     * Responsible method for debug all data
163
     */
164
    public function debug(): void
165
    {
166
        var_dump($this->data());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->data()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
167
    }
168
}