Prepare   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 252
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sub() 0 9 2
B multi() 0 30 4
B template() 0 52 4
B placeholders() 0 47 5
B hooks() 0 22 4
A params() 0 13 3
1
<?php
2
3
/**
4
 * Contains template string prepare 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 prepare 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 Prepare
30
{
31
    /**
32
     * List of commands that can pass to parse mode
33
     **/
34
    private static $_var = ['var' => 0, 'url' => 1, 'raw' => 2, 'form' => 3];
35
36
    /**
37
     * List of commands that need the plugin name
38
     **/
39
    private static $_plugin = ['com' => 0, 'lang' => 1, 'tpl' => 2];
40
41
    /**
42
     * List of commands that can be skipped at hooks
43
     **/
44
    private static $_skip = ['page' => 0, 'debug' => 1];
45
46
    /**
47
     * Prepares nested array targets
48
     *
49
     * @param array $part Placeholder cmd and key, maybe even more
50
     *
51
     * @return array
52
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
53
54
    public static function sub(array $part)
55
    {
56
        $part['hub'] = $part['key'];
57
        $hub         = explode('.', $part['key'], 2);
58
        $part['key'] = $hub[0];
59
        $part['sub'] = isset($hub[1]) ? $hub[1] : '';
60
61
        return $part;
62
    }
63
64
    /**
65
     * Changes text to a multi placeholder array
66
     *
67
     * @param string $string The string to split in parts
68
     * @param string $cmd    The cmd to use for matches (defaults to "var")
69
     *
70
     * @return array
71
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
72
73
    public static function multi($string, $cmd = 'var')
74
    {
75
        $tokens = explode('$', $string);
76
        $parts  = [];
77
78
        // Every second array index should be a var element
79
        $tokens_c = count($tokens);
80
81
        for ($i = 0; $i < $tokens_c; $i+=2) {
82
83
            // Skip text placeholders with empty string
84
            if ($tokens[$i] != '') {
85
86
                $parts[] = ['cmd' => 'text', 'text' => $tokens[$i]];
87
            }
88
89
            // Check if there is another var element
90
            if (isset($tokens[($i + 1)])) {
91
92
                $add = ['cmd' => $cmd, 'key' => $tokens[($i + 1)]];
93
94
                // Var element is maybe targeting an array key
95
                $parts[] = self::sub($add);
96
            }
97
        }
98
99
        $part = ['cmd' => 'multi', 'value' => $parts];
100
101
        return $part;
102
    }
103
104
    /**
105
     * Split template file logic
106
     *
107
     * @param string $string Template file part as a string
108
     * @param string $plugin Name of the plugin for tpl files
109
     * @param array  $coms   Array of commands to replace with others
110
     *
111
     * @return array
112
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
113
114
    public static function template($string, $plugin = '', array $coms = [])
115
    {
116
        // Add form end placeholder to form closing tags
117
        $pattern = "'((?:\{\* form end \*\}[\s]*)*\<\/form\>)'sS";
118
        $replace = '{* form end *}' . "\n" . '</form>';
119
        $string  = preg_replace($pattern, $replace, $string);
120
121
        // Split string into an array of foreach and if parts
122
        $pattern  = "'\{\* (?P<cmd>foreach|if)"
123
                  . " (?P<key>[\S]+?)"
124
                  . "(?: (?P<equal>\=\=|\!\=|\<|\>)"
125
                  . " \'(?P<cond>[\S]*?)\'){0,1} \*\}"
126
                  . "(?P<value>.*?)[\s]*"
127
                  . "(?:\{\* else (?P=key) \*\}(?P<else>.*?)[\s]*){0,1}"
128
                  . "\{\* (end)(?P=cmd) (?P=key) \*\}"
129
                  . "'sS";
130
        $template = preg_split($pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE);
131
132
        $parts = count($template);
133
134
        $new = self::placeholders($template[0], $plugin, $coms);
135
136
        // Parts: i1 = cmd, i2 = key, i3 = equal i4 = cond, i5 = value, i6 = else
137
        for ($i = 1; $i < $parts; $i++) {
138
139
            // Nesting of foreach and if tags
140
            if ($template[$i] == 'foreach' || $template[$i] == 'if') {
141
142
                $split = self::template($template[($i + 4)], $plugin, $coms);
143
                $else  = self::template($template[($i + 5)], $plugin, $coms);
144
145
                $next = ['cmd'   => $template[$i],
146
                         'key'   => $template[($i + 1)],
147
                         'equal' => $template[($i + 2)],
148
                         'cond'  => $template[($i + 3)],
149
                         'value' => $split,
150
                         'else'  => $else];
151
152
                $new[] = self::sub($next);
153
154
                // Add the part next to the nested content
155
                $add = self::placeholders($template[($i + 7)], $plugin, $coms);
156
157
                $new = array_merge($new, $add);
158
159
                // Skip end commands
160
                $i += 7;
161
            }
162
        }
163
164
        return $new;
165
    }
166
167
    /**
168
     * Split template file placeholders
169
     *
170
     * @param string $string Template file part as a string
171
     * @param string $plugin Name of the plugin for tpl files
172
     * @param array  $coms   Array of commands to replace with others
173
     *
174
     * @return array
175
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
176
177
    public static function placeholders($string, $plugin, array $coms = [])
178
    {
179
        // Split string into an array of placeholders and content
180
        $search   = "'\{\* (?P<cmd>[\S]+?)"
181
                  . " (?P<key>.*?) \*\}"
182
                  . "'S";
183
        $template = preg_split($search, $string, -1, PREG_SPLIT_DELIM_CAPTURE);
184
185
        $new = [];
186
187
        // Skip text placeholders with empty string
188
        if ($template[0] != '') {
189
190
            $new[] = ['cmd' => 'text', 'text' => $template[0]];
191
        }
192
193
        $traps = count($template);
194
195
        // Content parts: j1 = cmd, j2 = key, j3 = value
196
        for ($j = 1; $j < $traps; $j++) {
197
198
            // Run hooks and add array elements afterwards
199
            try {
200
                $new[] = self::hooks(
201
                    $template[$j], $template[($j + 1)], $plugin, $coms
202
                );
203
            }
204
            catch (\Exception $exception) {
205
206
                // Continue to not cause further problems
207
                $cont = new \csphere\core\Errors\Controller($exception, true);
208
209
                unset($cont);
210
            }
211
212
            // Skip text placeholders with empty string
213
            if ($template[($j + 2)] != '') {
214
215
                $new[] = ['cmd' => 'text', 'text' => $template[($j + 2)]];
216
            }
217
218
            // Skip end of placeholder
219
            $j += 2;
220
        }
221
222
        return $new;
223
    }
224
225
    /**
226
     * Hooks for placeholders
227
     *
228
     * @param string $cmd    Command of placeholder
229
     * @param string $key    Key data of placeholder
230
     * @param string $plugin Name of the plugin for tpl files
231
     * @param array  $coms   Array of commands to replace with others
232
     *
233
     * @return array
234
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
235
236
    public static function hooks($cmd, $key, $plugin, array $coms)
237
    {
238
        // Combine array and add plugin if required by placeholder
239
        $next = ['cmd' => $cmd, 'key' => $key];
240
241
        if (isset(self::$_plugin[$cmd])) {
242
243
            $next['plugin'] = $plugin;
244
        }
245
246
        // Run sub array for vars or run cmd prepare
247
        if (isset(self::$_var[$cmd])) {
248
249
            $next = self::sub($next);
250
251
        } elseif (!isset(self::$_skip[$cmd])) {
252
253
            $next = \csphere\core\template\CMD_Prepare::$cmd($next, $coms);
254
        }
255
256
        return $next;
257
    }
258
259
    /**
260
     * Parameters for placeholders
261
     *
262
     * @param array $array Parameters as an array
263
     *
264
     * @return array
265
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
266
267
    public static function params(array $array)
268
    {
269
        $cmds   = [];
270
        $splits = count($array);
271
272
        for ($i = 1; $i < $splits; $i++) {
273
274
            $split           = explode('=', $array[$i], 2);
275
            $cmds[$split[0]] = isset($split[1]) ? $split[1] : '';
276
        }
277
278
        return $cmds;
279
    }
280
}
281