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

FloatParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
dl 0
loc 35
ccs 12
cts 12
cp 1
rs 10
c 8
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A parse() 0 10 2
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