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

PageLayout::toJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 21
rs 9.7998
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\PageLayout\Assets;
9
10
class PageLayout implements IStringerEntity
11
{
12
    /** @var string */
13
    protected $id;
14
15
    /** @var string */
16
    protected $identifier;
17
18
    /** @var string */
19
    protected $body;
20
21
    /** @var Assets|null */
22
    protected $assets;
23
24
    /**
25
     * Page constructor.
26
     *
27
     * @param string      $id
28
     * @param string      $identifier
29
     * @param string      $body
30
     * @param Assets|null $assets
31
     */
32
    public function __construct(string $id, string $identifier, string $body, ?Assets $assets)
33
    {
34
        $this->id         = $id;
35
        $this->identifier = $identifier;
36
        $this->body       = $body;
37
        $this->assets     = $assets;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getId()
44
    {
45
        return $this->id;
46
    }
47
48
    /**
49
     * @param string $id
50
     */
51
    public function setId($id)
52
    {
53
        $this->id = $id;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getIdentifier(): string
60
    {
61
        return $this->identifier;
62
    }
63
64
    /**
65
     * @param string $identifier
66
     *
67
     * @return $this
68
     */
69
    public function setIdentifier(string $identifier): PageLayout
70
    {
71
        $this->identifier = $identifier;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getBody(): string
80
    {
81
        return $this->body;
82
    }
83
84
    /**
85
     * @param string $body
86
     *
87
     * @return $this
88
     */
89
    public function setBody(string $body): PageLayout
90
    {
91
        $this->body = $body;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @return Assets|null
98
     */
99
    public function getAssets(): ?Assets
100
    {
101
        return $this->assets;
102
    }
103
104
    /**
105
     * @param Assets|null $assets
106
     *
107
     * @return $this
108
     */
109
    public function setAssets(?Assets $assets): PageLayout
110
    {
111
        $this->assets = $assets;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function __toString(): string
120
    {
121
        return $this->getIdentifier();
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function toJSON(): string
128
    {
129
        $assetsData = null;
130
        if ($this->getAssets()) {
131
            $assets = $this->getAssets();
132
133
            $assetsData = [
134
                'key'       => $assets->getKey(),
135
                'header'    => $assets->getHeader(),
136
                'footer'    => $assets->getFooter(),
137
                'css_files' => $assets->getCssFiles(),
138
                'js_files'  => $assets->getJsFiles(),
139
            ];
140
        }
141
142
        return json_encode(
143
            [
144
                'id'         => $this->getId(),
145
                'identifier' => $this->getIdentifier(),
146
                'body'       => $this->getBody(),
147
                'assets'     => $assetsData,
148
            ]
149
        );
150
    }
151
}
152