-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestorePrint.php
More file actions
255 lines (228 loc) · 5.03 KB
/
RestorePrint.php
File metadata and controls
255 lines (228 loc) · 5.03 KB
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
<?php
/**
* 将 php print_r 方法执行结果还原为原始数组
*
* @author fdipzone
* @DateTime 2024-05-23 17:53:28
*
*/
class RestorePrint
{
/**
* 保存还原的原始数组
*
* @var array
*/
private $res = [];
/**
* 保存标记字典对应的处理方法
*
* @var array
*/
private $dict = [];
/**
* 缓冲,用于拼接字符串
*
* @var string
*/
private $buffer = '';
/**
* 标记
*
* @var string
*/
private $key = '';
/**
* 堆栈,用于处理边界符匹配
*
* @var array
*/
private $stack = [];
/**
* print_r 执行结果
*
* @var string
*/
private $tmp_doc;
/**
* print_r 执行结果长度
*
* @var int
*/
private $tmp_len;
/**
* 初始化
*
* @author fdipzone
* @DateTime 2024-05-23 17:54:14
*
*/
public function __construct()
{
$this->stack[] = & $this->res;
}
/**
* 使用 __call 魔术方法截获未定义方法的调用
* 如调用了未定义方法,抛出异常
*
* @author fdipzone
* @DateTime 2024-05-23 18:04:41
*
* @param string $method 方法
* @param array $param 参数
* @return void
*/
public function __call(string $method, array $param):void
{
throw new \Exception(sprintf('%s not defined method: %s param:%s', $this->buffer, $method, implode(',', $param)));
}
/**
* 设置字典关键字的处理方法
* dict 使用了前缀树(Trie)数据结构存储
*
* @author fdipzone
* @DateTime 2024-05-23 18:10:14
*
* @param mixed $word 字典关键字(字符串或数组)
* @param string $value 处理方法
* @return RestorePrint
*/
public function set($word, string $value=''):RestorePrint
{
if(is_array($word))
{
foreach($word as $k=>$v)
{
$this->set($k, $v);
}
}
$p = & $this->dict;
foreach(str_split($word) as $ch)
{
if(!isset($p[$ch]))
{
$p[$ch] = [];
}
$p = & $p[$ch];
}
$p['val'] = $value;
return $this;
}
/**
* 解析 php print_r 方法执行结果,还原为原始数组
*
* @author fdipzone
* @DateTime 2024-05-23 18:26:17
*
* @param string $str print_r 执行后的结果
* @return array
*/
public function parse(string $str):array
{
$this->tmp_doc = $str;
$this->tmp_len = strlen($str);
$i = 0;
while($i < $this->tmp_len)
{
$t = $this->find($this->dict, $i);
if($t)
{
$i = $t;
$this->buffer = '';
}
else
{
$this->buffer .= $this->tmp_doc[$i++];
}
}
return $this->res;
}
/**
* 查找字典,调用设置的处理方法
* 返回已处理的下标
*
* @author fdipzone
* @DateTime 2024-05-23 19:09:32
*
* @param mixed $p 标记字典
* @param int $i 下标
* @return int
*/
private function find(&$p, int $i):int
{
if($i >= $this->tmp_len)
{
return $i;
}
$t = 0;
$n = $this->tmp_doc[$i];
if(isset($p[$n]))
{
$t = $this->find($p[$n], $i+1);
}
if($t)
{
return $t;
}
if(isset($p['val']))
{
$arr = explode(',', $p['val']);
call_user_func_array(array($this, array_shift($arr)), $arr);
return $i;
}
return $t;
}
/**
* 分组处理
*
* @author fdipzone
* @DateTime 2024-05-23 19:13:19
*
* @return void
*/
private function group():void
{
if(!$this->key)
{
return;
}
$cnt = count($this->stack) - 1;
$this->stack[$cnt][$this->key] = [];
$this->stack[] = & $this->stack[$cnt][$this->key];
$this->key = '';
}
/**
* 边界符处理
*
* @author fdipzone
* @DateTime 2024-05-23 19:13:28
*
* @param string $c 边界符
* @return void
*/
private function brackets(string $c):void
{
$cnt = count($this->stack) - 1;
switch($c)
{
case ')':
if($this->key)
{
$this->stack[$cnt][$this->key] = trim($this->buffer);
}
$this->key = '';
array_pop($this->stack);
break;
case '[':
if($this->key)
{
$this->stack[$cnt][$this->key] = trim($this->buffer);
}
break;
case ']':
$this->key = $this->buffer;
break;
}
$this->buffer = '';
}
}