Passed
Push — master ( bb8381...e42fb8 )
by Edson
02:29
created

Evaluate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 27
dl 0
loc 71
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A eval3() 0 11 2
A eval2() 0 25 3
A style() 0 3 1
A script() 0 3 1
A __construct() 0 5 1
A eval1() 0 9 1
1
<?php
2
3
namespace EdsonOnildo\Tpl\Tag;
4
5
use EdsonOnildo\Tpl\Tpl;
6
7
class Evaluate extends Tag
8
{
9
    public function __construct()
10
    {
11
        $this->eval1();
12
        $this->eval2();
13
        $this->eval3();
14
    }
15
16
    private function eval1(): void
17
    {
18
        $search = "/{{(\s?)+([\w\.\(\)]+)(\s?)+}}/is";
19
20
        Tag::match($search, function($var) {
21
22
            $var = str_replace('.', '->', $var);
23
24
            Tag::replace("<?= \$$var ?>");
25
        });
26
    }
27
28
    private function eval2(): void
29
    {
30
        $search = "/{{(\s?)+([\w\.\(\)]+)(\s?)+\|(\s?)+([\w| ]+)(\s?)+}}/is";
31
32
        Tag::match($search, function($var, $filter) {
33
34
            $var = str_replace('.', '->', $var);
35
36
            $filters = explode('|', $filter);
37
38
            $res = "<?= ";
39
            
40
            foreach ($filters as $filter) {
0 ignored issues
show
introduced by
$filter is overwriting one of the parameters of this function.
Loading history...
41
                $res .= "$filter(";
42
            }
43
44
            $res .= "$$var";
45
46
            foreach ($filters as $filter) {
0 ignored issues
show
introduced by
$filter is overwriting one of the parameters of this function.
Loading history...
47
                $res .= ")";
48
            }
49
50
            $res .= " ?>";
51
52
            Tag::replace($res);
53
        });
54
    }
55
56
    private function eval3(): void
57
    {
58
        $search = "/{{(\s?)+([\w]+)(\s?)+\((.*?)\)(\s?)+}}/is";
59
60
        Tag::match($search, function($func, $params) {
61
62
            if (method_exists(__CLASS__, $func)) {
63
                $func = __CLASS__ . "::$func";
64
            }
65
66
            Tag::replace("<?= $func($params) ?>");
67
        });
68
    }
69
70
    public static function style(String $asset): String
71
    {
72
        return '<link rel="stylesheet" href="'.Tpl::getUrl().'assets/css/'.$asset.'">';
73
    }
74
75
    public static function script(String $asset): String
76
    {
77
        return '<script src="'.Tpl::getUrl().'assets/js/'.$asset.'"></script>';
78
    }
79
}
80