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

RegexpSubstringParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 38
ccs 8
cts 8
cp 1
rs 10
wmc 2
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A parse() 0 5 1
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