Completed
Push — master ( a293a3...292fca )
by X
02:23
created

EscapedStringHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 16 3
1
<?php
2
/**
3
 * handler for escaped string
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Handler for escaped string
9
 */
10
class EscapedStringHandler implements HandlerInterface
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
     * @param string $args   string length
20
     * @return array
21
     * @throws UnserializeFailedException
22
     */
23 13
    public function handle(Source $source, $args)
24
    {
25 13
        $length = intval($args, 10);
26 13
        $result = array();
27 13
        for ($i = 0; $i < $length; ++$i) {
28 12
            $char = $source->read(1);
29 12
            if ($char !== '\\') {
30 3
                $result[] = $char;
31 3
                continue;
32
            }
33 9
            $hex = $source->match('/\G([0-9a-fA-F]{2})/');
34 6
            $result[] = chr(base_convert($hex[0], 16, 10));
35 6
        }
36 10
        $source->consume('";', self::CLOSE_STRING_LENGTH);
37 9
        return array(implode('', $result), $source);
38
    }
39
}
40