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
|
|
|
/** |
54
|
|
|
* @param string[]|null $menu |
55
|
|
|
*/ |
56
|
|
|
public function __construct(?string $id, string $title, string $content, string $url, string $lang, ?string $website, ?array $menu, ?int $menuOrder, ?string $metaTitle, ?string $metaDescription, ?string $theme) |
57
|
|
|
{ |
58
|
|
|
$this->id = $id; |
59
|
|
|
$this->title = $title; |
60
|
|
|
$this->content = $content; |
61
|
|
|
$this->url = $url; |
62
|
|
|
$this->lang = $lang; |
63
|
|
|
$this->website = $website; |
64
|
|
|
$this->menu = $menu; |
65
|
|
|
$this->menuOrder = $menuOrder; |
66
|
|
|
$this->metaTitle = $metaTitle; |
67
|
|
|
$this->metaDescription = $metaDescription; |
68
|
|
|
$this->theme = $theme; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function fromFile(string $file): self |
72
|
|
|
{ |
73
|
|
|
if (!is_readable($file)) { |
74
|
|
|
throw new UnableToLoadFileException('Cannot read file '.$file); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
78
|
|
|
|
79
|
|
|
switch ($extension) { |
80
|
|
|
case 'md': |
81
|
|
|
$parseMarkDown = true; |
82
|
|
|
break; |
83
|
|
|
case 'html': |
84
|
|
|
$parseMarkDown = false; |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
throw new InvalidExtensionException(sprintf('Invalid extension for block %s. Valid extensions are .md and .html', $file)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$parser = new Parser(); |
91
|
|
|
|
92
|
|
|
$document = $parser->parse(file_get_contents($file), $parseMarkDown); |
93
|
|
|
|
94
|
|
|
$yaml = $document->getYAML(); |
95
|
|
|
|
96
|
|
|
$compulsoryFields = ['title', 'url', 'lang']; |
97
|
|
|
|
98
|
|
|
foreach ($compulsoryFields as $field) { |
99
|
|
|
if (!isset($yaml[$field])) { |
100
|
|
|
throw new UnableToLoadFileException('Missing field '.$field.' in YAML front matter of file '.$file); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return new self( |
105
|
|
|
$yaml['id'] ?? null, |
106
|
|
|
$yaml['title'], |
107
|
|
|
$document->getContent(), |
108
|
|
|
'/'.ltrim($yaml['url'], '/'), |
109
|
|
|
$yaml['lang'], |
110
|
|
|
$yaml['website'] ?? null, |
111
|
|
|
isset($yaml['menu']) ? array_map('trim', explode('/', $yaml['menu'])) : null, |
112
|
|
|
$yaml['menu_order'] ?? null, |
113
|
|
|
$yaml['meta_title'] ?? null, |
114
|
|
|
$yaml['meta_description'] ?? null, |
115
|
|
|
$yaml['theme'] ?? null |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return null|string |
121
|
|
|
*/ |
122
|
|
|
public function getId(): ?string |
123
|
|
|
{ |
124
|
|
|
return $this->id; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getTitle(): string |
131
|
|
|
{ |
132
|
|
|
return $this->title; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getContent(): string |
139
|
|
|
{ |
140
|
|
|
return $this->content; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getUrl(): string |
147
|
|
|
{ |
148
|
|
|
return $this->url; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getLang(): string |
155
|
|
|
{ |
156
|
|
|
return $this->lang; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string|null |
161
|
|
|
*/ |
162
|
|
|
public function getWebsite(): ?string |
163
|
|
|
{ |
164
|
|
|
return $this->website; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return null|string[] |
169
|
|
|
*/ |
170
|
|
|
public function getMenu(): ?array |
171
|
|
|
{ |
172
|
|
|
return $this->menu; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return int|null |
177
|
|
|
*/ |
178
|
|
|
public function getMenuOrder(): ?int |
179
|
|
|
{ |
180
|
|
|
return $this->menuOrder; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return null|string |
185
|
|
|
*/ |
186
|
|
|
public function getMetaTitle(): ?string |
187
|
|
|
{ |
188
|
|
|
return $this->metaTitle; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return null|string |
193
|
|
|
*/ |
194
|
|
|
public function getMetaDescription(): ?string |
195
|
|
|
{ |
196
|
|
|
return $this->metaDescription; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return null|string |
201
|
|
|
*/ |
202
|
|
|
public function getTheme(): ?string |
203
|
|
|
{ |
204
|
|
|
return $this->theme; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|