1 | <?php |
||
2 | |||
3 | |||
4 | namespace AloiaCms\Writer; |
||
5 | |||
6 | use Illuminate\Support\Collection; |
||
7 | use Symfony\Component\Yaml\Yaml; |
||
8 | |||
9 | class FrontMatterCreator |
||
10 | { |
||
11 | private $matter = []; |
||
12 | |||
13 | private $content = ''; |
||
14 | |||
15 | /** |
||
16 | * FrontMatterCreator constructor. |
||
17 | * @param array $matter |
||
18 | * @param string $content |
||
19 | */ |
||
20 | 52 | private function __construct(array $matter, string $content) |
|
21 | { |
||
22 | 52 | $this->matter = $matter; |
|
23 | 52 | $this->content = $content; |
|
24 | 52 | } |
|
25 | |||
26 | /** |
||
27 | * Provide the front matter and file content |
||
28 | * |
||
29 | * @param array $matter |
||
30 | * @param string $content |
||
31 | * @return static |
||
32 | */ |
||
33 | 52 | public static function seed(array $matter, string $content) |
|
34 | { |
||
35 | 52 | return new static($matter, $content); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Create a data file including the front matter |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | 52 | public function create(): string |
|
44 | { |
||
45 | 52 | $matter = Collection::make($this->matter) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
46 | 52 | ->keysToSnakeCase() |
|
47 | 43 | ->toArray(); |
|
48 | 52 | ||
49 | 52 | $front_matter_string = ""; |
|
50 | |||
51 | 52 | if (count($matter) > 0) { |
|
52 | $front_matter_string = "---\n"; |
||
53 | 52 | $front_matter_string .= Yaml::dump($matter); |
|
54 | 43 | $front_matter_string .= "---\n"; |
|
55 | 43 | } |
|
56 | 43 | ||
57 | $content = ltrim($this->content, "\r\n"); |
||
58 | |||
59 | 52 | if (strlen($content) > 0) { |
|
60 | $front_matter_string .= "\n" . $content; |
||
61 | 52 | } |
|
62 | |||
63 | return $front_matter_string; |
||
64 | } |
||
65 | } |
||
66 |