Passed
Push — master ( 86f0a4...06b540 )
by Edson
01:31
created

Evaluate::assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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().Tpl::getAssets().$asset.'">';
73
    }
74
75
    public static function script(String $asset): String
76
    {
77
        return '<script src="'.Tpl::getUrl().Tpl::getAssets().$asset.'"></script>';
78
    }
79
80
    public static function assets(String $asset): String
81
    {
82
        return Tpl::getUrl() . Tpl::getAssets() . $asset;
83
    }
84
}
85