Passed
Push — master ( b8029f...b45126 )
by 世昌
02:31
created

Command::parseCall()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace suda\application\template\compiler;
4
5
/**
6
 * Class Command
7
 * @package suda\application\template\compiler
8
 */
9
class Command implements EchoValueInterface, CommandInterface
10
{
11
    use EchoValueTrait;
12
    /**
13
     * 配置
14
     *
15
     * @var array
16
     */
17
    protected $config;
18
19
    /**
20
     * @param string $name
21
     * @return bool
22
     */
23
    public function has(string $name): bool
24
    {
25
        return method_exists($this, 'parse' . ucfirst($name));
26
    }
27
28
    /**
29
     * @param string $name
30
     * @param string $content
31
     * @return string
32
     */
33
    public function parse(string $name, string $content): string
34
    {
35
        if ($this->has($name)) {
36
            return $this->{'parse' . ucfirst($name)}($this->parseEchoValue($content));
37
        }
38
        return '';
39
    }
40
41
    /**
42
     * @param array $config
43
     * @return $this|mixed
44
     */
45
    public function setConfig(array $config)
46
    {
47
        $this->config = $config;
48
        return $this;
49
    }
50
51
    /**
52
     * @param $exp
53
     * @return string
54
     */
55
    public function parseSet($exp)
56
    {
57
        return "<?php \$this->set{$exp}; ?>";
58
    }
59
60
    /**
61
     * @param $exp
62
     * @return string
63
     */
64
    public function parseStartInsert($exp)
65
    {
66
        preg_match('/\((.+)\)/', $exp, $v);
67
        $name = trim(str_replace('\'', '-', trim($v[1], '"\'')));
68
        return '<?php $this->insert(\'' . $name . '\',function () { ?>';
69
    }
70
71
    /**
72
     * @param $exp
73
     * @return string
74
     */
75
    public function parseInsert($exp)
76
    {
77
        preg_match('/\((.+)\)/', $exp, $v);
78
        $name = str_replace('\'', '-', trim($v[1], '"\''));
79
        return "<?php \$this->exec('{$name}'); ?>";
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function parseEndInsert()
86
    {
87
        return '<?php });?>';
88
    }
89
90
91
    /**
92
     * @param $exp
93
     * @return string
94
     */
95
    protected function parseIf($exp)
96
    {
97
        return "<?php if {$exp}: ?>";
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    protected function parseEndif()
104
    {
105
        return '<?php endif; ?>';
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    protected function parseElse()
112
    {
113
        return '<?php else: ?>';
114
    }
115
116
    /**
117
     * @param $exp
118
     * @return string
119
     */
120
    protected function parseElseif($exp)
121
    {
122
        return "<?php elseif {$exp}: ?>";
123
    }
124
125
    /**
126
     * @param $expression
127
     * @return string
128
     */
129
    protected function parseFor($expression)
130
    {
131
        return "<?php for{$expression}: ?>";
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    protected function parseEndfor()
138
    {
139
        return '<?php endfor; ?>';
140
    }
141
142
143
    /**
144
     * @param $exp
145
     * @return string
146
     */
147
    protected function parseForeach($exp)
148
    {
149
        return "<?php foreach{$exp}: ?>";
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    protected function parseEndforeach()
156
    {
157
        return '<?php endforeach; ?>';
158
    }
159
160
    /**
161
     * @param $exp
162
     * @return string
163
     */
164
    protected function parseWhile($exp)
165
    {
166
        return "<?php while{$exp}: ?>";
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    protected function parseEndwhile()
173
    {
174
        return '<?php endwhile; ?>';
175
    }
176
177
    /**
178
     * @param $content
179
     * @return string
180
     */
181
    protected function parseInclude($content)
182
    {
183
        return '<?php $this->include' . $content . '; ?>';
184
    }
185
186
    /**
187
     * @param $content
188
     * @return string
189
     */
190
    protected function parseExtend($content)
191
    {
192
        return '<?php $this->extend' . $content . '; ?>';
193
    }
194
195
    /**
196
     * @param $content
197
     * @return string
198
     */
199
    protected function parseStatic($content)
200
    {
201
        $content = strlen(trim($content)) === 0 ? '()' : $content;
202
        return '<?php echo $this->getStaticPrefix' . $content . '; ?>';
203
    }
204
205
    /**
206
     * @param $exp
207
     * @return string
208
     */
209
    public function parseCall($exp)
210
    {
211
        return "<?php \$this->call{$exp}; ?>";
212
    }
213
}
214