1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models\Markdown; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\ConvertsArrayToFrontMatter; |
6
|
|
|
use Hyde\Framework\Concerns\JsonSerializesArrayable; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Object representing the YAML front matter of a Markdown file. |
12
|
|
|
* |
13
|
|
|
* The data here is equal to the YAML. Unless you are using the data to construct dynamic data, |
14
|
|
|
* you probably want to call the `get()` method on the Page object, as that will let you |
15
|
|
|
* access dynamic computed data if it exists, or it will fall back to this class's data. |
16
|
|
|
* |
17
|
|
|
* For package developers: |
18
|
|
|
* Use $page->get('foo') to access computed data, |
19
|
|
|
* Use $page->matter('foo') to access raw data. |
20
|
|
|
* |
21
|
|
|
* @see \Hyde\Framework\Testing\Unit\FrontMatterModelTest |
22
|
|
|
* @phpstan-consistent-constructor |
23
|
|
|
*/ |
24
|
|
|
class FrontMatter implements Arrayable, \Stringable, \JsonSerializable |
25
|
|
|
{ |
26
|
|
|
use JsonSerializesArrayable; |
27
|
|
|
|
28
|
|
|
public array $data; |
29
|
|
|
|
30
|
|
|
public function __construct(array $matter = []) |
31
|
|
|
{ |
32
|
|
|
$this->data = $matter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __toString(): string |
36
|
|
|
{ |
37
|
|
|
return (new ConvertsArrayToFrontMatter())->execute($this->data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __get(string $key): mixed |
41
|
|
|
{ |
42
|
|
|
return $this->get($key); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function get(string $key = null, mixed $default = null): mixed |
46
|
|
|
{ |
47
|
|
|
if ($key) { |
48
|
|
|
return Arr::get($this->data, $key, $default); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function set(string $key, mixed $value): static |
55
|
|
|
{ |
56
|
|
|
$this->data[$key] = $value; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function has(string $key): bool |
62
|
|
|
{ |
63
|
|
|
return Arr::has($this->data, $key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function toArray(): array |
67
|
|
|
{ |
68
|
|
|
return $this->data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function fromArray(array $matter): static |
72
|
|
|
{ |
73
|
|
|
return new static($matter); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|