Completed
Pull Request — master (#2)
by David
02:09
created

Page::getContext()   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
namespace TheCodingMachine\CMS\StaticRegistry\Loaders;
3
4
use Mni\FrontYAML\Parser;
5
6
class Page
7
{
8
    /**
9
     * @var string|null
10
     */
11
    private $id;
12
    /**
13
     * @var string
14
     */
15
    private $title;
16
    /**
17
     * @var string
18
     */
19
    private $content;
20
    /**
21
     * @var string
22
     */
23
    private $url;
24
    /**
25
     * @var string
26
     */
27
    private $lang;
28
    /**
29
     * @var null|string
30
     */
31
    private $website;
32
    /**
33
     * @var null|string[]
34
     */
35
    private $menu;
36
    /**
37
     * @var int|null
38
     */
39
    private $menuOrder;
40
    /**
41
     * @var null|string
42
     */
43
    private $metaTitle;
44
    /**
45
     * @var null|string
46
     */
47
    private $metaDescription;
48
    /**
49
     * @var null|string
50
     */
51
    private $theme;
52
    /**
53
     * @var null|string
54
     */
55
    private $menuCssClass;
56
    /**
57
     * @var null|string
58
     */
59
    private $template;
60
    /**
61
     * @var array
62
     */
63
    private $context;
64
65
    /**
66
     * @param string[]|null $menu
67
     */
68
    public function __construct(?string $id, string $title, string $content, string $url, string $lang, ?string $website, ?array $menu, ?int $menuOrder, ?string $menuCssClass, ?string $metaTitle, ?string $metaDescription, ?string $theme, ?string $template, array $context = [])
69
    {
70
        $this->id = $id;
71
        $this->title = $title;
72
        $this->content = $content;
73
        $this->url = $url;
74
        $this->lang = $lang;
75
        $this->website = $website;
76
        $this->menu = $menu;
77
        $this->menuOrder = $menuOrder;
78
        $this->menuCssClass = $menuCssClass;
79
        $this->metaTitle = $metaTitle;
80
        $this->metaDescription = $metaDescription;
81
        $this->theme = $theme;
82
        $this->template = $template;
83
        $this->context = $context;
84
    }
85
86
    public static function fromFile(string $file): self
87
    {
88
        if (!is_readable($file)) {
89
            throw new UnableToLoadFileException('Cannot read file '.$file);
90
        }
91
92
        $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
93
94
        switch ($extension) {
95
            case 'md':
96
                $parseMarkDown = true;
97
                break;
98
            case 'html':
99
                $parseMarkDown = false;
100
                break;
101
            default:
102
                throw new InvalidExtensionException(sprintf('Invalid extension for block %s. Valid extensions are .md and .html', $file));
103
        }
104
105
        $parser = new Parser();
106
107
        $document = $parser->parse(file_get_contents($file), $parseMarkDown);
108
109
        $yaml = $document->getYAML();
110
111
        $compulsoryFields = ['title', 'url', 'lang'];
112
113
        foreach ($compulsoryFields as $field) {
114
            if (!isset($yaml[$field])) {
115
                throw new UnableToLoadFileException('Missing field '.$field.' in YAML front matter of file '.$file);
116
            }
117
        }
118
119
        return new self(
120
            $yaml['id'] ?? null,
121
            $yaml['title'],
122
            $document->getContent(),
123
            '/'.ltrim($yaml['url'], '/'),
124
            $yaml['lang'],
125
            $yaml['website'] ?? null,
126
            isset($yaml['menu']) ? array_map('trim', explode('/', $yaml['menu'])) : null,
127
            $yaml['menu_order'] ?? null,
128
            $yaml['menu_css_class'] ?? null,
129
            $yaml['meta_title'] ?? null,
130
            $yaml['meta_description'] ?? null,
131
            $yaml['theme'] ?? null,
132
            $yaml['template'] ?? null,
133
            $yaml['context'] ?? []
134
        );
135
    }
136
137
    /**
138
     * @return null|string
139
     */
140
    public function getId(): ?string
141
    {
142
        return $this->id;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getTitle(): string
149
    {
150
        return $this->title;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getContent(): string
157
    {
158
        return $this->content;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getUrl(): string
165
    {
166
        return $this->url;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getLang(): string
173
    {
174
        return $this->lang;
175
    }
176
177
    /**
178
     * @return string|null
179
     */
180
    public function getWebsite(): ?string
181
    {
182
        return $this->website;
183
    }
184
185
    /**
186
     * @return null|string[]
187
     */
188
    public function getMenu(): ?array
189
    {
190
        return $this->menu;
191
    }
192
193
    /**
194
     * @return int
195
     */
196
    public function getMenuOrder(): int
197
    {
198
        return $this->menuOrder ?? 0;
199
    }
200
201
    /**
202
     * @return null|string
203
     */
204
    public function getMenuCssClass(): ?string
205
    {
206
        return $this->menuCssClass;
207
    }
208
209
    /**
210
     * @return null|string
211
     */
212
    public function getMetaTitle(): ?string
213
    {
214
        return $this->metaTitle;
215
    }
216
217
    /**
218
     * @return null|string
219
     */
220
    public function getMetaDescription(): ?string
221
    {
222
        return $this->metaDescription;
223
    }
224
225
    /**
226
     * @return null|string
227
     */
228
    public function getTheme(): ?string
229
    {
230
        return $this->theme;
231
    }
232
233
    /**
234
     * @return null|string
235
     */
236
    public function getTemplate(): ?string
237
    {
238
        return $this->template;
239
    }
240
241
    /**
242
     * @return array
243
     */
244
    public function getContext(): array
245
    {
246
        return $this->context;
247
    }
248
}
249