Completed
Pull Request — master (#18)
by Samuel
02:01
created

AbstractExtractor::extract()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Recurrence\Rrule\Extractor;
4
5
use Recurrence\Model\Exception\InvalidRruleException;
6
7
/**
8
 * Class AbstractExtractor
9
 * @package Recurrence\Rrule\Model
10
 */
11
abstract class AbstractExtractor implements RruleExtractorInterface
12
{
13
    /**
14
     * @param string $rRule
15
     * @throws InvalidRruleException
16
     * @return array|null
17
     */
18
    public function extract($rRule)
19
    {
20 1
        if (preg_match(sprintf('/%s=%s/', $this::RRULE_PARAMETER, $this::RRULE_PATTERN), $rRule, $matches)) {
21 1
            return array_slice($matches, 1);
22
        }
23
24 1
        $this->throwExceptionOnInvalidParameter($rRule, $this::RRULE_PARAMETER);
25
26 1
        return null;
27
    }
28
29
    /**
30
     * @param string $rRule
31
     * @param string $ruleKey
32
     * @throws InvalidRruleException
33
     * @return void
34
     */
35
    public function throwExceptionOnInvalidParameter($rRule, $ruleKey)
36
    {
37 1
        if ((preg_match(sprintf('/%s=([\d\w]+)/', $ruleKey), $rRule, $matches) === 1)) {
38 1
            throw new InvalidRruleException($ruleKey, ((count($matches) > 0)? implode(', ', array_slice($matches, 1)) : ''));
39
        }
40 1
    }
41
}
42