Passed
Push — master ( 24e0e0...391762 )
by Peter
02:06
created

Page::setRenderedBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Domain\Entities;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use AbterPhp\Website\Domain\Entities\Page\Assets;
9
use AbterPhp\Website\Domain\Entities\Page\Meta;
10
11
class Page implements IStringerEntity
12
{
13
    /** @var string */
14
    protected $id;
15
16
    /** @var string */
17
    protected $identifier;
18
19
    /** @var string */
20
    protected $title;
21
22
    /** @var string */
23
    protected $body;
24
25
    /** @var string|null */
26
    protected $renderedBody;
27
28
    /** @var PageCategory|null */
29
    protected $category;
30
31
    /** @var string */
32
    protected $layout;
33
34
    /** @var string|null */
35
    protected $layoutId;
36
37
    /** @var Meta */
38
    protected $meta;
39
40
    /** @var Assets|null */
41
    protected $assets;
42
43
    /**
44
     * Page constructor.
45
     *
46
     * @param string            $id
47
     * @param string            $identifier
48
     * @param string            $title
49
     * @param string            $body
50
     * @param PageCategory|null $category
51
     * @param string            $layout
52
     * @param string|null       $layoutId
53
     * @param Meta|null         $meta
54
     * @param Assets|null       $assets
55
     * @param string|null       $renderedBody
56
     */
57
    public function __construct(
58
        string $id,
59
        string $identifier,
60
        string $title,
61
        string $body,
62
        ?PageCategory $category = null,
63
        string $layout = '',
64
        ?string $layoutId = null,
65
        ?Meta $meta = null,
66
        ?Assets $assets = null,
67
        ?string $renderedBody = null
68
    ) {
69
        $this->id           = $id;
70
        $this->identifier   = $identifier;
71
        $this->title        = $title;
72
        $this->body         = $body;
73
        $this->category     = $category;
74
        $this->layout       = $layout;
75
        $this->layoutId     = $layoutId ? $layoutId : null;
76
        $this->meta         = $meta ?: new Meta('', '', '', '', '', '', '', '');
77
        $this->assets       = $assets;
78
        $this->renderedBody = $renderedBody;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getId()
85
    {
86
        return $this->id;
87
    }
88
89
    /**
90
     * @param string $id
91
     */
92
    public function setId($id)
93
    {
94
        $this->id = $id;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getIdentifier(): string
101
    {
102
        return $this->identifier;
103
    }
104
105
    /**
106
     * @param string $identifier
107
     *
108
     * @return $this
109
     */
110
    public function setIdentifier(string $identifier): Page
111
    {
112
        $this->identifier = $identifier;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getTitle(): string
121
    {
122
        return $this->title;
123
    }
124
125
    /**
126
     * @param string $title
127
     *
128
     * @return $this
129
     */
130
    public function setTitle(string $title): Page
131
    {
132
        $this->title = $title;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getBody(): string
141
    {
142
        return $this->body;
143
    }
144
145
    /**
146
     * @param string $body
147
     *
148
     * @return $this
149
     */
150
    public function setBody(string $body): Page
151
    {
152
        $this->body = $body;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function hasRenderedBody(): bool
161
    {
162
        return !($this->renderedBody === null);
163
    }
164
165
    /**
166
     * @return string|null
167
     */
168
    public function getRenderedBody(): ?string
169
    {
170
        return $this->renderedBody;
171
    }
172
173
    /**
174
     * @param string|null $renderedBody
175
     *
176
     * @return $this
177
     */
178
    public function setRenderedBody(?string $renderedBody): Page
179
    {
180
        $this->renderedBody = $renderedBody;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return PageCategory|null
187
     */
188
    public function getCategory(): ?PageCategory
189
    {
190
        return $this->category;
191
    }
192
193
    /**
194
     * @param PageCategory|null $category
195
     *
196
     * @return $this
197
     */
198
    public function setCategory(?PageCategory $category): Page
199
    {
200
        $this->category = $category;
201
202
        return $this;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getLayout(): string
209
    {
210
        return $this->layout;
211
    }
212
213
    /**
214
     * @param string $layout
215
     *
216
     * @return $this
217
     */
218
    public function setLayout(string $layout): Page
219
    {
220
        $this->layout = $layout;
221
222
        return $this;
223
    }
224
225
    /**
226
     * @return string|null
227
     */
228
    public function getLayoutId(): ?string
229
    {
230
        return $this->layoutId;
231
    }
232
233
    /**
234
     * @param string|null $layout
235
     *
236
     * @return $this
237
     */
238
    public function setLayoutId(?string $layoutId): Page
239
    {
240
        if ($layoutId === '') {
241
            $layoutId = null;
242
        }
243
244
        $this->layoutId = $layoutId;
245
246
        return $this;
247
    }
248
249
    /**
250
     * @return Meta
251
     */
252
    public function getMeta(): Meta
253
    {
254
        return $this->meta;
255
    }
256
257
    /**
258
     * @param Meta $meta
259
     *
260
     * @return $this
261
     */
262
    public function setMeta(Meta $meta): Page
263
    {
264
        $this->meta = $meta;
265
266
        return $this;
267
    }
268
269
    /**
270
     * @return Assets|null
271
     */
272
    public function getAssets(): ?Assets
273
    {
274
        return $this->assets;
275
    }
276
277
    /**
278
     * @param Assets|null $header
279
     *
280
     * @return $this
281
     */
282
    public function setAssets(?Assets $assets): Page
283
    {
284
        $this->assets = $assets;
285
286
        return $this;
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function __toString(): string
293
    {
294
        return $this->getIdentifier();
295
    }
296
297
    /**
298
     * @return string
299
     */
300
    public function toJSON(): string
301
    {
302
        $meta   = $this->getMeta();
303
        $assets = $this->getAssets();
304
305
        $category = null;
306
        if ($this->getCategory()) {
307
            $category = [
308
                'id' => $this->getCategory()->getId(),
309
            ];
310
        }
311
312
        $assetsData = null;
313
        if ($assets) {
314
            $assetsData = [
315
                'key'       => $assets->getKey(),
316
                'header'    => $assets->getHeader(),
317
                'footer'    => $assets->getFooter(),
318
                'css_files' => $assets->getCssFiles(),
319
                'js_files'  => $assets->getJsFiles(),
320
            ];
321
        }
322
323
        $data = [
324
                'id'         => $this->getId(),
325
                'identifier' => $this->getIdentifier(),
326
                'title'      => $this->getTitle(),
327
                'body'       => $this->getBody(),
328
                'category'   => $category,
329
                'layout'     => $this->getLayout(),
330
                'layout_id'  => $this->getLayoutId(),
331
                'meta'       => [
332
                    'description'    => $meta->getDescription(),
333
                    'robots'         => $meta->getRobots(),
334
                    'author'         => $meta->getAuthor(),
335
                    'copyright'      => $meta->getCopyright(),
336
                    'keywords'       => $meta->getKeywords(),
337
                    'og_title'       => $meta->getOGTitle(),
338
                    'og_image'       => $meta->getOGImage(),
339
                    'og_description' => $meta->getOGDescription(),
340
                ],
341
                'assets'     => $assetsData,
342
            ];
343
344
        if ($this->hasRenderedBody()) {
345
            $data['rendered'] = $this->getRenderedBody();
346
        }
347
348
        return json_encode($data);
349
    }
350
}
351