Passed
Push — master ( f5acbf...d4cc60 )
by X
34s
created

ExpressionParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 53
ccs 21
cts 21
cp 1
rs 10
c 1
b 0
f 1
wmc 4
lcom 1
cbo 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A parse() 0 6 1
A createParser() 0 8 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 $parsers parser list to use */
13
    private $parsers;
14
15
    /**
16
     * constructor
17
     */
18 39
    public function __construct()
19
    {
20 39
        $this->parsers = array(
21 39
            'N' => new NullParser(),
22 39
            'b' => new TypeConvertParser(
23 39
                new RegexpSubstringParser('/\Gb:[01];/', 2, 1),
24 39
                new BooleanConverter()
25 39
            ),
26 39
            'i' => new IntegerParser(),
27 39
            'd' => new FloatParser(),
28 39
            's' => new StringParser(),
29 39
            'a' => new ArrayParser($this),
30
        );
31 39
    }
32
33
    /**
34
     * parse given `$source` as PHP serialized value
35
     *
36
     * @param Source $source parser input
37
     * @return array
38
     * @throws UnserializeFailedException
39
     */
40 39
    public function parse(Source $source)
41
    {
42 39
        $parser = $this->createParser($source);
43 35
        list($result, $source) = $parser->parse($source);
44 32
        return array($result, $source);
45
    }
46
47
    /**
48
     * create parser for given input
49
     *
50
     * @param Source $source input
51
     * @return ParserInterface
52
     * @throws UnserializeFailedException
53
     */
54 39
    private function createParser($source)
55
    {
56 39
        $char = $source->peek();
57 39
        if (isset($this->parsers[$char])) {
58 35
            return $this->parsers[$char];
59
        }
60 4
        return $source->triggerError();
61
    }
62
}
63