-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.go
312 lines (256 loc) · 6.51 KB
/
form.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"bytes"
"fmt"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/visitor/printer"
"strings"
)
type Form struct {
prefix string
code string
root *ast.Root
}
func NewForm() *Form {
return &Form{
prefix: "form",
}
}
func (f *Form) SetPrefix(prefix string) {
f.prefix = prefix
}
func (f *Form) SetCode(code string) {
f.code = code
f.root = nil
}
func (f *Form) IsSnippetCode() bool {
code := strings.TrimSpace(f.code)
return !(strings.HasPrefix(code, "<?") || strings.HasPrefix(code, "<?php"))
}
func (f *Form) Prefix() string {
if strings.HasPrefix(f.prefix, "$") {
return f.prefix
}
return fmt.Sprintf("$%s", f.prefix)
}
func (f *Form) Root() (*ast.Root, error) {
if f.root != nil {
return f.root, nil
}
code := f.code
// 如果是代码片段
if f.IsSnippetCode() {
code = fmt.Sprintf("<?php %s", f.code)
}
root, err := ParseCode([]byte(code))
if err != nil {
return nil, err
}
f.root = root
return f.root, nil
}
// 根据输入的 fields 生成代码
func (f *Form) Stringify(fields []Field) (string, error) {
root, err := f.Root()
if err != nil {
return "", err
}
stmtList := root.Stmts
for _, stmt := range stmtList {
// 如果是表达式
if expr, ok := stmt.(*ast.StmtExpression); ok {
if exprAssign, ok := expr.Expr.(*ast.ExprAssign); ok {
if f.IsExprAssignMatched(exprAssign) {
f.UpdateExprAssign(exprAssign, fields)
}
}
}
}
buf := &bytes.Buffer{}
pr := printer.NewPrinter(buf)
root.Accept(pr)
res := buf.String()
if f.IsSnippetCode() {
res = strings.Replace(res, "<?php ", "", 1)
}
return res, nil
}
// 根据 fields 更新 ExprAssign
func (f *Form) UpdateExprAssign(expr *ast.ExprAssign, fields []Field) {
for _, field := range fields {
// 变量名匹配
if field.Name == f.GetExprVariableName(expr.Var) {
switch e := expr.Expr.(type) {
case *ast.ExprArray:
for _, item := range e.Items {
key, val := f.GetItemExpr(item)
if f.GetExprValue(key) == "value" {
f.SetExprValue(val, field.Value)
return
}
}
default:
f.SetExprValue(e, field.Value)
}
}
}
}
// 表达式的变量是否匹配前缀
func (f *Form) IsExprAssignMatched(expr *ast.ExprAssign) bool {
return strings.HasPrefix(f.GetExprVariableName(expr.Var), f.Prefix())
}
// 表达式的变量是否一样
func (f *Form) IsExprAssignEqualed(expr *ast.ExprAssign, name string) bool {
return f.GetExprVariableName(expr.Var) == name
}
// 根据 ExprAssign 获取 Input
func (f *Form) ParseExprAssign(expr *ast.ExprAssign) *Field {
field := NewField()
field.SetName(f.GetExprVariableName(expr.Var))
switch e := expr.Expr.(type) {
case *ast.ExprArray:
for _, item := range e.Items {
field.Set(f.GetItemValues(item))
}
default:
field.SetValue(f.GetExprValue(e))
}
return field
}
func (f *Form) ParseCode(code string) ([]Field, error) {
f.SetCode(code)
return f.Parse()
}
// 解析代码获取 fields
func (f *Form) Parse() ([]Field, error) {
root, err := f.Root()
if err != nil {
return nil, err
}
stmtList := root.Stmts
var fields []Field
for _, stmt := range stmtList {
// 如果是表达式
if expr, ok := stmt.(*ast.StmtExpression); ok {
if exprAssign, ok := expr.Expr.(*ast.ExprAssign); ok {
if f.IsExprAssignMatched(exprAssign) {
field := f.ParseExprAssign(exprAssign)
if !field.IsEmpty() {
fields = append(fields, *field)
}
}
}
}
}
return fields, nil
}
// 获取 ExprArrayItem 的 kv 表达式
func (f *Form) GetItemExpr(item ast.Vertex) (ast.Vertex, ast.Vertex) {
expr := item.(*ast.ExprArrayItem)
return expr.Key, expr.Val
}
// 获取 ExprArray 中的数组数据
func (f *Form) GetExprArrayValue(expr *ast.ExprArray) interface{} {
// 没有 key 的情况,返回数组
if keyCheck, _ := f.GetItemExpr(expr.Items[0]); keyCheck == nil {
var res []interface{}
for _, item := range expr.Items {
_, val := f.GetItemValues(item)
if val != "" {
res = append(res, val)
}
}
return res
}
// 返回对象
res := make(map[string]interface{})
for _, item := range expr.Items {
key, val := f.GetItemValues(item)
if key != "" {
res[key] = val
}
}
return res
}
// 获取 ExprArrayItem 中的 kv 值
func (f *Form) GetItemValues(item ast.Vertex) (string, interface{}) {
keyExpr, valExpr := f.GetItemExpr(item)
key := f.GetExprValue(keyExpr)
if arrayExpr, ok := valExpr.(*ast.ExprArray); ok {
return key, f.GetExprArrayValue(arrayExpr)
}
return key, f.GetExprValue(valExpr)
}
// 获取表达式变量名称
func (f *Form) GetExprVariableName(expr ast.Vertex) string {
if exprVariable, ok := expr.(*ast.ExprVariable); ok {
return string(exprVariable.Name.(*ast.Identifier).Value)
}
return ""
}
// 获取变量的值
func (f *Form) GetExprVariableValue(expr *ast.ExprVariable) string {
root, err := f.Root()
if err != nil {
return ""
}
stmtList := root.Stmts
for i := len(stmtList) - 1; i >= 0; i-- {
stmt := stmtList[i]
// 如果是表达式
if exprStmt, ok := stmt.(*ast.StmtExpression); ok &&
stmt.GetPosition().StartLine < expr.GetPosition().StartLine {
if exprAssign, ok := exprStmt.Expr.(*ast.ExprAssign); ok {
if exprAssign.Expr.GetPosition() == expr.GetPosition() {
return ""
}
if f.IsExprAssignEqualed(exprAssign, f.GetExprVariableName(expr)) {
return f.GetExprValue(exprAssign.Expr)
}
}
}
}
return ""
}
// 获取表达式的值
func (f *Form) GetExprValue(expr ast.Vertex) string {
var res []byte
switch v := expr.(type) {
case *ast.ExprVariable:
return f.GetExprVariableValue(v)
case *ast.ScalarString:
res = v.Value[1 : len(v.Value)-1]
case *ast.ScalarLnumber:
res = v.Value
case *ast.ScalarDnumber:
res = v.Value
case *ast.ExprConstFetch:
res = v.Const.(*ast.Name).Parts[0].(*ast.NamePart).Value
case *ast.ExprClosure, *ast.ExprArrowFunction:
buf := &bytes.Buffer{}
pr := printer.NewPrinter(buf)
v.Accept(pr)
res = buf.Bytes()
res = bytes.TrimLeft(res, "<?php ")
}
return string(res)
}
// 设置表达式的值
func (f Form) SetExprValue(expr ast.Vertex, value string) {
data := []byte(value)
switch v := expr.(type) {
case *ast.ScalarString:
v.Value = append([]byte{'\''}, data...)
v.Value = append(v.Value, '\'')
v.StringTkn.Value = v.Value
case *ast.ScalarLnumber:
v.Value = data
v.NumberTkn.Value = data
case *ast.ScalarDnumber:
v.Value = data
v.NumberTkn.Value = data
case *ast.ExprConstFetch:
v.Const.(*ast.Name).Parts[0].(*ast.NamePart).Value = data
v.Const.(*ast.Name).Parts[0].(*ast.NamePart).StringTkn.Value = data
}
}