Tag   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 147
rs 10
c 2
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setClose() 0 5 1
A getClose() 0 3 1
A getName() 0 3 1
A compile() 0 3 1
A setConfig() 0 19 6
A setName() 0 5 1
A __construct() 0 6 1
A setOpen() 0 5 1
A getOpen() 0 3 1
1
<?php
2
3
namespace suda\application\template\compiler;
4
5
use Exception;
6
use function str_replace;
7
8
class Tag implements EchoValueInterface
9
{
10
    use EchoValueTrait;
11
12
    protected $content;
13
14
    /**
15
     * 配置信息
16
     *
17
     * @var array
18
     */
19
    protected $config;
20
    /**
21
     * 标签名
22
     *
23
     * @var string
24
     */
25
    protected $name;
26
    /**
27
     * 开标签
28
     *
29
     * @var string
30
     */
31
    protected $open;
32
    /**
33
     * 闭标签
34
     *
35
     * @var string
36
     */
37
    protected $close;
38
39
    public function __construct(string $name, string $open, string $close, string $content)
40
    {
41
        $this->content = $content;
42
        $this->name = $name;
43
        $this->open = $open;
44
        $this->close = $close;
45
    }
46
47
    /**
48
     * @param string $content
49
     * @return string
50
     * @throws Exception
51
     */
52
    public function compile(string $content): string
53
    {
54
        return $this->parseEchoValue(str_replace('$code', $content, $this->content));
55
    }
56
57
    /**
58
     * Get 开标签
59
     *
60
     * @return  string
61
     */
62
    public function getOpen()
63
    {
64
        return $this->open;
65
    }
66
67
    /**
68
     * Get 闭标签
69
     *
70
     * @return  string
71
     */
72
    public function getClose()
73
    {
74
        return $this->close;
75
    }
76
77
    /**
78
     * Set 闭标签
79
     *
80
     * @param string $close 闭标签
81
     *
82
     * @return  self
83
     */
84
    public function setClose(string $close)
85
    {
86
        $this->close = $close;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Set 开标签
93
     *
94
     * @param string $open 开标签
95
     *
96
     * @return  self
97
     */
98
    public function setOpen(string $open)
99
    {
100
        $this->open = $open;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Get 标签名
107
     *
108
     * @return  string
109
     */
110
    public function getName()
111
    {
112
        return $this->name;
113
    }
114
115
    /**
116
     * Set 标签名
117
     *
118
     * @param string $name 标签名
119
     *
120
     * @return  self
121
     */
122
    public function setName(string $name)
123
    {
124
        $this->name = $name;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Set 配置信息
131
     *
132
     * @param array $config 配置信息
133
     *
134
     * @return  self
135
     */
136
    public function setConfig(array $config)
137
    {
138
        $this->config = $config;
139
        if (is_numeric(key($config))) {
140
            if (array_key_exists(0, $config)) {
141
                $this->setOpen($config[0]);
142
            }
143
            if (array_key_exists(1, $config)) {
144
                $this->setClose($config[1]);
145
            }
146
        } else {
147
            if (array_key_exists('open', $config)) {
148
                $this->setOpen($config['open']);
149
            }
150
            if (array_key_exists('close', $config)) {
151
                $this->setClose($config['close']);
152
            }
153
        }
154
        return $this;
155
    }
156
}
157