Passed
Push — master ( 133874...778cd6 )
by Edson
01:18
created

Evaluate   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
eloc 36
dl 0
loc 98
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A eval0() 0 7 1
A eval3() 0 11 2
A eval2() 0 25 3
A eval4() 0 7 1
A style() 0 3 1
A script() 0 3 1
A assets() 0 3 1
A __construct() 0 7 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->eval0();
12
        $this->eval1();
13
        $this->eval2();
14
        $this->eval3();
15
        $this->eval4();
16
    }
17
18
    private function eval0(): void
19
    {
20
        $search = "/\@{{(\s?)+([\w\.\(\)]+)(\s?)+}}/is";
21
22
        Tag::match($search, function($var) {
23
24
            Tag::replace("@@ $var @@");
25
        });
26
    }
27
28
    private function eval4(): void
29
    {
30
        $search = "/\@\@(\s?)+([\w\.\(\)]+)(\s?)+\@\@/is";
31
32
        Tag::match($search, function($var) {
33
34
            Tag::replace("{{ $var }}");
35
        });
36
    }
37
38
    private function eval1(): void
39
    {
40
        $search = "/{{(\s?)+([\w\.\(\)]+)(\s?)+}}/is";
41
42
        Tag::match($search, function($var) {
43
44
            $var = str_replace('.', '->', $var);
45
46
            Tag::replace("<?= \$$var ?>");
47
        });
48
    }
49
50
    private function eval2(): void
51
    {
52
        $search = "/{{(\s?)+([\w\.\(\)]+)(\s?)+\|(\s?)+([\w| ]+)(\s?)+}}/is";
53
54
        Tag::match($search, function($var, $filter) {
55
56
            $var = str_replace('.', '->', $var);
57
58
            $filters = explode('|', $filter);
59
60
            $res = "<?= ";
61
            
62
            foreach ($filters as $filter) {
0 ignored issues
show
introduced by
$filter is overwriting one of the parameters of this function.
Loading history...
63
                $res .= "$filter(";
64
            }
65
66
            $res .= "$$var";
67
68
            foreach ($filters as $filter) {
0 ignored issues
show
introduced by
$filter is overwriting one of the parameters of this function.
Loading history...
69
                $res .= ")";
70
            }
71
72
            $res .= " ?>";
73
74
            Tag::replace($res);
75
        });
76
    }
77
78
    private function eval3(): void
79
    {
80
        $search = "/{{(\s?)+([\w]+)(\s?)+\((.*?)\)(\s?)+}}/is";
81
82
        Tag::match($search, function($func, $params) {
83
84
            if (method_exists(__CLASS__, $func)) {
85
                $func = __CLASS__ . "::$func";
86
            }
87
88
            Tag::replace("<?= $func($params) ?>");
89
        });
90
    }
91
92
    public static function style(String $asset): String
93
    {
94
        return '<link rel="stylesheet" href="'.Tpl::getUrl().Tpl::getAssets().$asset.'">';
95
    }
96
97
    public static function script(String $asset): String
98
    {
99
        return '<script src="'.Tpl::getUrl().Tpl::getAssets().$asset.'"></script>';
100
    }
101
102
    public static function assets(String $asset): String
103
    {
104
        return Tpl::getUrl() . Tpl::getAssets() . $asset;
105
    }
106
}
107