Completed
Pull Request — master (#20)
by Vladimir
02:10
created

FrontMatterParser::evaluateBasicType()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 5
rs 8.8571
1
<?php
2
3
/**
4
 * @copyright 2016 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\FrontMatter;
9
10
use allejo\stakx\Utilities\ArrayUtilities;
11
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 15
    public function __construct(&$rawFrontMatter)
65
    {
66 15
        $this->expansionUsed = false;
67 15
        $this->nestingLevel = 0;
68 15
        $this->yamlKeys = array();
69
70 15
        $this->frontMatter = &$rawFrontMatter;
71
72 15
        $this->evaluateBlock($this->frontMatter);
73 10
    }
74
75
    /**
76
     * True if any fields were expanded in the Front Matter block
77
     *
78
     * @return bool
79
     */
80 5
    public function hasExpansion ()
81
    {
82 5
        return $this->expansionUsed;
83
    }
84
85
    /**
86
     * Evaluate an array as Front Matter
87
     *
88
     * @param array $yaml
89
     */
90 15
    private function evaluateBlock (&$yaml)
91
    {
92 15
        $this->nestingLevel++;
93
94 15
        foreach ($yaml as $key => &$value)
95
        {
96 15
            $this->yamlKeys[$this->nestingLevel] = $key;
97 15
            $keys = implode('.', $this->yamlKeys);
98
99 15
            if (in_array($key, self::$expandableFields, true))
100 15
            {
101 5
                $value = $this->evaluateExpandableField($keys, $value);
102 4
            }
103 15
            else if (is_array($value))
104 15
            {
105 5
                $this->evaluateBlock($value);
106 5
            }
107
            else
108
            {
109 15
                $value = $this->evaluateBasicType($keys, $value);
110
            }
111 13
        }
112
113 12
        $this->nestingLevel--;
114 12
        $this->yamlKeys = array();
115 12
    }
116
117
    /**
118
     * Evaluate an expandable field
119
     *
120
     * @param  string $key
121
     * @param  string $fmStatement
122
     *
123
     * @return array
124
     */
125 5
    private function evaluateExpandableField ($key, $fmStatement)
126
    {
127 5
        if (!is_array($fmStatement))
128 5
        {
129 4
            $fmStatement = array($fmStatement);
130 4
        }
131
132 5
        $wip = array();
133
134 5
        foreach ($fmStatement as $statement)
135
        {
136 5
            $value = $this->evaluateBasicType($key, $statement, true);
137
138
            // Only continue expansion if there are Front Matter variables remain in the string, this means there'll be
139
            // Front Matter variables referencing arrays
140 5
            $expandingVars = $this->getFrontMatterVariables($value);
141 5
            if (!empty($expandingVars))
142 5
            {
143 3
                $value = $this->evaluateArrayType($key, $value, $expandingVars);
144 2
            }
145
146 4
            $wip[] = $value;
147 4
        }
148
149 4
        return $wip;
150
    }
151
152
    /**
153
     * Convert a string or an array into an array of ExpandedValue objects created through "value expansion"
154
     *
155
     * @param  string $frontMatterKey     The current hierarchy of the Front Matter keys being used
156
     * @param  string $expandableValue    The Front Matter value that will be expanded
157
     * @param  array  $arrayVariableNames The Front Matter variable names that reference arrays
158
     *
159
     * @return array
160
     *
161
     * @throws YamlUnsupportedVariableException If a multidimensional array is given for value expansion
162
     */
163 3
    private function evaluateArrayType ($frontMatterKey, $expandableValue, $arrayVariableNames)
164
    {
165 3
        if (!is_array($expandableValue))
166 3
        {
167 3
            $expandableValue = array($expandableValue);
168 3
        }
169
170 3
        $this->expansionUsed = true;
171
172 3
        foreach ($arrayVariableNames as $variable)
173
        {
174 3
            if (ArrayUtilities::is_multidimensional($this->frontMatter[$variable]))
175 3
            {
176 1
                throw new YamlUnsupportedVariableException("Yaml array expansion is not supported with multidimensional arrays with `$variable` for key `$frontMatterKey`");
177
            }
178
179 2
            $wip = array();
180
181 2
            foreach ($expandableValue as &$statement)
182
            {
183 2
                foreach ($this->frontMatter[$variable] as $value)
184
                {
185 2
                    $evaluatedValue = ($statement instanceof ExpandedValue) ? clone($statement) : new ExpandedValue($statement);
186 2
                    $evaluatedValue->setEvaluated(str_replace('%' . $variable, $value, $evaluatedValue->getEvaluated()));
187 2
                    $evaluatedValue->setIterator($variable, $value);
188
189 2
                    $wip[] = $evaluatedValue;
190 2
                }
191 2
            }
192
193 2
            $expandableValue = $wip;
194 2
        }
195
196 2
        return $expandableValue;
197
    }
198
199
    /**
200
     * Evaluate an string for FrontMatter variables and replace them with the corresponding values
201
     *
202
     * @param  string $key          The key of the Front Matter value
203
     * @param  string $string       The string that will be evaluated
204
     * @param  bool   $ignoreArrays When set to true, an exception won't be thrown when an array is found with the
205
     *                              interpolation
206
     *
207
     * @return string The final string with variables evaluated
208
     *
209
     * @throws YamlUnsupportedVariableException A FrontMatter variable is not an int, float, or string
210
     */
211 15
    private function evaluateBasicType ($key, $string, $ignoreArrays = false)
212
    {
213 15
        $variables = $this->getFrontMatterVariables($string);
214
215 15
        foreach ($variables as $variable)
216
        {
217 14
            $value = $this->getVariableValue($key, $variable);
218
219 12
            if (is_array($value) || is_bool($value))
220 12
            {
221 5
                if ($ignoreArrays) { continue; }
222
223 2
                throw new YamlUnsupportedVariableException("Yaml variable `$variable` for `$key` is not a supported data type.");
224
            }
225
226 7
            $string = str_replace('%' . $variable, $value, $string);
227 13
        }
228
229 13
        return $string;
230
    }
231
232
    /**
233
     * Get an array of FrontMatter variables in the specified string that need to be interpolated
234
     *
235
     * @param  string $string
236
     *
237
     * @return string[]
238
     */
239 15
    private function getFrontMatterVariables ($string)
240
    {
241 15
        $variables = array();
242
243 15
        preg_match_all(self::VARIABLE_DEF, $string, $variables);
244
245
        // Default behavior causes $variables[0] is the entire string that was matched. $variables[1] will be each
246
        // matching result individually.
247 15
        return $variables[1];
248
    }
249
250
    /**
251
     * Get the value of a FM variable or throw an exception
252
     *
253
     * @param  string $key
254
     * @param  string $varName
255
     *
256
     * @return mixed
257
     * @throws YamlVariableUndefinedException
258
     */
259 14
    private function getVariableValue ($key, $varName)
260
    {
261 14
        if (!isset($this->frontMatter[$varName]))
262 14
        {
263 2
            throw new YamlVariableUndefinedException("Yaml variable `$varName` is not defined for: $key");
264
        }
265
266 12
        return $this->frontMatter[$varName];
267
    }
268
}