GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 4598b2...94c39e )
by Zordius
03:20
created

Runtime::in()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
/*
3
4
MIT License
5
Copyright 2013-2019 Zordius Chen. All Rights Reserved.
6
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
10
Origin: https://github.com/zordius/lightncandy
11
*/
12
13
/**
14
 * file to support LightnCandy compiled PHP runtime
15
 *
16
 * @package    LightnCandy
17
 * @author     Zordius <[email protected]>
18
 */
19
20
namespace LightnCandy;
21
22
use \LightnCandy\Encoder;
23
24
/**
25
 * LightnCandy class for Object property access on a string.
26
 */
27
class StringObject
28
{
29
    protected $string;
30
31 2
    public function __construct($string)
32
    {
33 2
        $this->string = $string;
34 2
    }
35
36 2
    public function __toString()
37
    {
38 2
        return strval($this->string);
39
    }
40
}
41
42
/**
43
 * LightnCandy class for compiled PHP runtime.
44
 */
45
class Runtime extends Encoder
46
{
47
    const DEBUG_ERROR_LOG = 1;
48
    const DEBUG_ERROR_EXCEPTION = 2;
49
    const DEBUG_TAGS = 4;
50
    const DEBUG_TAGS_ANSI = 12;
51
    const DEBUG_TAGS_HTML = 20;
52
53
    /**
54
     * Output debug info.
55
     *
56
     * @param string $v expression
57
     * @param string $f runtime function name
58
     * @param array<string,array|string|integer> $cx render time context for lightncandy
59
     *
60
     * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), ''
61
     * @expect '<!--MISSED((-->{{#123}}<!--))--><!--SKIPPED--><!--MISSED((-->{{/123}}<!--))-->' when input '123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML), 'runtime' => 'LightnCandy\\Runtime'), false, null, false, function () {return 'A';}
62
     */
63 9
    public static function debug($v, $f, $cx)
64
    {
65
        // Build array of reference for call_user_func_array
66 9
        $P = func_get_args();
67 9
        $params = array();
68 9
        for ($i=2;$i<count($P);$i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
69 9
            $params[] = &$P[$i];
70
        }
71 9
        $r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params);
72
73 8
        if ($cx['flags']['debug'] & static::DEBUG_TAGS) {
74 5
            $ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS);
75 5
            $html = $cx['flags']['debug'] & (static::DEBUG_TAGS_HTML - static::DEBUG_TAGS);
76 5
            $cs = ($html ? (($r !== '') ? '<!!--OK((-->' : '<!--MISSED((-->') : '')
77 5
                  . ($ansi ? (($r !== '') ? "\033[0;32m" : "\033[0;31m") : '');
78 5
            $ce = ($html ? '<!--))-->' : '')
79 5
                  . ($ansi ? "\033[0m" : '');
80 5
            switch ($f) {
81 5
                case 'sec':
82 3
                case 'wi':
83 3
                    if ($r == '') {
84 3
                        if ($ansi) {
85 1
                            $r = "\033[0;33mSKIPPED\033[0m";
86
                        }
87 3
                        if ($html) {
88 2
                            $r = '<!--SKIPPED-->';
89
                        }
90
                    }
91 3
                    return "$cs{{#{$v}}}$ce{$r}$cs{{/{$v}}}$ce";
92
                default:
93 3
                    return "$cs{{{$v}}}$ce";
94
            }
95
        } else {
96 3
            return $r;
97
        }
98
    }
99
100
    /**
101
     * Handle error by error_log or throw exception.
102
     *
103
     * @param array<string,array|string|integer> $cx render time context for lightncandy
104
     * @param string $err error message
105
     *
106
     * @throws \Exception
107
     */
108 18
    public static function err($cx, $err)
109
    {
110 18
        if ($cx['flags']['debug'] & static::DEBUG_ERROR_LOG) {
111 5
            error_log($err);
112 5
            return;
113
        }
114 13
        if ($cx['flags']['debug'] & static::DEBUG_ERROR_EXCEPTION) {
115 9
            throw new \Exception($err);
116
        }
117 4
    }
118
119
    /**
120
     * Handle missing data error.
121
     *
122
     * @param array<string,array|string|integer> $cx render time context for lightncandy
123
     * @param string $v expression
124
     */
125 5
    public static function miss($cx, $v)
126
    {
127 5
        static::err($cx, "Runtime: $v does not exist");
128 4
    }
129
130
    /**
131
     * For {{log}} .
132
     *
133
     * @param array<string,array|string|integer> $cx render time context for lightncandy
134
     * @param string $v expression
135
     */
136
    public static function lo($cx, $v)
137
    {
138
        error_log(var_export($v[0], true));
139
        return '';
140
    }
141
142
    /**
143
     * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas.
144
     *
145
     * @param array<string,array|string|integer> $cx render time context for lightncandy
146
     * @param array|string|boolean|integer|double|null $in current context
147
     * @param array<array|string|integer> $base current variable context
148
     * @param array<string|integer> $path array of names for path
149
     * @param array|null $args extra arguments for lambda
150
     *
151
     * @return null|string Return the value or null when not found
152
     *
153
     * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b')
154
     * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b')
155
     * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, (Object) array('a' => array('b' => 3)), array('a', 'b')
156
     * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 1, 'method' => 0, 'mustlok' => 0)), null, (Object) array('a' => array('b' => 3)), array('a', 'b')
157
     */
158 388
    public static function v($cx, $in, $base, $path, $args = null)
159
    {
160 388
        $count = count($cx['scopes']);
161 388
        $plen = count($path);
162 388
        while ($base) {
163 352
            $v = $base;
164 352
            foreach ($path as $i => $name) {
165 352
                if (is_array($v)) {
166 350
                    if (isset($v[$name])) {
167 346
                        $v = $v[$name];
168 346
                        continue;
169
                    }
170 31
                    if (($i === $plen - 1) && ($name === 'length')) {
171 1
                        return count($v);
172
                    }
173
                }
174 37
                if (is_object($v)) {
175 6
                    if ($cx['flags']['prop'] && !($v instanceof \Closure) && isset($v->$name)) {
176 4
                        $v = $v->$name;
177 4
                        continue;
178
                    }
179 5
                    if ($cx['flags']['method'] && is_callable(array($v, $name))) {
180
                        try {
181 4
                            $v = $v->$name();
182 4
                            continue;
183
                        } catch (\BadMethodCallException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
184
                    }
185 3
                    if ($v instanceof \ArrayAccess) {
186
                        if (isset($v[$name])) {
187
                            $v = $v[$name];
188
                            continue;
189
                        }
190
                    }
191
                }
192 34
                if ($cx['flags']['mustlok']) {
193 31
                    unset($v);
194 31
                    break;
195
                }
196 3
                return null;
197
            }
198 351
            if (isset($v)) {
199 340
                if ($v instanceof \Closure) {
200 29
                    if ($cx['flags']['mustlam'] || $cx['flags']['lambda']) {
201 29
                        if (!$cx['flags']['knohlp'] && ($args || ($args === 0))) {
202 20
                            $A = $args ? $args[0] : array();
203 20
                            $A[] = array('hash' => $args[1], '_this' => $in);
204
                        } else {
205 10
                            $A = array($in);
206
                        }
207 29
                        $v = call_user_func_array($v, $A);
208
                    }
209
                }
210 340
                return $v;
211
            }
212 31
            $count--;
213 31
            switch ($count) {
214
                case -1:
215 30
                    $base = $cx['sp_vars']['root'];
216 30
                    break;
217
                case -2:
218 29
                    return null;
219
                default:
220 6
                    $base = $cx['scopes'][$count];
221
            }
222
        }
223 40
        if ($args) {
224 3
            static::err($cx, 'Can not find helper or lambda: "' . implode('.', $path) . '" !');
225
        }
226 40
    }
227
228
    /**
229
     * For {{#if}} .
230
     *
231
     * @param array<string,array|string|integer> $cx render time context for lightncandy
232
     * @param array<array|string|integer>|string|integer|null $v value to be tested
233
     * @param boolean $zero include zero as true
234
     *
235
     * @return boolean Return true when the value is not null nor false.
236
     *
237
     * @expect false when input array(), null, false
238
     * @expect false when input array(), 0, false
239
     * @expect true when input array(), 0, true
240
     * @expect false when input array(), false, false
241
     * @expect true when input array(), true, false
242
     * @expect true when input array(), 1, false
243
     * @expect false when input array(), '', false
244
     * @expect false when input array(), array(), false
245
     * @expect true when input array(), array(''), false
246
     * @expect true when input array(), array(0), false
247
     */
248 74
    public static function ifvar($cx, $v, $zero)
249
    {
250 74
        return ($v !== null) && ($v !== false) && ($zero || ($v !== 0) && ($v !== 0.0)) && ($v !== '') && (is_array($v) ? (count($v) > 0) : true);
251
    }
252
253
    /**
254
     * For {{^var}} .
255
     *
256
     * @param array<string,array|string|integer> $cx render time context for lightncandy
257
     * @param array<array|string|integer>|string|integer|null $v value to be tested
258
     *
259
     * @return boolean Return true when the value is not null nor false.
260
     *
261
     * @expect true when input array(), null
262
     * @expect false when input array(), 0
263
     * @expect true when input array(), false
264
     * @expect false when input array(), 'false'
265
     * @expect true when input array(), array()
266
     * @expect false when input array(), array('1')
267
     */
268 37
    public static function isec($cx, $v)
269
    {
270 37
        return ($v === null) || ($v === false) || (is_array($v) && (count($v) === 0));
271
    }
272
273
    /**
274
     * For {{var}} .
275
     *
276
     * @param array<string,array|string|integer> $cx render time context for lightncandy
277
     * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded
278
     *
279
     * @return string The htmlencoded value of the specified variable
280
     *
281
     * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a'
282
     * @expect 'a&amp;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
283
     * @expect 'a&#039;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
284
     * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b')
285
     */
286 46
    public static function enc($cx, $var)
287
    {
288 46
        if ($var instanceof \LightnCandy\SafeString) {
289 1
            return (string)$var;
290
        }
291
292 46
        return htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8');
293
    }
294
295
    /**
296
     * For {{var}} , do html encode just like handlebars.js .
297
     *
298
     * @param array<string,array|string|integer> $cx render time context for lightncandy
299
     * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded
300
     *
301
     * @return string The htmlencoded value of the specified variable
302
     *
303
     * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a'
304
     * @expect 'a&amp;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
305
     * @expect 'a&#x27;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
306
     * @expect '&#x60;a&#x27;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b'
307
     */
308 342
    public static function encq($cx, $var)
309
    {
310 342
        if ($var instanceof \LightnCandy\SafeString) {
311 3
            return (string)$var;
312
        }
313
314 339
        return str_replace(array('=', '`', '&#039;'), array('&#x3D;', '&#x60;', '&#x27;'), htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8'));
315
    }
316
317
    /**
318
     * For {{#var}} or {{#each}} .
319
     *
320
     * @param array<string,array|string|integer> $cx render time context for lightncandy
321
     * @param array<array|string|integer>|string|integer|null $v value for the section
322
     * @param array<string>|null $bp block parameters
323
     * @param array<array|string|integer>|string|integer|null $in input data with current scope
324
     * @param boolean $each true when rendering #each
325
     * @param Closure $cb callback function to render child context
326
     * @param Closure|null $else callback function to render child context when {{else}}
327
     *
328
     * @return string The rendered string of the section
329
     *
330
     * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';}
331
     * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';}
332
     * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';}
333
     * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';}
334
     * @expect '-a=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a'), null, array('a'), false, function ($c, $i) {return "-$i=";}
335
     * @expect '-a=-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a','b'), null, array('a','b'), false, function ($c, $i) {return "-$i=";}
336
     * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";}
337
     * @expect '-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a' => 'b'), null, array('a' => 'b'), true, function ($c, $i) {return "-$i=";}
338
     * @expect 'b' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return print_r($i, true);}
339
     * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 1, null, 1, false, function ($c, $i) {return print_r($i, true);}
340
     * @expect '0' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return print_r($i, true);}
341
     * @expect '{"b":"c"}' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('b' => 'c'), null, array('b' => 'c'), false, function ($c, $i) {return json_encode($i);}
342
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
343
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
344
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
345
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
346
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), '', null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
347
     * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), '', null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
348
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
349
     * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
350
     * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), new stdClass, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
351
     * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), new stdClass, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
352
     * @expect '268' when input array('scopes' => array(), 'flags' => array('spvar' => 1, 'mustlam' => 0, 'lambda' => 0), 'sp_vars'=>array('root' => 0)), array(1,3,4), null, 0, false, function ($c, $i) {return $i * 2;}
353
     * @expect '038' when input array('scopes' => array(), 'flags' => array('spvar' => 1, 'mustlam' => 0, 'lambda' => 0), 'sp_vars'=>array('root' => 0)), array(1,3,'a'=>4), null, 0, true, function ($c, $i) {return $i * $c['sp_vars']['index'];}
354
     */
355 171
    public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null)
356
    {
357 171
        $push = ($in !== $v) || $each;
358
359 171
        $isAry = is_array($v) || ($v instanceof \ArrayObject);
360 171
        $isTrav = $v instanceof \Traversable;
361 171
        $loop = $each;
362 171
        $keys = null;
363 171
        $last = null;
364 171
        $isObj = false;
365
366 171
        if ($isAry && $else !== null && count($v) === 0) {
367 3
            $ret = $else($cx, $in);
368 3
            return $ret;
369
        }
370
371
        // #var, detect input type is object or not
372 169
        if (!$loop && $isAry) {
373 66
            $keys = array_keys($v);
374 66
            $loop = (count(array_diff_key($v, array_keys($keys))) == 0);
375 66
            $isObj = !$loop;
376
        }
377
378 169
        if (($loop && $isAry) || $isTrav) {
379 107
            if ($each && !$isTrav) {
380
                // Detect input type is object or not when never done once
381 51
                if ($keys == null) {
382 51
                    $keys = array_keys($v);
383 51
                    $isObj = (count(array_diff_key($v, array_keys($keys))) > 0);
384
                }
385
            }
386 107
            $ret = array();
387 107
            if ($push) {
388 105
                $cx['scopes'][] = $in;
389
            }
390 107
            $i = 0;
391 107
            if ($cx['flags']['spvar']) {
392 96
                $old_spvar = $cx['sp_vars'];
393 96
                $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $old_spvar, array('_parent' => $old_spvar));
394 96
                if (!$isTrav) {
395 95
                    $last = count($keys) - 1;
396
                }
397
            }
398
399 107
            $isSparceArray = $isObj && (count(array_filter(array_keys($v), 'is_string')) == 0);
400 107
            foreach ($v as $index => $raw) {
401 100
                if ($cx['flags']['spvar']) {
402 90
                    $cx['sp_vars']['first'] = ($i === 0);
403 90
                    $cx['sp_vars']['last'] = ($i == $last);
404 90
                    $cx['sp_vars']['key'] = $index;
405 90
                    $cx['sp_vars']['index'] = $isSparceArray ? $index : $i;
406 90
                    $i++;
407
                }
408 100
                if (isset($bp[0])) {
409 4
                    $raw = static::m($cx, $raw, array($bp[0] => $raw));
410
                }
411 100
                if (isset($bp[1])) {
412 3
                    $raw = static::m($cx, $raw, array($bp[1] => $index));
413
                }
414 100
                $ret[] = $cb($cx, $raw);
415
            }
416 106
            if ($cx['flags']['spvar']) {
417 95
                if ($isObj) {
418 17
                    unset($cx['sp_vars']['key']);
419
                } else {
420 80
                    unset($cx['sp_vars']['last']);
421
                }
422 95
                unset($cx['sp_vars']['index']);
423 95
                unset($cx['sp_vars']['first']);
424 95
                $cx['sp_vars'] = $old_spvar;
425
            }
426 106
            if ($push) {
427 104
                array_pop($cx['scopes']);
428
            }
429 106
            return join('', $ret);
430
        }
431 66
        if ($each) {
432 2
            if ($else !== null) {
433 1
                $ret = $else($cx, $v);
434 1
                return $ret;
435
            }
436 2
            return '';
437
        }
438 65
        if ($isAry) {
439 11
            if ($push) {
440 10
                $cx['scopes'][] = $in;
441
            }
442 11
            $ret = $cb($cx, $v);
443 11
            if ($push) {
444 10
                array_pop($cx['scopes']);
445
            }
446 11
            return $ret;
447
        }
448
449 55
        if ($cx['flags']['mustsec']) {
450 20
            return $v ? $cb($cx, $in) : '';
451
        }
452
453 35
        if ($v === true) {
454 9
            return $cb($cx, $in);
455
        }
456
457 28
        if (($v !== null) && ($v !== false)) {
458 9
            return $cb($cx, $v);
459
        }
460
461 21
        if ($else !== null) {
462 10
            $ret = $else($cx, $in);
463 10
            return $ret;
464
        }
465
466 12
        return '';
467
    }
468
469
    /**
470
     * For {{#with}} .
471
     *
472
     * @param array<string,array|string|integer> $cx render time context for lightncandy
473
     * @param array<array|string|integer>|string|integer|null $v value to be the new context
474
     * @param array<array|string|integer>|string|integer|null $in input data with current scope
475
     * @param array<string>|null $bp block parameters
476
     * @param Closure $cb callback function to render child context
477
     * @param Closure|null $else callback function to render child context when {{else}}
478
     *
479
     * @return string The rendered string of the token
480
     *
481
     * @expect '' when input array(), false, null, false, function () {return 'A';}
482
     * @expect '' when input array(), null, null, null, function () {return 'A';}
483
     * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);}
484
     * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";}
485
     */
486 30
    public static function wi($cx, $v, $bp, $in, $cb, $else = null)
487
    {
488 30
        if (isset($bp[0])) {
489 4
            $v = static::m($cx, $v, array($bp[0] => $v));
490
        }
491 30
        if (($v === false) || ($v === null) || (is_array($v) && (count($v) === 0))) {
492 5
            return $else ? $else($cx, $in) : '';
493
        }
494 26
        if ($v === $in) {
495 5
            $ret = $cb($cx, $v);
496
        } else {
497 21
            $cx['scopes'][] = $in;
498 21
            $ret = $cb($cx, $v);
499 21
            array_pop($cx['scopes']);
500
        }
501 26
        return $ret;
502
    }
503
504
    /**
505
     * Get merged context.
506
     *
507
     * @param array<string,array|string|integer> $cx render time context for lightncandy
508
     * @param array<array|string|integer>|string|integer|null $a the context to be merged
509
     * @param array<array|string|integer>|string|integer|null $b the new context to overwrite
510
     *
511
     * @return array<array|string|integer>|string|integer the merged context object
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string|integer|null|StringObject? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
512
     *
513
     */
514 103
    public static function m($cx, $a, $b)
515
    {
516 103
        if (is_array($b)) {
517 103
            if ($a === null) {
518 14
                return $b;
519 92
            } elseif (is_array($a)) {
520 82
                return array_merge($a, $b);
521 10
            } elseif ($cx['flags']['method'] || $cx['flags']['prop']) {
522 6
                if (!is_object($a)) {
523 2
                    $a = new StringObject($a);
524
                }
525 6
                foreach ($b as $i => $v) {
526 2
                    $a->$i = $v;
527
                }
528
            }
529
        }
530 10
        return $a;
531
    }
532
533
    /**
534
     * For {{> partial}} .
535
     *
536
     * @param array<string,array|string|integer> $cx render time context for lightncandy
537
     * @param string $p partial name
538
     * @param array<array|string|integer>|string|integer|null $v value to be the new context
539
     *
540
     * @return string The rendered string of the partial
541
     *
542
     */
543 100
    public static function p($cx, $p, $v, $pid, $sp = '')
544
    {
545 100
        $pp = ($p === '@partial-block') ? "$p" . ($pid > 0 ? $pid : $cx['partialid']) : $p;
546
547 100
        if (!isset($cx['partials'][$pp])) {
548 8
            static::err($cx, "Can not find partial named as '$p' !!");
549 4
            return '';
550
        }
551
552 94
        $cx['partialid'] = ($p === '@partial-block') ? (($pid > 0) ? $pid : (($cx['partialid'] > 0) ? $cx['partialid'] - 1 : 0)) : $pid;
553
554 94
        return call_user_func($cx['partials'][$pp], $cx, static::m($cx, $v[0][0], $v[1]), $sp);
555
    }
556
557
    /**
558
     * For {{#* inlinepartial}} .
559
     *
560
     * @param array<string,array|string|integer> $cx render time context for lightncandy
561
     * @param string $p partial name
562
     * @param Closure $code the compiled partial code
563
     *
564
     */
565 9
    public static function in(&$cx, $p, $code)
566
    {
567 9
        $cx['partials'][$p] = $code;
568 9
    }
569
570
    /* For single custom helpers.
571
     *
572
     * @param array<string,array|string|integer> $cx render time context for lightncandy
573
     * @param string $ch the name of custom helper to be executed
574
     * @param array<array|string|integer>|string|integer|null $vars variables for the helper
575
     * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'.
576
     * @param array<string,array|string|integer> $_this current rendering context for the helper
577
     *
578
     * @return string The rendered string of the token
579
     */
580 121
    public static function hbch(&$cx, $ch, $vars, $op, &$_this)
581
    {
582 121
        if (isset($cx['blparam'][0][$ch])) {
583 1
            return $cx['blparam'][0][$ch];
584
        }
585
586
        $options = array(
587 121
            'name' => $ch,
588 121
            'hash' => $vars[1],
589 121
            'contexts' => count($cx['scopes']) ? $cx['scopes'] : array(null),
590 121
            'fn.blockParams' => 0,
591 121
            '_this' => &$_this
592
        );
593
594 121
        if ($cx['flags']['spvar']) {
595 116
            $options['data'] = &$cx['sp_vars'];
596
        }
597
598 121
        return static::exch($cx, $ch, $vars, $options);
599
    }
600
601
    /**
602
     * For block custom helpers.
603
     *
604
     * @param array<string,array|string|integer> $cx render time context for lightncandy
605
     * @param string $ch the name of custom helper to be executed
606
     * @param array<array|string|integer>|string|integer|null $vars variables for the helper
607
     * @param array<string,array|string|integer> $_this current rendering context for the helper
608
     * @param boolean $inverted the logic will be inverted
609
     * @param Closure|null $cb callback function to render child context
610
     * @param Closure|null $else callback function to render child context when {{else}}
611
     *
612
     * @return string The rendered string of the token
613
     */
614 57
    public static function hbbch(&$cx, $ch, $vars, &$_this, $inverted, $cb, $else = null)
615
    {
616
        $options = array(
617 57
            'name' => $ch,
618 57
            'hash' => $vars[1],
619 57
            'contexts' => count($cx['scopes']) ? $cx['scopes'] : array(null),
620 57
            'fn.blockParams' => 0,
621 57
            '_this' => &$_this,
622
        );
623
624 57
        if ($cx['flags']['spvar']) {
625 53
            $options['data'] = &$cx['sp_vars'];
626
        }
627
628 57
        if (isset($vars[2])) {
629 3
            $options['fn.blockParams'] = count($vars[2]);
630
        }
631
632
        // $invert the logic
633 57
        if ($inverted) {
634
            $tmp = $else;
635
            $else = $cb;
636
            $cb = $tmp;
637
        }
638
639
        $options['fn'] = function ($context = '_NO_INPUT_HERE_', $data = null) use ($cx, &$_this, $cb, $options, $vars) {
640 46
            if ($cx['flags']['echo']) {
641
                ob_start();
642
            }
643 46
            if (isset($data['data'])) {
644 6
                $old_spvar = $cx['sp_vars'];
645 6
                $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $data['data'], array('_parent' => $old_spvar));
646
            }
647 46
            $ex = false;
648 46
            if (isset($data['blockParams']) && isset($vars[2])) {
649 3
                $ex = array_combine($vars[2], array_slice($data['blockParams'], 0, count($vars[2])));
650 3
                array_unshift($cx['blparam'], $ex);
651 44
            } elseif (isset($cx['blparam'][0])) {
652 1
                $ex = $cx['blparam'][0];
653
            }
654 46
            if (($context === '_NO_INPUT_HERE_') || ($context === $_this)) {
655 29
                $ret = $cb($cx, is_array($ex) ? static::m($cx, $_this, $ex) : $_this);
656
            } else {
657 20
                $cx['scopes'][] = $_this;
658 20
                $ret = $cb($cx, is_array($ex) ? static::m($cx, $context, $ex) : $context);
659 20
                array_pop($cx['scopes']);
660
            }
661 46
            if (isset($data['data'])) {
662 6
                $cx['sp_vars'] = $old_spvar;
663
            }
664 46
            return $cx['flags']['echo'] ? ob_get_clean() : $ret;
665
        };
666
667 57
        if ($else) {
668
            $options['inverse'] = function ($context = '_NO_INPUT_HERE_') use ($cx, $_this, $else) {
669 6
                if ($cx['flags']['echo']) {
670
                    ob_start();
671
                }
672 6
                if ($context === '_NO_INPUT_HERE_') {
673 1
                    $ret = $else($cx, $_this);
674
                } else {
675 5
                    $cx['scopes'][] = $_this;
676 5
                    $ret = $else($cx, $context);
677 5
                    array_pop($cx['scopes']);
678
                }
679 6
                return $cx['flags']['echo'] ? ob_get_clean() : $ret;
680 10
            };
681
        } else {
682
            $options['inverse'] = function () {
683
                return '';
684
            };
685
        }
686
687 57
        return static::exch($cx, $ch, $vars, $options);
688
    }
689
690
    /**
691
     * Execute custom helper with prepared options
692
     *
693
     * @param array<string,array|string|integer> $cx render time context for lightncandy
694
     * @param string $ch the name of custom helper to be executed
695
     * @param array<array|string|integer>|string|integer|null $vars variables for the helper
696
     * @param array<string,array|string|integer> $options the options object
697
     *
698
     * @return string The rendered string of the token
699
     */
700 168
    public static function exch($cx, $ch, $vars, &$options)
701
    {
702 168
        $args = $vars[0];
703 168
        $args[] = &$options;
704 168
        $r = true;
705
706
        try {
707 168
            $r = call_user_func_array($cx['helpers'][$ch], $args);
708 2
        } catch (\Exception $E) {
709 2
            static::err($cx, "Runtime: call custom helper '$ch' error: " . $E->getMessage());
710
        }
711
712 167
        return $r;
713
    }
714
}
715