|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* parser for serialized expression |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace xKerman\Restricted; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Parser for serialized PHP values |
|
9
|
|
|
*/ |
|
10
|
|
|
class ExpressionParser implements ParserInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array $parsers parser list to use */ |
|
13
|
|
|
private $parsers; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* constructor |
|
17
|
|
|
*/ |
|
18
|
39 |
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
39 |
|
$this->parsers = array( |
|
21
|
39 |
|
'N' => new NullParser(), |
|
22
|
39 |
|
'b' => new TypeConvertParser( |
|
23
|
39 |
|
new RegexpSubstringParser('/\Gb:[01];/', 2, 1), |
|
24
|
39 |
|
new BooleanConverter() |
|
25
|
39 |
|
), |
|
26
|
39 |
|
'i' => new IntegerParser(), |
|
27
|
39 |
|
'd' => new FloatParser(), |
|
28
|
39 |
|
's' => new StringParser(), |
|
29
|
39 |
|
'a' => new ArrayParser($this), |
|
30
|
|
|
); |
|
31
|
39 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* parse given `$source` as PHP serialized value |
|
35
|
|
|
* |
|
36
|
|
|
* @param Source $source parser input |
|
37
|
|
|
* @return array |
|
38
|
|
|
* @throws UnserializeFailedException |
|
39
|
|
|
*/ |
|
40
|
39 |
|
public function parse(Source $source) |
|
41
|
|
|
{ |
|
42
|
39 |
|
$parser = $this->createParser($source); |
|
43
|
35 |
|
list($result, $source) = $parser->parse($source); |
|
44
|
32 |
|
return array($result, $source); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* create parser for given input |
|
49
|
|
|
* |
|
50
|
|
|
* @param Source $source input |
|
51
|
|
|
* @return ParserInterface |
|
52
|
|
|
* @throws UnserializeFailedException |
|
53
|
|
|
*/ |
|
54
|
39 |
|
private function createParser($source) |
|
55
|
|
|
{ |
|
56
|
39 |
|
$char = $source->peek(); |
|
57
|
39 |
|
if (isset($this->parsers[$char])) { |
|
58
|
35 |
|
return $this->parsers[$char]; |
|
59
|
|
|
} |
|
60
|
4 |
|
return $source->triggerError(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|