FieldModifierParser::getConstValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\database\struct;
3
4
use function call_user_func_array;
5
use function is_array;
6
use function method_exists;
7
use function token_get_all;
8
9
/**
10
 * 字段修饰符解析
11
 */
12
class FieldModifierParser
13
{
14
    /**
15
     * 判断位置
16
     *
17
     * @var int
18
     */
19
    protected $pos;
20
21
    /**
22
     * 长度
23
     *
24
     * @var int
25
     */
26
    protected $length;
27
28
29
    /**
30
     * Tokens
31
     *
32
     * @var array
33
     */
34
    protected $tokens;
35
36
    /**
37
     * 修饰符
38
     *
39
     * @var array
40
     */
41
    protected $modifier;
42
43
    /**
44
     * 解析
45
     *
46
     * @param string $modifier
47
     * @return $this
48
     */
49
    public function parse(string $modifier)
50
    {
51
        $this->tokens = token_get_all('<?php '. $modifier);
52
        $this->length = count($this->tokens);
53
        $this->pos = 1;
54
        $this->modifier = [];
55
        while ($this->isNotEnd()) {
56
            $token = $this->tokens[$this->pos];
57
            if (is_array($token)) {
58
                if ($token[0] === T_STRING || $token[0] === T_DEFAULT) {
59
                    $name = $token[1];
60
                    $parameter = $this->getParameter();
61
                    $this->modifier[] = [$name, $parameter];
62
                }
63
            }
64
            $this->pos++;
65
        }
66
        return $this;
67
    }
68
69
    /**
70
     * 修改
71
     *
72
     * @param Field $field
73
     * @return void
74
     */
75
    public function modify(Field $field)
76
    {
77
        foreach ($this->modifier as $value) {
78
            list($name, $parameter) = $value;
79
            if (method_exists($field, $name)) {
80
                call_user_func_array([$field, $name], $parameter);
81
            }
82
        }
83
    }
84
85
    public function getModifier()
86
    {
87
        return $this->modifier;
88
    }
89
90
    protected function isNotEnd()
91
    {
92
        return $this->pos < $this->length;
93
    }
94
    
95
    protected function getParameter()
96
    {
97
        $this->skipWhiteComment();
98
        $paramter = [];
99
        if ($this->skipAfterLeftBorder()) {
100
            do {
101
                $token = $this->tokens[$this->pos];
102
                $paramter[] = $this->getValue($token);
103
                $this->pos++;
104
            } while ($this->nextIsNotEnd() === true);
105
        }
106
        return $paramter;
107
    }
108
109
    protected function skipWhiteComment()
110
    {
111
        for ($i = $this->pos + 1; $i < $this->length ; $i++) {
112
            if (is_array($this->tokens[$i])) {
113
                if (in_array($this->tokens[$i][0], [T_COMMENT, T_DOC_COMMENT, T_WHITESPACE])) {
114
                    $this->pos++;
115
                }
116
            } else {
117
                return;
118
            }
119
        }
120
    }
121
122
    protected function skipAfterLeftBorder()
123
    {
124
        $this->skipWhiteComment();
125
        if ($this->pos + 1 < $this->length && $this->tokens[$this->pos + 1][0] === '(') {
126
            $this->pos += 2;
127
            if ($this->pos < $this->length && $this->tokens[$this->pos] !== ')') {
128
                return true;
129
            } else {
130
                $this->pos++;
131
            }
132
        }
133
        return false;
134
    }
135
136
137
    protected function getValue($token)
138
    {
139
        if ($token[0] === T_STRING) {
140
            return $this->getConstValue($token[1]);
141
        } elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
142
            return $this->getStringValue($token[1]);
143
        } elseif ($token[0] === T_LNUMBER) {
144
            return $this->getNumberValue($token[1]);
145
        }
146
        return null;
147
    }
148
149
    protected function getConstValue(string $value)
150
    {
151
        $value = strtolower($value);
152
        if ($value === 'true') {
153
            return true;
154
        }
155
        if ($value === 'false') {
156
            return false;
157
        }
158
        return null;
159
    }
160
161
    protected function getStringValue(string $value)
162
    {
163
        $value = trim($value, '\'"');
164
        return stripslashes($value);
165
    }
166
167
168
    protected function getNumberValue(string $value)
169
    {
170
        return intval($value);
171
    }
172
173
    protected function nextIsNotEnd()
174
    {
175
        $this->skipWhiteComment();
176
        $token = $this->tokens[$this->pos];
177
        if (is_array($token)) {
178
            return false;
179
        }
180
        if ($token === ')') {
181
            $this->pos++;
182
            $this->skipWhiteComment();
183
            return false;
184
        }
185
        if ($token === ',') {
186
            $this->pos++;
187
            $this->skipWhiteComment();
188
            return true;
189
        }
190
        return false;
191
    }
192
}
193