ExpressionParser   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 14
dl 0
loc 45
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A parse() 0 17 2
1
<?php
2
/**
3
 * parser for serialized expression
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser for serialized PHP values
9
 */
10
class ExpressionParser implements ParserInterface
11
{
12
    /** @var array $handlers handlers list to use */
13
    private $handlers;
14
15
    /**
16
     * constructor
17
     */
18 109
    public function __construct()
19
    {
20 109
        $this->handlers = array(
21 109
            'N' => new NullHandler(),
22 109
            'b' => new BooleanHandler(),
23 109
            'i' => new IntegerHandler(),
24 109
            'd' => new FloatHandler(),
25 109
            's' => new StringHandler(),
26 109
            'S' => new EscapedStringHandler(),
27 109
            'a' => new ArrayHandler($this),
28
        );
29 109
    }
30
31
    /**
32
     * parse given `$source` as PHP serialized value
33
     *
34
     * @param Source $source parser input
35
     * @return array
36
     * @throws UnserializeFailedException
37
     */
38 109
    public function parse(Source $source)
39
    {
40 109
        $matches = $source->match('/\G(?|
41
            (s):([0-9]+):"
42
            |(i):([+-]?[0-9]+);
43
            |(a):([0-9]+):{
44
            |(d):((?:
45
                [+-]?(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?)
46
                |-?INF
47
                |NAN);
48
            |(b):([01]);
49
            |(N);
50
            |(S):([0-9]+):"
51
        )/x');
52 51
        $tag = $matches[0];
53 51
        $args = isset($matches[1]) ? $matches[1] : null;
54 51
        return $this->handlers[$tag]->handle($source, $args);
55
    }
56
}
57