Passed
Push — master ( 64d8f0...ea07e6 )
by X
29s
created

FloatParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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
    /** @var array $mapping parser result mapping */
15
    private $mapping;
16
17
    /**
18
     * constructor
19
     */
20 51
    public function __construct()
21
    {
22 51
        $this->mapping = array(
23 51
            'INF'  => INF,
24 51
            '-INF' => -INF,
25 51
            'NAN'  => NAN,
26
        );
27 51
    }
28
29
    /**
30
     * parse given `$source` as PHP serialized float number
31
     *
32
     * @param Source $source parser input
33
     * @return array
34
     * @throws UnserializeFailedException
35
     */
36 29
    public function parse(Source $source)
37
    {
38 29
        $value = $source->match(
39
            '/\Gd:((?:[+-]?(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?)|-?INF|NAN);/'
40 29
        );
41 15
        if (array_key_exists($value, $this->mapping)) {
42 3
            return array($this->mapping[$value], $source);
43
        }
44 12
        return array(floatval($value), $source);
45
    }
46
}
47