| Total Complexity | 5 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 13 | class MarkdownFileParser |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The extracted Front Matter. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | public array $matter = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The extracted Markdown body. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public string $body = ''; |
||
| 28 | |||
| 29 | public function __construct(string $filepath) |
||
| 30 | { |
||
| 31 | $stream = file_get_contents($filepath); |
||
| 32 | |||
| 33 | // Check if the file has Front Matter. |
||
| 34 | if (str_starts_with($stream, '---')) { |
||
| 35 | $object = YamlFrontMatter::markdownCompatibleParse($stream); |
||
| 36 | |||
| 37 | if ($object->matter()) { |
||
| 38 | $this->matter = $object->matter(); |
||
| 39 | } |
||
| 40 | |||
| 41 | if ($object->body()) { |
||
| 42 | $this->body = $object->body(); |
||
| 43 | } |
||
| 44 | } else { |
||
| 45 | $this->body = $stream; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the processed Markdown file as a MarkdownDocument. |
||
| 51 | */ |
||
| 52 | public function get(): MarkdownDocument |
||
| 55 | } |
||
| 56 | } |
||
| 57 |