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

RegexpSubstringParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php
2
/**
3
 * parser for regexp match and then substring
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser that first process regexp match, and then substring the result
9
 */
10
class RegexpSubstringParser implements ParserInterface
11
{
12
    /** @var string $regexp regexp for matching */
13
    private $regexp;
14
15
    /** @var integer $start start position of substring */
16
    private $start;
17
18
    /** @var integer $length length of substring (see PHP `substr` manual) */
19
    private $length;
20
21
    /**
22
     * constructor
23
     *
24
     * @param string  $regexp regexp for matching
25
     * @param integer $start  start position of substring
26
     * @param integer $length length of substring
27
     */
28 15
    public function __construct($regexp, $start, $length)
29
    {
30 15
        $this->regexp = $regexp;
31 15
        $this->start = $start;
32 15
        $this->length = $length;
33 15
    }
34
35
    /**
36
     * parse given `$source` and return substring result
37
     *
38
     * @param Source $source parser input
39
     * @return array
40
     * @throws UnserializeFailedException
41
     */
42 15
    public function parse(Source $source)
43
    {
44 15
        $result = $source->match($this->regexp);
45 11
        return array(substr($result, $this->start, $this->length), $source);
46
    }
47
}
48