Completed
Push — master ( 09ae22...bc0d3f )
by Edson
01:06
created

RepeatTag::handle()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 1
dl 0
loc 21
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
class RepeatTag extends Tag
6
{
7
    public function __construct()
8
    {
9
        parent::__construct(
10
            '/{(\s?)+repeat (\s?)+([\d]+)(\s?)+(:?)(\s?)+([\w]+)?(\s?)+}(.*?){(\s?)+\/repeat(\s?)+}/is'
11
        );
12
    }
13
14
    public function handle(array $match): string
15
    {
16
        static $count = 0;
17
18
        if (!empty($match[5]) && empty($match[7])) {
19
            throw new \Exception("Invalid tag"); // @codeCoverageIgnore
20
        }
21
22
        if (empty($match[5]) && !empty($match[7])) {
23
            throw new \Exception("Invalid tag"); // @codeCoverageIgnore
24
        }
25
26
        $index = !empty($match[5]) && !empty($match[7])
27
            ? $match[7]
28
            : 'index' . ++$count;
29
30
        $replace  = "<?php for (\$$index = 0; \$$index < $match[3]; \$$index++) { ?>";
31
        $replace .= $match[9];
32
        $replace .= "<?php } ?>";
33
34
        return $replace;
35
    }
36
}
37