1 | <?php |
||
12 | class FrontMatterParser |
||
13 | { |
||
14 | /** |
||
15 | * The RegEx used to identify Front Matter variables |
||
16 | */ |
||
17 | const VARIABLE_DEF = '/(?<!\\\\)%([a-zA-Z]+)/'; |
||
18 | |||
19 | /** |
||
20 | * A list of special fields in the Front Matter that will support expansion |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | private static $expandableFields = array('permalink'); |
||
25 | |||
26 | /** |
||
27 | * Whether or not an field was expanded into several values |
||
28 | * |
||
29 | * Only fields specified in $expandableFields will cause this value to be set to true |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $expansionUsed; |
||
34 | |||
35 | /** |
||
36 | * The current depth of the recursion for evaluating nested arrays in the Front Matter |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | private $nestingLevel; |
||
41 | |||
42 | /** |
||
43 | * The current hierarchy of the keys that are being evaluated |
||
44 | * |
||
45 | * Since arrays can be nested, we'll keep track of the keys up until the current depth. This information is used for |
||
46 | * error reporting |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $yamlKeys; |
||
51 | |||
52 | /** |
||
53 | * The entire Front Matter block; evaluation will happen in place |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $frontMatter; |
||
58 | |||
59 | /** |
||
60 | * FrontMatterParser constructor |
||
61 | * |
||
62 | * @param array $rawFrontMatter |
||
63 | */ |
||
64 | 22 | public function __construct(&$rawFrontMatter) |
|
75 | |||
76 | /** |
||
77 | * True if any fields were expanded in the Front Matter block |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | 6 | public function hasExpansion () |
|
85 | |||
86 | // |
||
87 | // Special FrontMatter fields |
||
88 | // |
||
89 | |||
90 | /** |
||
91 | * Special treatment for some FrontMatter variables |
||
92 | */ |
||
93 | 22 | private function handleSpecialFrontMatter () |
|
97 | |||
98 | /** |
||
99 | * Special treatment for the `date` field in FrontMatter that creates three new variables: year, month, day |
||
100 | */ |
||
101 | 22 | private function handleDateField () |
|
133 | |||
134 | // |
||
135 | // Evaluation |
||
136 | // |
||
137 | |||
138 | /** |
||
139 | * Evaluate an array as Front Matter |
||
140 | * |
||
141 | * @param array $yaml |
||
142 | */ |
||
143 | 22 | private function evaluateBlock (&$yaml) |
|
169 | |||
170 | /** |
||
171 | * Evaluate an expandable field |
||
172 | * |
||
173 | * @param string $key |
||
174 | * @param string $fmStatement |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | 6 | private function evaluateExpandableField ($key, $fmStatement) |
|
204 | |||
205 | /** |
||
206 | * Convert a string or an array into an array of ExpandedValue objects created through "value expansion" |
||
207 | * |
||
208 | * @param string $frontMatterKey The current hierarchy of the Front Matter keys being used |
||
209 | * @param string $expandableValue The Front Matter value that will be expanded |
||
210 | * @param array $arrayVariableNames The Front Matter variable names that reference arrays |
||
211 | * |
||
212 | * @return array |
||
213 | * |
||
214 | * @throws YamlUnsupportedVariableException If a multidimensional array is given for value expansion |
||
215 | */ |
||
216 | 4 | private function evaluateArrayType ($frontMatterKey, $expandableValue, $arrayVariableNames) |
|
251 | |||
252 | /** |
||
253 | * Evaluate an string for FrontMatter variables and replace them with the corresponding values |
||
254 | * |
||
255 | * @param string $key The key of the Front Matter value |
||
256 | * @param string $string The string that will be evaluated |
||
257 | * @param bool $ignoreArrays When set to true, an exception won't be thrown when an array is found with the |
||
258 | * interpolation |
||
259 | * |
||
260 | * @return string The final string with variables evaluated |
||
261 | * |
||
262 | * @throws YamlUnsupportedVariableException A FrontMatter variable is not an int, float, or string |
||
263 | */ |
||
264 | 21 | private function evaluateBasicType ($key, $string, $ignoreArrays = false) |
|
284 | |||
285 | // |
||
286 | // Variable management |
||
287 | // |
||
288 | |||
289 | /** |
||
290 | * Get an array of FrontMatter variables in the specified string that need to be interpolated |
||
291 | * |
||
292 | * @param string $string |
||
293 | * |
||
294 | * @return string[] |
||
295 | */ |
||
296 | 21 | private function getFrontMatterVariables ($string) |
|
306 | |||
307 | /** |
||
308 | * Get the value of a FM variable or throw an exception |
||
309 | * |
||
310 | * @param string $key |
||
311 | * @param string $varName |
||
312 | * |
||
313 | * @return mixed |
||
314 | * @throws YamlVariableUndefinedException |
||
315 | */ |
||
316 | 15 | private function getVariableValue ($key, $varName) |
|
325 | } |