1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Input for parser |
4
|
|
|
*/ |
5
|
|
|
namespace xKerman\Restricted; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parser Input |
9
|
|
|
*/ |
10
|
|
|
class Source |
11
|
|
|
{ |
12
|
|
|
/** @var string $str given string to deserialize */ |
13
|
|
|
private $str; |
14
|
|
|
|
15
|
|
|
/** @var int $length given string length */ |
16
|
|
|
private $length; |
17
|
|
|
|
18
|
|
|
/** @var int $current current position of parser */ |
19
|
|
|
private $current; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* constructor |
23
|
|
|
* |
24
|
|
|
* @param string $str parser input |
25
|
|
|
* @throws \InvalidArgumentException |
26
|
|
|
*/ |
27
|
110 |
|
public function __construct($str) |
28
|
|
|
{ |
29
|
110 |
|
if (!is_string($str)) { |
30
|
1 |
|
throw new \InvalidArgumentException('expected string, but got: ' . gettype($str)); |
31
|
|
|
} |
32
|
109 |
|
$this->str = $str; |
33
|
109 |
|
$this->length = strlen($str); |
34
|
109 |
|
$this->current = 0; |
35
|
109 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* throw error with currnt position |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
* @throws UnserializeFailedException |
42
|
|
|
*/ |
43
|
67 |
|
public function triggerError() |
44
|
|
|
{ |
45
|
67 |
|
$bytes = strlen($this->str); |
46
|
67 |
|
throw new UnserializeFailedException("unserialize(): Error at offset {$this->current} of {$bytes} bytes"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* consume given string if it is as expected |
51
|
|
|
* |
52
|
|
|
* @param string $expected expected string |
53
|
|
|
* @param integer $length length of $expected |
54
|
|
|
* @return void |
55
|
|
|
* @throws UnserializeFailedException |
56
|
|
|
*/ |
57
|
26 |
|
public function consume($expected, $length) |
58
|
|
|
{ |
59
|
26 |
|
if (strpos($this->str, $expected, $this->current) !== $this->current) { |
60
|
5 |
|
return $this->triggerError(); |
61
|
|
|
} |
62
|
21 |
|
$this->current += $length; |
63
|
21 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* read givin length substring |
67
|
|
|
* |
68
|
|
|
* @param integer $length length to read |
69
|
|
|
* @return string |
70
|
|
|
* @throws UnserializeFailedException |
71
|
|
|
*/ |
72
|
25 |
|
public function read($length) |
73
|
|
|
{ |
74
|
25 |
|
if ($length < 0) { |
75
|
1 |
|
return $this->triggerError(); |
76
|
|
|
} |
77
|
24 |
|
if ($this->current + $length > $this->length) { |
78
|
1 |
|
return $this->triggerError(); |
79
|
|
|
} |
80
|
|
|
|
81
|
23 |
|
$this->current += $length; |
82
|
23 |
|
return substr($this->str, $this->current - $length, $length); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* return matching string for given regexp |
87
|
|
|
* |
88
|
|
|
* @param string $regexp Regular Expression for expected substring |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
109 |
|
public function match($regexp) |
92
|
|
|
{ |
93
|
109 |
|
if (!preg_match($regexp, $this->str, $matches, 0, $this->current)) { |
94
|
62 |
|
return $this->triggerError(); |
95
|
|
|
} |
96
|
|
|
|
97
|
52 |
|
$this->current += strlen($matches[0]); |
98
|
52 |
|
array_shift($matches); |
99
|
52 |
|
return $matches; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|