Completed
Pull Request — master (#18)
by Samuel
03:55
created

AbstractExtractor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 10 2
A throwExceptionOnInvalidParameter() 0 6 3
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
        if (preg_match(sprintf('/%s=%s/', $this::RRULE_PARAMETER, $this::RRULE_PATTERN), $rRule, $matches)) {
21
            return array_slice($matches, 1);
22
        }
23
24
        $this->throwExceptionOnInvalidParameter($rRule, $this::RRULE_PARAMETER);
25
26
        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
        if ((preg_match(sprintf('/%s=([\d\w]+)/', $ruleKey), $rRule, $matches) === 1)) {
38
            throw new InvalidRruleException($ruleKey, ((count($matches) > 0)? implode(', ', array_slice($matches, 1)) : ''));
39
        }
40
    }
41
}
42