Passed
Push — master ( 963aa0...23b86c )
by Peter
03:16
created

Page::toJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 34
rs 9.488
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 PageCategory|null */
26
    protected $category;
27
28
    /** @var string */
29
    protected $layout;
30
31
    /** @var string|null */
32
    protected $layoutId;
33
34
    /** @var Meta */
35
    protected $meta;
36
37
    /** @var Assets|null */
38
    protected $assets;
39
40
    /**
41
     * Page constructor.
42
     *
43
     * @param string            $id
44
     * @param string            $identifier
45
     * @param string            $title
46
     * @param string            $body
47
     * @param PageCategory|null $category
48
     * @param string            $layout
49
     * @param string|null       $layoutId
50
     * @param Meta|null         $meta
51
     * @param Assets|null       $assets
52
     */
53
    public function __construct(
54
        string $id,
55
        string $identifier,
56
        string $title,
57
        string $body,
58
        ?PageCategory $category = null,
59
        string $layout = '',
60
        ?string $layoutId = null,
61
        ?Meta $meta = null,
62
        ?Assets $assets = null
63
    ) {
64
        $this->id         = $id;
65
        $this->identifier = $identifier;
66
        $this->title      = $title;
67
        $this->body       = $body;
68
        $this->category   = $category;
69
        $this->layout     = $layout;
70
        $this->layoutId   = $layoutId ? $layoutId : null;
71
        $this->meta       = $meta ?: new Meta('', '', '', '', '', '', '', '');
72
        $this->assets     = $assets;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getId()
79
    {
80
        return $this->id;
81
    }
82
83
    /**
84
     * @param string $id
85
     */
86
    public function setId($id)
87
    {
88
        $this->id = $id;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getIdentifier(): string
95
    {
96
        return $this->identifier;
97
    }
98
99
    /**
100
     * @param string $identifier
101
     *
102
     * @return $this
103
     */
104
    public function setIdentifier(string $identifier): Page
105
    {
106
        $this->identifier = $identifier;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getTitle(): string
115
    {
116
        return $this->title;
117
    }
118
119
    /**
120
     * @param string $title
121
     *
122
     * @return $this
123
     */
124
    public function setTitle(string $title): Page
125
    {
126
        $this->title = $title;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getBody(): string
135
    {
136
        return $this->body;
137
    }
138
139
    /**
140
     * @param string $body
141
     *
142
     * @return $this
143
     */
144
    public function setBody(string $body): Page
145
    {
146
        $this->body = $body;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return PageCategory|null
153
     */
154
    public function getCategory(): ?PageCategory
155
    {
156
        return $this->category;
157
    }
158
159
    /**
160
     * @param PageCategory|null $category
161
     *
162
     * @return $this
163
     */
164
    public function setCategory(?PageCategory $category): Page
165
    {
166
        $this->category = $category;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getLayout(): string
175
    {
176
        return $this->layout;
177
    }
178
179
    /**
180
     * @param string $layout
181
     *
182
     * @return $this
183
     */
184
    public function setLayout(string $layout): Page
185
    {
186
        $this->layout = $layout;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return string|null
193
     */
194
    public function getLayoutId(): ?string
195
    {
196
        return $this->layoutId;
197
    }
198
199
    /**
200
     * @param string|null $layout
201
     *
202
     * @return $this
203
     */
204
    public function setLayoutId(?string $layoutId): Page
205
    {
206
        if ($layoutId === '') {
207
            $layoutId = null;
208
        }
209
210
        $this->layoutId = $layoutId;
211
212
        return $this;
213
    }
214
215
    /**
216
     * @return Meta
217
     */
218
    public function getMeta(): Meta
219
    {
220
        return $this->meta;
221
    }
222
223
    /**
224
     * @param Meta $meta
225
     *
226
     * @return $this
227
     */
228
    public function setMeta(Meta $meta): Page
229
    {
230
        $this->meta = $meta;
231
232
        return $this;
233
    }
234
235
    /**
236
     * @return Assets|null
237
     */
238
    public function getAssets(): ?Assets
239
    {
240
        return $this->assets;
241
    }
242
243
    /**
244
     * @param Assets|null $header
245
     *
246
     * @return $this
247
     */
248
    public function setAssets(?Assets $assets): Page
249
    {
250
        $this->assets = $assets;
251
252
        return $this;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function __toString(): string
259
    {
260
        return $this->getIdentifier();
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    public function toJSON(): string
267
    {
268
        $meta   = $this->getMeta();
269
        $assets = $this->getAssets();
270
271
        return json_encode(
272
            [
273
                'id'         => $this->getId(),
274
                'identifier' => $this->getIdentifier(),
275
                'title'      => $this->getTitle(),
276
                'body'       => $this->getBody(),
277
                'category'   => [
278
                    'id' => $this->getCategory()->getId(),
279
                ],
280
                'layout'     => $this->getLayout(),
281
                'layout_id'  => $this->getLayoutId(),
282
                'meta'       => [
283
                    'description'   => $meta->getDescription(),
284
                    'robots'        => $meta->getRobots(),
285
                    'author'        => $meta->getAuthor(),
286
                    'copyright'     => $meta->getCopyright(),
287
                    'keywords'      => $meta->getKeywords(),
288
                    'og_title'      => $meta->getOGTitle(),
289
                    'og_image'      => $meta->getOGImage(),
290
                    'og_escription' => $meta->getOGDescription(),
291
                ],
292
                'assets'     => [
293
                    'key'       => $assets->getKey(),
294
                    'header'    => $assets->getHeader(),
295
                    'footer'    => $assets->getFooter(),
296
                    'css_files' => $assets->getCssFiles(),
297
                    'js_files'  => $assets->getJsFiles(),
298
                ],
299
                'layout_id'  => $this->getLayoutId(),
300
            ]
301
        );
302
    }
303
}
304