1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cecil\Collection\Page; |
15
|
|
|
|
16
|
|
|
use Cecil\Collection\Item; |
17
|
|
|
use Cecil\Exception\RuntimeException; |
18
|
|
|
use Cecil\Util; |
19
|
|
|
use Cocur\Slugify\Slugify; |
20
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Page class. |
24
|
|
|
* |
25
|
|
|
* Represents a page in the collection, which can be created from a file or be virtual. |
26
|
|
|
* Provides methods to manage page properties, variables, and rendering. |
27
|
|
|
*/ |
28
|
|
|
class Page extends Item |
29
|
|
|
{ |
30
|
|
|
public const SLUGIFY_PATTERN = '/(^\/|[^._a-z0-9\/]|-)+/'; // should be '/^\/|[^_a-z0-9\/]+/' |
31
|
|
|
|
32
|
|
|
/** @var bool True if page is not created from a file. */ |
33
|
|
|
protected $virtual; |
34
|
|
|
|
35
|
|
|
/** @var SplFileInfo */ |
36
|
|
|
protected $file; |
37
|
|
|
|
38
|
|
|
/** @var Type Type */ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
protected $folder; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
protected $slug; |
46
|
|
|
|
47
|
|
|
/** @var string path = folder + slug. */ |
48
|
|
|
protected $path; |
49
|
|
|
|
50
|
|
|
/** @var string */ |
51
|
|
|
protected $section; |
52
|
|
|
|
53
|
|
|
/** @var string */ |
54
|
|
|
protected $frontmatter; |
55
|
|
|
|
56
|
|
|
/** @var array Front matter before conversion. */ |
57
|
|
|
protected $fmVariables = []; |
58
|
|
|
|
59
|
|
|
/** @var string Body before conversion. */ |
60
|
|
|
protected $body; |
61
|
|
|
|
62
|
|
|
/** @var string Body after conversion. */ |
63
|
|
|
protected $html; |
64
|
|
|
|
65
|
|
|
/** @var array Output, by format */ |
66
|
|
|
protected $rendered = []; |
67
|
|
|
|
68
|
|
|
/** @var Collection Subpages of a list page. */ |
69
|
|
|
protected $subPages; |
70
|
|
|
|
71
|
|
|
/** @var array */ |
72
|
|
|
protected $paginator = []; |
73
|
|
|
|
74
|
|
|
/** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary. */ |
75
|
|
|
protected $terms; |
76
|
|
|
|
77
|
|
|
/** @var Slugify */ |
78
|
|
|
private static $slugifier; |
79
|
|
|
|
80
|
1 |
|
public function __construct(string $id) |
81
|
|
|
{ |
82
|
1 |
|
parent::__construct($id); |
83
|
1 |
|
$this->setVirtual(true); |
84
|
1 |
|
$this->setType(Type::PAGE->value); |
85
|
|
|
// default variables |
86
|
1 |
|
$this->setVariables([ |
87
|
1 |
|
'title' => 'Page Title', |
88
|
1 |
|
'date' => new \DateTime(), |
89
|
1 |
|
'updated' => new \DateTime(), |
90
|
1 |
|
'weight' => null, |
91
|
1 |
|
'filepath' => null, |
92
|
1 |
|
'published' => true, |
93
|
1 |
|
'content_template' => 'page.content.twig', |
94
|
1 |
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* toString magic method to prevent Twig get_attribute fatal error. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
public function __toString() |
103
|
|
|
{ |
104
|
1 |
|
return $this->getId(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Turns a path (string) into a slug (URI). |
109
|
|
|
*/ |
110
|
1 |
|
public static function slugify(string $path): string |
111
|
|
|
{ |
112
|
1 |
|
if (!self::$slugifier instanceof Slugify) { |
113
|
1 |
|
self::$slugifier = Slugify::create(['regexp' => self::SLUGIFY_PATTERN]); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return self::$slugifier->slugify($path); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates the ID from the file (path). |
121
|
|
|
*/ |
122
|
1 |
|
public static function createIdFromFile(SplFileInfo $file): string |
123
|
|
|
{ |
124
|
1 |
|
$relativePath = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath())); |
125
|
1 |
|
$basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension()))); |
126
|
|
|
// if file is "README.md", ID is "index" |
127
|
1 |
|
$basename = (string) str_ireplace('readme', 'index', $basename); |
128
|
|
|
// if file is section's index: "section/index.md", ID is "section" |
129
|
1 |
|
if (!empty($relativePath) && PrefixSuffix::sub($basename) == 'index') { |
130
|
|
|
// case of a localized section's index: "section/index.fr.md", ID is "fr/section" |
131
|
1 |
|
if (PrefixSuffix::hasSuffix($basename)) { |
132
|
1 |
|
return PrefixSuffix::getSuffix($basename) . '/' . $relativePath; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return $relativePath; |
136
|
|
|
} |
137
|
|
|
// localized page |
138
|
1 |
|
if (PrefixSuffix::hasSuffix($basename)) { |
139
|
1 |
|
return trim(Util::joinPath(PrefixSuffix::getSuffix($basename), $relativePath, PrefixSuffix::sub($basename)), '/'); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
return trim(Util::joinPath($relativePath, $basename), '/'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the ID of a page without language. |
147
|
|
|
*/ |
148
|
1 |
|
public function getIdWithoutLang(): string |
149
|
|
|
{ |
150
|
1 |
|
$langPrefix = $this->getVariable('language') . '/'; |
151
|
1 |
|
if ($this->hasVariable('language') && Util\Str::startsWith($this->getId(), $langPrefix)) { |
152
|
1 |
|
return substr($this->getId(), \strlen($langPrefix)); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return $this->getId(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set file. |
160
|
|
|
*/ |
161
|
1 |
|
public function setFile(SplFileInfo $file): self |
162
|
|
|
{ |
163
|
1 |
|
$this->file = $file; |
164
|
1 |
|
$this->setVirtual(false); |
165
|
|
|
|
166
|
|
|
/* |
167
|
|
|
* File path components |
168
|
|
|
*/ |
169
|
1 |
|
$fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); |
170
|
1 |
|
$fileExtension = $this->file->getExtension(); |
171
|
1 |
|
$fileName = $this->file->getBasename('.' . $fileExtension); |
172
|
|
|
// renames "README" to "index" |
173
|
1 |
|
$fileName = (string) str_ireplace('readme', 'index', $fileName); |
174
|
|
|
// case of "index" = home page |
175
|
1 |
|
if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') { |
176
|
1 |
|
$this->setType(Type::HOMEPAGE->value); |
177
|
|
|
} |
178
|
|
|
/* |
179
|
|
|
* Set page properties and variables |
180
|
|
|
*/ |
181
|
1 |
|
$this->setFolder($fileRelativePath); |
182
|
1 |
|
$this->setSlug($fileName); |
183
|
1 |
|
$this->setPath($this->getFolder() . '/' . $this->getSlug()); |
184
|
1 |
|
$this->setVariables([ |
185
|
1 |
|
'title' => PrefixSuffix::sub($fileName), |
186
|
1 |
|
'date' => (new \DateTime())->setTimestamp($this->file->getMTime()), |
187
|
1 |
|
'updated' => (new \DateTime())->setTimestamp($this->file->getMTime()), |
188
|
1 |
|
'filepath' => $this->file->getRelativePathname(), |
189
|
1 |
|
]); |
190
|
|
|
/* |
191
|
|
|
* Set specific variables |
192
|
|
|
*/ |
193
|
|
|
// is file has a prefix? |
194
|
1 |
|
if (PrefixSuffix::hasPrefix($fileName)) { |
195
|
1 |
|
$prefix = PrefixSuffix::getPrefix($fileName); |
196
|
1 |
|
if ($prefix !== null) { |
197
|
|
|
// prefix is an integer: used for sorting |
198
|
1 |
|
if (is_numeric($prefix)) { |
199
|
1 |
|
$this->setVariable('weight', (int) $prefix); |
200
|
|
|
} |
201
|
|
|
// prefix is a valid date? |
202
|
1 |
|
if (Util\Date::isValid($prefix)) { |
203
|
1 |
|
$this->setVariable('date', (string) $prefix); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
// is file has a language suffix? |
208
|
1 |
|
if (PrefixSuffix::hasSuffix($fileName)) { |
209
|
1 |
|
$this->setVariable('language', PrefixSuffix::getSuffix($fileName)); |
210
|
|
|
} |
211
|
|
|
// set reference between page's translations, even if it exist in only one language |
212
|
1 |
|
$this->setVariable('langref', $this->getPath()); |
213
|
|
|
|
214
|
1 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns file name, with extension. |
219
|
|
|
*/ |
220
|
|
|
public function getFileName(): ?string |
221
|
|
|
{ |
222
|
|
|
if ($this->file === null) { |
223
|
|
|
return null; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $this->file->getBasename(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns file real path. |
231
|
|
|
*/ |
232
|
1 |
|
public function getFilePath(): ?string |
233
|
|
|
{ |
234
|
1 |
|
if ($this->file === null) { |
235
|
|
|
return null; |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
return $this->file->getRealPath() === false ? null : $this->file->getRealPath(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Parse file content. |
243
|
|
|
*/ |
244
|
1 |
|
public function parse(): self |
245
|
|
|
{ |
246
|
1 |
|
$parser = new Parser($this->file); |
247
|
1 |
|
$parsed = $parser->parse(); |
248
|
1 |
|
$this->frontmatter = $parsed->getFrontmatter(); |
249
|
1 |
|
$this->body = $parsed->getBody(); |
250
|
|
|
|
251
|
1 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get front matter. |
256
|
|
|
*/ |
257
|
1 |
|
public function getFrontmatter(): ?string |
258
|
|
|
{ |
259
|
1 |
|
return $this->frontmatter; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get body as raw. |
264
|
|
|
*/ |
265
|
1 |
|
public function getBody(): ?string |
266
|
|
|
{ |
267
|
1 |
|
return $this->body; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set virtual status. |
272
|
|
|
*/ |
273
|
1 |
|
public function setVirtual(bool $virtual): self |
274
|
|
|
{ |
275
|
1 |
|
$this->virtual = $virtual; |
276
|
|
|
|
277
|
1 |
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Is current page is virtual? |
282
|
|
|
*/ |
283
|
1 |
|
public function isVirtual(): bool |
284
|
|
|
{ |
285
|
1 |
|
return $this->virtual; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Set page type. |
290
|
|
|
*/ |
291
|
1 |
|
public function setType(string $type): self |
292
|
|
|
{ |
293
|
1 |
|
$this->type = Type::from($type); |
294
|
|
|
|
295
|
1 |
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Get page type. |
300
|
|
|
*/ |
301
|
1 |
|
public function getType(): string |
302
|
|
|
{ |
303
|
1 |
|
return $this->type->value; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Set path without slug. |
308
|
|
|
*/ |
309
|
1 |
|
public function setFolder(string $folder): self |
310
|
|
|
{ |
311
|
1 |
|
$this->folder = self::slugify($folder); |
312
|
|
|
|
313
|
1 |
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Get path without slug. |
318
|
|
|
*/ |
319
|
1 |
|
public function getFolder(): ?string |
320
|
|
|
{ |
321
|
1 |
|
return $this->folder; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Set slug. |
326
|
|
|
*/ |
327
|
1 |
|
public function setSlug(string $slug): self |
328
|
|
|
{ |
329
|
1 |
|
if (!$this->slug) { |
330
|
1 |
|
$slug = self::slugify(PrefixSuffix::sub($slug)); |
331
|
|
|
} |
332
|
|
|
// force slug and update path |
333
|
1 |
|
if ($this->slug && $this->slug != $slug) { |
334
|
1 |
|
$this->setPath($this->getFolder() . '/' . $slug); |
335
|
|
|
} |
336
|
1 |
|
$this->slug = $slug; |
337
|
|
|
|
338
|
1 |
|
return $this; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Get slug. |
343
|
|
|
*/ |
344
|
1 |
|
public function getSlug(): string |
345
|
|
|
{ |
346
|
1 |
|
return $this->slug; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Set path. |
351
|
|
|
*/ |
352
|
1 |
|
public function setPath(string $path): self |
353
|
|
|
{ |
354
|
1 |
|
$path = trim($path, '/'); |
355
|
|
|
|
356
|
|
|
// case of homepage |
357
|
1 |
|
if ($path == 'index') { |
358
|
1 |
|
$this->path = ''; |
359
|
|
|
|
360
|
1 |
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
// case of custom sections' index (ie: section/index.md -> section) |
364
|
1 |
|
if (substr($path, -6) == '/index') { |
365
|
1 |
|
$path = substr($path, 0, \strlen($path) - 6); |
366
|
|
|
} |
367
|
1 |
|
$this->path = $path; |
368
|
|
|
|
369
|
1 |
|
$lastslash = strrpos($this->path, '/'); |
370
|
|
|
|
371
|
|
|
// case of root/top-level pages |
372
|
1 |
|
if ($lastslash === false) { |
373
|
1 |
|
$this->slug = $this->path; |
374
|
|
|
|
375
|
1 |
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
// case of sections' pages: set section |
379
|
1 |
|
if (!$this->virtual && $this->getSection() === null) { |
380
|
1 |
|
$this->section = explode('/', $this->path)[0]; |
381
|
|
|
} |
382
|
|
|
// set/update folder and slug |
383
|
1 |
|
$this->folder = substr($this->path, 0, $lastslash); |
384
|
1 |
|
$this->slug = substr($this->path, -(\strlen($this->path) - $lastslash - 1)); |
385
|
|
|
|
386
|
1 |
|
return $this; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Get path. |
391
|
|
|
*/ |
392
|
1 |
|
public function getPath(): ?string |
393
|
|
|
{ |
394
|
1 |
|
return $this->path; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @see getPath() |
399
|
|
|
*/ |
400
|
|
|
public function getPathname(): ?string |
401
|
|
|
{ |
402
|
|
|
return $this->getPath(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Set section. |
407
|
|
|
*/ |
408
|
1 |
|
public function setSection(string $section): self |
409
|
|
|
{ |
410
|
1 |
|
$this->section = $section; |
411
|
|
|
|
412
|
1 |
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Get section. |
417
|
|
|
*/ |
418
|
1 |
|
public function getSection(): ?string |
419
|
|
|
{ |
420
|
1 |
|
return !empty($this->section) ? $this->section : null; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Unset section. |
425
|
|
|
*/ |
426
|
|
|
public function unSection(): self |
427
|
|
|
{ |
428
|
|
|
$this->section = null; |
429
|
|
|
|
430
|
|
|
return $this; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Set body as HTML. |
435
|
|
|
*/ |
436
|
1 |
|
public function setBodyHtml(string $html): self |
437
|
|
|
{ |
438
|
1 |
|
$this->html = $html; |
439
|
|
|
|
440
|
1 |
|
return $this; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Get body as HTML. |
445
|
|
|
*/ |
446
|
1 |
|
public function getBodyHtml(): ?string |
447
|
|
|
{ |
448
|
1 |
|
return $this->html; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @see getBodyHtml() |
453
|
|
|
*/ |
454
|
1 |
|
public function getContent(): ?string |
455
|
|
|
{ |
456
|
1 |
|
return $this->getBodyHtml(); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Add rendered. |
461
|
|
|
*/ |
462
|
1 |
|
public function addRendered(array $rendered): self |
463
|
|
|
{ |
464
|
1 |
|
$this->rendered += $rendered; |
465
|
|
|
|
466
|
1 |
|
return $this; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Get rendered. |
471
|
|
|
*/ |
472
|
1 |
|
public function getRendered(): array |
473
|
|
|
{ |
474
|
1 |
|
return $this->rendered; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Set Subpages. |
479
|
|
|
*/ |
480
|
1 |
|
public function setPages(\Cecil\Collection\Page\Collection $subPages): self |
481
|
|
|
{ |
482
|
1 |
|
$this->subPages = $subPages; |
483
|
|
|
|
484
|
1 |
|
return $this; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Get Subpages. |
489
|
|
|
*/ |
490
|
1 |
|
public function getPages(): ?\Cecil\Collection\Page\Collection |
491
|
|
|
{ |
492
|
1 |
|
return $this->subPages; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Set paginator. |
497
|
|
|
*/ |
498
|
1 |
|
public function setPaginator(array $paginator): self |
499
|
|
|
{ |
500
|
1 |
|
$this->paginator = $paginator; |
501
|
|
|
|
502
|
1 |
|
return $this; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Get paginator. |
507
|
|
|
*/ |
508
|
1 |
|
public function getPaginator(): array |
509
|
|
|
{ |
510
|
1 |
|
return $this->paginator; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Paginator backward compatibility. |
515
|
|
|
*/ |
516
|
|
|
public function getPagination(): array |
517
|
|
|
{ |
518
|
|
|
return $this->getPaginator(); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Set vocabulary terms. |
523
|
|
|
*/ |
524
|
1 |
|
public function setTerms(\Cecil\Collection\Taxonomy\Vocabulary $terms): self |
525
|
|
|
{ |
526
|
1 |
|
$this->terms = $terms; |
527
|
|
|
|
528
|
1 |
|
return $this; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Get vocabulary terms. |
533
|
|
|
*/ |
534
|
1 |
|
public function getTerms(): \Cecil\Collection\Taxonomy\Vocabulary |
535
|
|
|
{ |
536
|
1 |
|
return $this->terms; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/* |
540
|
|
|
* Helpers to set and get variables. |
541
|
|
|
*/ |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Set an array as variables. |
545
|
|
|
* |
546
|
|
|
* @throws RuntimeException |
547
|
|
|
*/ |
548
|
1 |
|
public function setVariables(array $variables): self |
549
|
|
|
{ |
550
|
1 |
|
foreach ($variables as $key => $value) { |
551
|
1 |
|
$this->setVariable($key, $value); |
552
|
|
|
} |
553
|
|
|
|
554
|
1 |
|
return $this; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Get all variables. |
559
|
|
|
*/ |
560
|
1 |
|
public function getVariables(): array |
561
|
|
|
{ |
562
|
1 |
|
return $this->properties; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* Set a variable. |
567
|
|
|
* |
568
|
|
|
* @param string $name Name of the variable |
569
|
|
|
* @param mixed $value Value of the variable |
570
|
|
|
* |
571
|
|
|
* @throws RuntimeException |
572
|
|
|
*/ |
573
|
1 |
|
public function setVariable(string $name, $value): self |
574
|
|
|
{ |
575
|
1 |
|
$this->filterBool($value); |
576
|
|
|
switch ($name) { |
577
|
1 |
|
case 'date': |
578
|
1 |
|
case 'updated': |
579
|
1 |
|
case 'lastmod': |
580
|
|
|
try { |
581
|
1 |
|
$date = Util\Date::toDatetime($value); |
582
|
|
|
} catch (\Exception) { |
583
|
|
|
throw new \Exception(\sprintf('The value of "%s" is not a valid date: "%s".', $name, var_export($value, true))); |
584
|
|
|
} |
585
|
1 |
|
$this->offsetSet($name == 'lastmod' ? 'updated' : $name, $date); |
586
|
1 |
|
break; |
587
|
|
|
|
588
|
1 |
|
case 'schedule': |
589
|
|
|
/* |
590
|
|
|
* publish: 2012-10-08 |
591
|
|
|
* expiry: 2012-10-09 |
592
|
|
|
*/ |
593
|
1 |
|
$this->offsetSet('published', false); |
594
|
1 |
|
if (\is_array($value)) { |
595
|
1 |
|
if (\array_key_exists('publish', $value) && Util\Date::toDatetime($value['publish']) <= Util\Date::toDatetime('now')) { |
596
|
1 |
|
$this->offsetSet('published', true); |
597
|
|
|
} |
598
|
1 |
|
if (\array_key_exists('expiry', $value) && Util\Date::toDatetime($value['expiry']) >= Util\Date::toDatetime('now')) { |
599
|
|
|
$this->offsetSet('published', true); |
600
|
|
|
} |
601
|
|
|
} |
602
|
1 |
|
break; |
603
|
1 |
|
case 'draft': |
604
|
|
|
// draft: true = published: false |
605
|
1 |
|
if ($value === true) { |
606
|
1 |
|
$this->offsetSet('published', false); |
607
|
|
|
} |
608
|
1 |
|
break; |
609
|
1 |
|
case 'path': |
610
|
1 |
|
case 'slug': |
611
|
1 |
|
$slugify = self::slugify((string) $value); |
612
|
1 |
|
if ($value != $slugify) { |
613
|
|
|
throw new RuntimeException(\sprintf('"%s" variable should be "%s" (not "%s") in "%s".', $name, $slugify, (string) $value, $this->getId())); |
614
|
|
|
} |
615
|
1 |
|
$method = 'set' . ucfirst($name); |
616
|
1 |
|
$this->$method($value); |
617
|
1 |
|
break; |
618
|
|
|
default: |
619
|
1 |
|
$this->offsetSet($name, $value); |
620
|
|
|
} |
621
|
|
|
|
622
|
1 |
|
return $this; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Is variable exists? |
627
|
|
|
* |
628
|
|
|
* @param string $name Name of the variable |
629
|
|
|
*/ |
630
|
1 |
|
public function hasVariable(string $name): bool |
631
|
|
|
{ |
632
|
1 |
|
return $this->offsetExists($name); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* Get a variable. |
637
|
|
|
* |
638
|
|
|
* @param string $name Name of the variable |
639
|
|
|
* @param mixed|null $default Default value |
640
|
|
|
* |
641
|
|
|
* @return mixed|null |
642
|
|
|
*/ |
643
|
1 |
|
public function getVariable(string $name, $default = null) |
644
|
|
|
{ |
645
|
1 |
|
if ($this->offsetExists($name)) { |
646
|
1 |
|
return $this->offsetGet($name); |
647
|
|
|
} |
648
|
|
|
|
649
|
1 |
|
return $default; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Unset a variable. |
654
|
|
|
* |
655
|
|
|
* @param string $name Name of the variable |
656
|
|
|
*/ |
657
|
1 |
|
public function unVariable(string $name): self |
658
|
|
|
{ |
659
|
1 |
|
if ($this->offsetExists($name)) { |
660
|
1 |
|
$this->offsetUnset($name); |
661
|
|
|
} |
662
|
|
|
|
663
|
1 |
|
return $this; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* Set front matter (only) variables. |
668
|
|
|
*/ |
669
|
1 |
|
public function setFmVariables(array $variables): self |
670
|
|
|
{ |
671
|
1 |
|
$this->fmVariables = $variables; |
672
|
|
|
|
673
|
1 |
|
return $this; |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* Get front matter variables. |
678
|
|
|
*/ |
679
|
1 |
|
public function getFmVariables(): array |
680
|
|
|
{ |
681
|
1 |
|
return $this->fmVariables; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Cast "boolean" string (or array of strings) to boolean. |
686
|
|
|
* |
687
|
|
|
* @param mixed $value Value to filter |
688
|
|
|
* |
689
|
|
|
* @return bool|mixed |
690
|
|
|
* |
691
|
|
|
* @see strToBool() |
692
|
|
|
*/ |
693
|
1 |
|
private function filterBool(&$value) |
694
|
|
|
{ |
695
|
1 |
|
\Cecil\Util\Str::strToBool($value); |
696
|
1 |
|
if (\is_array($value)) { |
697
|
1 |
|
array_walk_recursive($value, '\Cecil\Util\Str::strToBool'); |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
/** |
702
|
|
|
* {@inheritdoc} |
703
|
|
|
*/ |
704
|
1 |
|
public function setId(string $id): self |
705
|
|
|
{ |
706
|
1 |
|
return parent::setId($id); |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|