Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ConvertsArrayToFrontMatter | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| execute | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Actions; |
| 4 | |
| 5 | /** |
| 6 | * Convert an array into YAML Front Matter. |
| 7 | */ |
| 8 | class ConvertsArrayToFrontMatter |
| 9 | { |
| 10 | /** |
| 11 | * Execute the action. |
| 12 | * |
| 13 | * @param array $array |
| 14 | * @return string $yaml front matter |
| 15 | */ |
| 16 | public function execute(array $array): string |
| 17 | { |
| 18 | // Initialize the array |
| 19 | $yaml = []; |
| 20 | |
| 21 | // Set the first line to the opening starting block |
| 22 | $yaml[] = '---'; |
| 23 | |
| 24 | // For each line, add the key-value pair as YAML |
| 25 | foreach ($array as $key => $value) { |
| 26 | $yaml[] = "$key: $value"; |
| 27 | } |
| 28 | |
| 29 | // Set the closing block |
| 30 | $yaml[] = '---'; |
| 31 | |
| 32 | // Add an extra line |
| 33 | $yaml[] = ''; |
| 34 | |
| 35 | // Return the array imploded into a string with newline characters |
| 36 | return implode("\n", $yaml); |
| 37 | } |
| 38 | } |