Passed
Push — master ( 6b957b...39821f )
by X
02:25
created

EscapedStringParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 15
ccs 12
cts 12
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * parser for escaped string
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser for escaped string
9
 */
10
class EscapedStringParser implements ParserInterface
11
{
12
    /** @var integer */
13
    const CLOSE_STRING_LENGTH = 2;
14
15
    /**
16
     * parse given `$source` as escaped string
17
     *
18
     * @param Source $source parser input
19
     * @return array
20
     * @throws UnserializeFailedException
21
     */
22 22
    public function parse(Source $source)
23
    {
24 22
        $length = intval($source->match('/\GS:([+]?[0-9]+):"/'), 10);
25 13
        $result = array();
26 13
        for ($i = 0; $i < $length; ++$i) {
27 12
            $char = $source->read(1);
28 12
            if ($char !== '\\') {
29 3
                $result[] = $char;
30 3
                continue;
31
            }
32 9
            $result[] = chr(base_convert(($source->match('/\G[0-9a-fA-F]{2}/')), 16, 10));
33 6
        }
34 10
        $source->consume('";', self::CLOSE_STRING_LENGTH);
35 9
        return array(implode('', $result), $source);
36
    }
37
}
38