1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* parser for PHP serialized array |
4
|
|
|
*/ |
5
|
|
|
namespace xKerman\Restricted; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parser for PHP serialiezed array |
9
|
|
|
*/ |
10
|
|
|
class ArrayParser implements ParserInterface |
11
|
|
|
{ |
12
|
|
|
/** @var ParserInterface $expressionParser parser for unserialize expression */ |
13
|
|
|
private $expressionParser; |
14
|
|
|
|
15
|
|
|
/** @var ParserInterface $integerParser parser for unserialized integer */ |
16
|
|
|
private $integerParser; |
17
|
|
|
|
18
|
|
|
/** @var ParserInterface $stringParser parser for unserialized string */ |
19
|
|
|
private $stringParser; |
20
|
|
|
|
21
|
|
|
/** @var ParserInterface $parser internal parser */ |
22
|
|
|
private $parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* constructor |
26
|
|
|
* |
27
|
|
|
* @param ParserInterface $expressionParser parser for unserialize expression |
28
|
|
|
* @param ParserInterface $integerParser parser for unserialized integer |
29
|
|
|
* @param ParserInterface $stringParser parser for unserialized string |
30
|
|
|
*/ |
31
|
50 |
|
public function __construct( |
32
|
|
|
ParserInterface $expressionParser, |
33
|
|
|
ParserInterface $integerParser, |
34
|
|
|
ParserInterface $stringParser |
35
|
|
|
) { |
36
|
50 |
|
$this->expressionParser = $expressionParser; |
37
|
50 |
|
$this->integerParser = $integerParser; |
38
|
50 |
|
$this->stringParser = $stringParser; |
39
|
50 |
|
$this->parser = new TypeConvertParser( |
40
|
50 |
|
new RegexpParser('/\Ga:([+]?[0-9]+):{/'), |
41
|
50 |
|
new IntegerConverter() |
42
|
50 |
|
); |
43
|
50 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* parse given `$source` as PHP serialized array |
47
|
|
|
* |
48
|
|
|
* @param Source $source parser input |
49
|
|
|
* @return array |
50
|
|
|
* @throws UnserializeFailedException |
51
|
|
|
*/ |
52
|
16 |
|
public function parse(Source $source) |
53
|
|
|
{ |
54
|
16 |
|
list($length, $source) = $this->parser->parse($source); |
55
|
|
|
|
56
|
8 |
|
$result = array(); |
57
|
8 |
|
for ($i = 0; $i < $length; ++$i) { |
58
|
5 |
|
list($key, $source) = $this->parseKey($source); |
59
|
4 |
|
list($value, $source) = $this->expressionParser->parse($source); |
60
|
3 |
|
$result[$key] = $value; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
5 |
|
$source->consume('}'); |
64
|
3 |
|
return array($result, $source); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* parse given `$source` as array key (s.t. integer|string) |
69
|
|
|
* |
70
|
|
|
* @param Source $source input |
71
|
|
|
* @return array |
72
|
|
|
* @throws UnserializeFailedException |
73
|
|
|
*/ |
74
|
5 |
|
private function parseKey($source) |
75
|
|
|
{ |
76
|
5 |
|
switch ($source->peek()) { |
77
|
5 |
|
case 'i': |
78
|
4 |
|
return $this->integerParser->parse($source); |
79
|
3 |
|
case 's': |
80
|
1 |
|
return $this->stringParser->parse($source); |
81
|
2 |
|
default: |
82
|
2 |
|
return $source->triggerError(); |
83
|
2 |
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|