|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* parser for PHP serialized float number |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace xKerman\Restricted; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Parser for PHP serialized float number |
|
9
|
|
|
* |
|
10
|
|
|
* format: http://php.net/manual/en/language.types.float.php |
|
11
|
|
|
*/ |
|
12
|
|
|
class FloatParser implements ParserInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* parse given `$source` as PHP serialized float number |
|
16
|
|
|
* |
|
17
|
|
|
* @param Source $source parser input |
|
18
|
|
|
* @return array |
|
19
|
|
|
* @throws UnserializeFailedException |
|
20
|
|
|
*/ |
|
21
|
29 |
|
public function parse(Source $source) |
|
22
|
|
|
{ |
|
23
|
29 |
|
$source->consume('d:'); |
|
24
|
|
|
|
|
25
|
26 |
|
if ($source->peek() === 'N') { |
|
26
|
1 |
|
return $this->parseNan($source); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
25 |
|
$parser = new OptionalSignParser(); |
|
30
|
25 |
|
list($sign, $source) = $parser->parse($source); |
|
31
|
|
|
|
|
32
|
25 |
|
if ($source->peek() === 'I') { |
|
33
|
3 |
|
return $this->parseInf($source, $sign); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
22 |
|
$num = $source->match('/\G((?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?);/'); |
|
37
|
12 |
|
return array(floatval($sign . $num), $source); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* parse given `$source` as NAN |
|
42
|
|
|
* |
|
43
|
|
|
* @param Source $source input |
|
44
|
|
|
* @return array |
|
45
|
|
|
* @throws UnserializeFailedException |
|
46
|
|
|
*/ |
|
47
|
1 |
|
private function parseNan($source) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$source->consume('NAN;'); |
|
50
|
1 |
|
return array(NAN, $source); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* parse given `$source` as INF / -INF |
|
55
|
|
|
* |
|
56
|
|
|
* @param Source $source input |
|
57
|
|
|
* @param string $sign '-', '+' or '' |
|
58
|
|
|
* @return array |
|
59
|
|
|
* @throws UnserializeFailedException |
|
60
|
|
|
*/ |
|
61
|
3 |
|
private function parseInf($source, $sign) |
|
62
|
|
|
{ |
|
63
|
3 |
|
if (!in_array($sign, array('', '-'), true)) { |
|
64
|
1 |
|
return $source->triggerError(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$source->consume('INF;'); |
|
68
|
|
|
|
|
69
|
2 |
|
if ($sign === '-') { |
|
70
|
1 |
|
return array(-INF, $source); |
|
71
|
|
|
} |
|
72
|
1 |
|
return array(INF, $source); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|