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 | 30 | public function __construct(&$rawFrontMatter) |
|
75 | |||
76 | /** |
||
77 | * True if any fields were expanded in the Front Matter block |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | 11 | public function hasExpansion () |
|
85 | |||
86 | // |
||
87 | // Special FrontMatter fields |
||
88 | // |
||
89 | |||
90 | /** |
||
91 | * Special treatment for some FrontMatter variables |
||
92 | */ |
||
93 | 30 | private function handleSpecialFrontMatter () |
|
97 | |||
98 | /** |
||
99 | * Special treatment for the `date` field in FrontMatter that creates three new variables: year, month, day |
||
100 | */ |
||
101 | 30 | private function handleDateField () |
|
116 | |||
117 | // |
||
118 | // Evaluation |
||
119 | // |
||
120 | |||
121 | /** |
||
122 | * Evaluate an array as Front Matter |
||
123 | * |
||
124 | * @param array $yaml |
||
125 | */ |
||
126 | 30 | private function evaluateBlock (&$yaml) |
|
156 | |||
157 | /** |
||
158 | * Evaluate an expandable field |
||
159 | * |
||
160 | * @param string $key |
||
161 | * @param string $fmStatement |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | 13 | private function evaluateExpandableField ($key, $fmStatement) |
|
191 | |||
192 | /** |
||
193 | * Convert a string or an array into an array of ExpandedValue objects created through "value expansion" |
||
194 | * |
||
195 | * @param string $frontMatterKey The current hierarchy of the Front Matter keys being used |
||
196 | * @param string $expandableValue The Front Matter value that will be expanded |
||
197 | * @param array $arrayVariableNames The Front Matter variable names that reference arrays |
||
198 | * |
||
199 | * @return array |
||
200 | * |
||
201 | * @throws YamlUnsupportedVariableException If a multidimensional array is given for value expansion |
||
202 | */ |
||
203 | 7 | private function evaluateArrayType ($frontMatterKey, $expandableValue, $arrayVariableNames) |
|
238 | |||
239 | /** |
||
240 | * Evaluate an string for FrontMatter variables and replace them with the corresponding values |
||
241 | * |
||
242 | * @param string $key The key of the Front Matter value |
||
243 | * @param string $string The string that will be evaluated |
||
244 | * @param bool $ignoreArrays When set to true, an exception won't be thrown when an array is found with the |
||
245 | * interpolation |
||
246 | * |
||
247 | * @return string The final string with variables evaluated |
||
248 | * |
||
249 | * @throws YamlUnsupportedVariableException A FrontMatter variable is not an int, float, or string |
||
250 | */ |
||
251 | 29 | private function evaluateBasicType ($key, $string, $ignoreArrays = false) |
|
271 | |||
272 | // |
||
273 | // Variable management |
||
274 | // |
||
275 | |||
276 | /** |
||
277 | * Get an array of FrontMatter variables in the specified string that need to be interpolated |
||
278 | * |
||
279 | * @param string $string |
||
280 | * |
||
281 | * @return string[] |
||
282 | */ |
||
283 | 29 | private function getFrontMatterVariables ($string) |
|
293 | |||
294 | /** |
||
295 | * Get the value of a FM variable or throw an exception |
||
296 | * |
||
297 | * @param string $key |
||
298 | * @param string $varName |
||
299 | * |
||
300 | * @return mixed |
||
301 | * @throws YamlVariableUndefinedException |
||
302 | */ |
||
303 | 22 | private function getVariableValue ($key, $varName) |
|
312 | |||
313 | // |
||
314 | // Utility functions |
||
315 | // |
||
316 | |||
317 | /** |
||
318 | * @param string $epochTime |
||
319 | * |
||
320 | * @return bool|\DateTime |
||
|
|||
321 | */ |
||
322 | 9 | private function castDateTimeTimezone ($epochTime) |
|
330 | |||
331 | /** |
||
332 | * @param $guess |
||
333 | * |
||
334 | * @return bool|\DateTime |
||
335 | */ |
||
336 | 7 | private function guessDateTime ($guess) |
|
353 | } |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.