Completed
Pull Request — master (#38)
by X
02:20
created

EscapedStringParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

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
    const CLOSE_STRING_LENGTH = 2;
13
14
    /**
15
     * parse given `$source` as escaped string
16
     *
17
     * @param Source $source parser input
18
     * @return array
19
     * @throws UnserializeFailedException
20
     */
21 22
    public function parse(Source $source)
22
    {
23 22
        $length = intval($source->match('/\GS:([+]?[0-9]+):"/'), 10);
24 13
        $result = array();
25 13
        for ($i = 0; $i < $length; ++$i) {
26 12
            $char = $source->read(1);
27 12
            if ($char !== '\\') {
28 3
                $result[] = $char;
29 3
                continue;
30
            }
31 9
            $result[] = chr(base_convert(($source->match('/\G[0-9a-fA-F]{2}/')), 16, 10));
32 6
        }
33 10
        $source->consume('";', self::CLOSE_STRING_LENGTH);
34 9
        return array(implode('', $result), $source);
35
    }
36
}
37