Passed
Push — master ( 7bd07e...7e5ad5 )
by 世昌
02:03
created

Command::parseEndif()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\component\template;
3
4
use nebula\component\template\CommandInterface;
5
6
class Command implements EchoValueInterface, CommandInterface
7
{
8
    use EchoValueTrait;
9
    /**
10
     * 配置
11
     *
12
     * @var array
13
     */
14
    protected $config;
15
16
    public function has(string $name):bool
17
    {
18
        return method_exists($this, 'parse'.ucfirst($name));
19
    }
20
    
21
    public function parse(string $name, string $content):string
22
    {
23
        if ($this->has($name)) {
24
            return $this->{'parse'.ucfirst($name)}($this->parseEchoValue($content));
25
        }
26
        return '';
27
    }
28
29
    public function setConfig(array $config)
30
    {
31
        $this->config = $config;
32
        return $this;
33
    }
34
    
35
    public function parseSet($exp)
36
    {
37
        return "<?php \$this->set{$exp}; ?>";
38
    }
39
40
    public function parseStartInsert($exp)
41
    {
42
        preg_match('/\((.+)\)/', $exp, $v);
43
        $name=trim(str_replace('\'', '-', trim($v[1], '"\'')));
44
        return '<?php $this->insert(\''.$name.'\',function () { ?>';
45
    }
46
    
47
    public function parseInsert($exp)
48
    {
49
        preg_match('/\((.+)\)/', $exp, $v);
50
        $name=str_replace('\'', '-', trim($v[1], '"\''));
51
        return "<?php \$this->exec('{$name}'); ?>";
52
    }
53
    
54
    public function parseEndInsert()
55
    {
56
        return '<?php });?>';
57
    }
58
59
60
    protected function parseIf($exp)
61
    {
62
        return "<?php if {$exp}: ?>";
63
    }
64
65
    protected function parseEndif()
66
    {
67
        return '<?php endif; ?>';
68
    }
69
    
70
    protected function parseElse()
71
    {
72
        return '<?php else: ?>';
73
    }
74
75
    protected function parseElseif($exp)
76
    {
77
        return "<?php elseif {$exp}: ?>";
78
    }
79
80
    protected function parseFor($expression)
81
    {
82
        return "<?php for{$expression}: ?>";
83
    }
84
85
    protected function parseEndfor()
86
    {
87
        return '<?php endfor; ?>';
88
    }
89
90
91
    protected function parseForeach($exp)
92
    {
93
        return "<?php foreach{$exp}: ?>";
94
    }
95
96
    protected function parseEndforeach()
97
    {
98
        return '<?php endforeach; ?>';
99
    }
100
101
    protected function parseWhile($exp)
102
    {
103
        return "<?php while{$exp}: ?>";
104
    }
105
106
    protected function parseEndwhile()
107
    {
108
        return '<?php endwhile; ?>';
109
    }
110
111
    protected function parseInclude($content)
112
    {
113
        return '<?php $this->include(\nebula\component\template\TemplateLoader::credibleLoad'.$content.'); ?>';
114
    }
115
116
    protected function parseExtends($content)
117
    {
118
        return '<?php $this->extends(\nebula\component\template\TemplateLoader::credibleLoad'.$content.'); ?>';
119
    }
120
}
121