Conditions | 3 |
Paths | 3 |
Total Lines | 23 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
1 | <?php |
||
17 | public function parse(string $content) : Document |
||
18 | { |
||
19 | // Parser regex borrowed from the `devster/frontmatter` package |
||
20 | // https://github.com/devster/frontmatter/blob/bb5d2c7/src/Parser.php#L123 |
||
21 | $pattern = "/^\s*(?:---)[\n\r\s]*(.*?)[\n\r\s]*(?:---)[\s\n\r]*(.*)$/s"; |
||
22 | |||
23 | $parts = []; |
||
24 | |||
25 | $match = preg_match($pattern, $content, $parts); |
||
26 | |||
27 | if ($match === false) { |
||
28 | throw new Exception('An error occurred while extracting the front matter from the contents'); |
||
29 | } |
||
30 | |||
31 | if ($match === 0) { |
||
32 | return new Document([], $content); |
||
33 | } |
||
34 | |||
35 | $matter = $this->yamlParser->parse($parts[1]); |
||
36 | $body = $parts[2]; |
||
37 | |||
38 | return new Document($matter, $body); |
||
39 | } |
||
40 | } |
||
41 |