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 | 40 | public function __construct(&$rawFrontMatter) |
|
75 | |||
76 | /** |
||
77 | * True if any fields were expanded in the Front Matter block. |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | 18 | public function hasExpansion() |
|
85 | |||
86 | // |
||
87 | // Special FrontMatter fields |
||
88 | // |
||
89 | |||
90 | /** |
||
91 | * Special treatment for some FrontMatter variables. |
||
92 | */ |
||
93 | 40 | private function handleSpecialFrontMatter() |
|
97 | |||
98 | /** |
||
99 | * Special treatment for the `date` field in FrontMatter that creates three new variables: year, month, day. |
||
100 | */ |
||
101 | 40 | private function handleDateField() |
|
119 | |||
120 | // |
||
121 | // Evaluation |
||
122 | // |
||
123 | |||
124 | /** |
||
125 | * Evaluate an array as Front Matter. |
||
126 | * |
||
127 | * @param array $yaml |
||
128 | */ |
||
129 | 40 | private function evaluateBlock(&$yaml) |
|
159 | |||
160 | /** |
||
161 | * Evaluate an expandable field. |
||
162 | * |
||
163 | * @param string $key |
||
164 | * @param string $fmStatement |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 21 | private function evaluateExpandableField($key, $fmStatement) |
|
194 | |||
195 | /** |
||
196 | * Convert a string or an array into an array of ExpandedValue objects created through "value expansion". |
||
197 | * |
||
198 | * @param string $frontMatterKey The current hierarchy of the Front Matter keys being used |
||
199 | * @param string $expandableValue The Front Matter value that will be expanded |
||
200 | * @param array $arrayVariableNames The Front Matter variable names that reference arrays |
||
201 | * |
||
202 | * @throws YamlUnsupportedVariableException If a multidimensional array is given for value expansion |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | 4 | private function evaluateArrayType($frontMatterKey, $expandableValue, $arrayVariableNames) |
|
241 | |||
242 | /** |
||
243 | * Evaluate an string for FrontMatter variables and replace them with the corresponding values. |
||
244 | * |
||
245 | * @param string $key The key of the Front Matter value |
||
246 | * @param string $string The string that will be evaluated |
||
247 | * @param bool $ignoreArrays When set to true, an exception won't be thrown when an array is found with the |
||
248 | * interpolation |
||
249 | * |
||
250 | * @throws YamlUnsupportedVariableException A FrontMatter variable is not an int, float, or string |
||
251 | * |
||
252 | * @return string The final string with variables evaluated |
||
253 | */ |
||
254 | 39 | private function evaluateBasicType($key, $string, $ignoreArrays = false) |
|
277 | |||
278 | // |
||
279 | // Variable management |
||
280 | // |
||
281 | |||
282 | /** |
||
283 | * Get an array of FrontMatter variables in the specified string that need to be interpolated. |
||
284 | * |
||
285 | * @param string $string |
||
286 | * |
||
287 | * @return string[] |
||
288 | */ |
||
289 | 39 | private function getFrontMatterVariables($string) |
|
299 | |||
300 | /** |
||
301 | * Get the value of a FM variable or throw an exception. |
||
302 | * |
||
303 | * @param string $key |
||
304 | * @param string $varName |
||
305 | * |
||
306 | * @throws YamlVariableUndefinedException |
||
307 | * |
||
308 | * @return mixed |
||
309 | */ |
||
310 | 22 | private function getVariableValue($key, $varName) |
|
319 | |||
320 | // |
||
321 | // Utility functions |
||
322 | // |
||
323 | |||
324 | /** |
||
325 | * @param string $epochTime |
||
326 | * |
||
327 | * @return bool|\DateTime |
||
|
|||
328 | */ |
||
329 | 6 | private function castDateTimeTimezone($epochTime) |
|
337 | |||
338 | /** |
||
339 | * @param $guess |
||
340 | * |
||
341 | * @return bool|\DateTime |
||
342 | */ |
||
343 | 7 | private function guessDateTime($guess) |
|
363 | } |
||
364 |
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.