Passed
Push — master ( 93a267...f5acbf )
by X
32s
created

ExpressionParser::createParser()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 18
nc 7
nop 1
dl 0
loc 22
ccs 20
cts 20
cp 1
crap 7
rs 6.9811
c 1
b 0
f 1
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
    /**
13
     * parse given `$source` as PHP serialized value
14
     *
15
     * @param Source $source parser input
16
     * @return array
17
     * @throws UnserializeFailedException
18
     */
19 39
    public function parse(Source $source)
20
    {
21 39
        $parser = $this->createParser($source);
22 35
        list($result, $source) = $parser->parse($source);
23 32
        return array($result, $source);
24
    }
25
26
    /**
27
     * create parser for given input
28
     *
29
     * @param Source $source input
30
     * @return ParserInterface
31
     * @throws UnserializeFailedException
32
     */
33 39
    private function createParser($source)
34
    {
35 39
        switch ($source->peek()) {
36 39
            case 'N':
37 2
                return new NullParser();
38 38
            case 'b':
39 5
                return new TypeConvertParser(
40 5
                    new RegexpSubstringParser('/\Gb:[01];/', 2, 1),
41 5
                    new BooleanConverter()
42 5
                );
43 33
            case 'i':
44 6
                return new IntegerParser();
45 28
            case 'd':
46 15
                return new FloatParser();
47 13
            case 's':
48 8
                return new StringParser();
49 7
            case 'a':
50 3
                return new ArrayParser();
51 4
            default:
52 4
                return $source->triggerError();
53 4
        }
54
    }
55
}
56