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

EscapedStringParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 15 3
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