Parse::_math()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 8
nc 2
nop 1
dl 0
loc 16
rs 7.756
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Contains template string parse tools
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Template
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\template;
17
18
/**
19
 * Contains template string parse tools
20
 *
21
 * @category  Core
22
 * @package   Template
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Parse
30
{
31
    /**
32
     * List of commands that can contain a variable
33
     **/
34
    private static $_var = ['var' => 0, 'url' => 1, 'raw' => 2];
35
36
    /**
37
     * Adds requested data as a key to the part array after some checks
38
     *
39
     * @param array $part Template file part as an array
40
     * @param array $data Array with data to use in the template
41
     *
42
     * @return array
43
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
44
45
    public static function sub(array $part, array $data)
46
    {
47
        // Set requested data as new array element
48
        if ($part['sub'] == '' && isset($data[$part['key']])) {
49
50
            $part['data'] = $data[$part['key']];
51
52
        } elseif (isset($data[$part['key']][$part['sub']])) {
53
54
            $part['data'] = $data[$part['key']][$part['sub']];
55
56
        } else {
57
58
            $part['data'] = '';
59
60
            // Throwing an exception does not make sense here
61
            $msg = 'CMD "' . $part['cmd'] . '" without data: ' . $part['hub'];
62
            trigger_error($msg, E_USER_WARNING);
63
        }
64
65
        return $part;
66
    }
67
68
    /**
69
     * Parses nested foreach loops
70
     *
71
     * @param array $part Template file part as an array
72
     * @param array $data Array with data to use in the template
73
     *
74
     * @return string
75
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
76
77
    private static function _loops(array $part, array $data)
78
    {
79
        $part  = self::sub($part, $data);
80
        $nest  = $part['sub'];
81
        $value = [];
82
        $all   = '<!-- foreach ' . $part['hub'] . ' -->';
83
84
        if ($part['sub'] == '') {
85
86
            $nest = $part['key'];
87
        }
88
89
        if (is_array($part['value'])) {
90
91
            $value = $part['value'];
92
        }
93
94
        // Check if required data exists
95
        if ($part['data'] != []) {
96
97
            // Loop through data array
98
            foreach ($part['data'] AS $set) {
99
100
                $set = array_merge($data, [$nest => $set]);
101
102
                $all .= self::template($value, $set);
103
            }
104
105
        } elseif ($part['else'] != []) {
106
107
            $all .= '<!-- else ' . $part['hub'] . ' -->';
108
109
            $all .= self::template($part['else'], $part['data']);
110
        }
111
112
        $all .= "\n" . '<!-- endforeach ' . $part['hub'] . ' -->';
113
114
        return $all;
115
    }
116
117
    /**
118
     * Checks if data matches the given key
119
     *
120
     * @param array $part Template file part as an array
121
     *
122
     * @return boolean
123
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
124
125
    private static function _math(array $part)
126
    {
127
        $result = false;
128
129
        // Check condition against data array
130
        if (($part['equal'] == '==' && $part['cond'] == $part['data'])
131
            || ($part['equal'] == '!=' && $part['cond'] != $part['data'])
132
            || ($part['equal'] == '>' && (int)$part['cond'] < $part['data'])
133
            || ($part['equal'] == '<' && (int)$part['cond'] > $part['data'])
134
        ) {
135
136
            $result = true;
137
        }
138
139
        return $result;
140
    }
141
142
    /**
143
     * Parses nested conditions
144
     *
145
     * @param array $part Template file part as an array
146
     * @param array $data Array with data to use in the template
147
     *
148
     * @return string
149
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
150
151
    private static function _conditions(array $part, array $data)
152
    {
153
        $part  = self::sub($part, $data);
154
        $check = self::_math($part);
155
        $value = [];
156
        $all   = '<!-- if ' . $part['hub'] . ' -->';
157
158
        if (is_array($part['value'])) {
159
160
            $value = $part['value'];
161
        }
162
163
        // Only parse sub content if check is true
164
        if ($check == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
165
166
            $all .= self::template($value, $data);
167
168
        } elseif ($part['else'] != []) {
169
170
            $all .= '<!-- else ' . $part['hub'] . ' -->';
171
172
            $all .= self::template($part['else'], $data);
173
        }
174
175
        $all .= '<!-- endif ' . $part['hub'] . ' -->';
176
177
        return $all;
178
    }
179
180
    /**
181
     * Add required var content from data array
182
     *
183
     * @param array $part Template file part as an array
184
     * @param array $data Array with data to use in the template
185
     *
186
     * @return string
187
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
188
189
    private static function _vars(array $part, array $data)
190
    {
191
        $part = self::sub($part, $data);
192
193
        if (is_array($part['data'])) {
194
195
            $escape = '';
196
197
            // Throwing an exception does not make sense here
198
            $msg = 'CMD "' . $part['cmd'] . '" is an array: ' . $part['hub'];
199
            trigger_error($msg, E_USER_WARNING);
200
201
        } elseif ($part['cmd'] == 'url') {
202
203
            // CMD url needs to be rawurlencoded
204
            $escape = rawurlencode($part['data']);
205
206
        } elseif ($part['cmd'] == 'var') {
207
208
            // HHVM does not support ENT_SUBSTITUTE and ENT_HTML5 yet
209
            $escape = htmlspecialchars($part['data'], ENT_QUOTES, 'UTF-8', false);
210
211
            // @TODO Maybe another way ??
212
            if (isset($data['action']) && $data['action'] != "edit") {
213
                $escape = nl2br($escape, false);
214
            }
215
216
        } else {
217
218
            // Raw data is not escaped
219
            $escape = $part['data'];
220
        }
221
222
        return $escape;
223
    }
224
225
    /**
226
     * Processes a single placeholder
227
     *
228
     * @param array $part Template file part as an array
229
     * @param array $data Array with data to use in the template
230
     *
231
     * @return string
232
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
233
234
    private static function _advanced(array $part, array $data)
235
    {
236
        $cmd = $part['cmd'];
237
238
        // Logic parts first
239
        if ($cmd == 'if') {
240
241
            $string = self::_conditions($part, $data);
242
243
        } elseif ($cmd == 'foreach') {
244
245
            $string = self::_loops($part, $data);
246
247
        } elseif ($cmd == 'multi') {
248
249
            $string = self::template($part['value'], $data);
250
251
        } elseif ($cmd == 'text') {
252
253
            $string = $part['text'];
254
255
        } elseif (isset(self::$_var[$cmd])) {
256
257
            $string = self::_vars($part, $data);
258
259
        } else {
260
261
            // Transform remaining CMD calls
262
            $string = \csphere\core\template\CMD_Parse::$cmd($part, $data);
263
        }
264
265
        return $string;
266
    }
267
268
    /**
269
     * Combine template file content for usage
270
     *
271
     * @param array $array Template file (part) as an array
272
     * @param array $data  Array with data to use in the template
273
     *
274
     * @return string
275
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
276
277
    public static function template(array $array, array $data)
278
    {
279
        // Combine array of parts to a string
280
        $string = '';
281
282
        foreach ($array AS $part) {
283
284
            $string .= self::_advanced($part, $data);
285
        }
286
287
        return $string;
288
    }
289
}
290