Completed
Pull Request — master (#14)
by Samuel
03:28
created

DtStartTransformer::transform()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 9
Ratio 34.62 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 9
loc 26
ccs 12
cts 12
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Recurrence\RruleTransformer;
4
5
/**
6
 * Class DtStartTransformer
7
 * @package Recurrence\RruleTransformer
8
 */
9
class DtStartTransformer extends AbstractRruleTransformer implements TransformerInterface
10
{
11
    const RRULE_PARAMETER = 'DTSTART';
12
13
    /**
14
     * @param string $rRule
15
     * @return \DateTime
16
     */
17
    public function transform($rRule)
18
    {
19 1
        if (preg_match('/'.$this::RRULE_PARAMETER.';TZID=([a-zA-Z_-]+[\/[a-zA-Z_+\-0-9]+]?):([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
20
            try {
21 1
                return \DateTime::createFromFormat('Ymd\THis', $matches[2], new \DateTimeZone($matches[1]));
22 1
            } catch (\Exception $e) {
23 1
                throw new \InvalidArgumentException(sprintf('Invalid RRULE [%s] option : [%s] with timezone [%s]', $this::RRULE_PARAMETER, $matches[2], $matches[1]));
24
            }
25
        }
26
27 1 View Code Duplication
        if (preg_match('/'.$this::RRULE_PARAMETER.'=([0-9]{8}T[0-9]{6}Z)/', $rRule, $matches)) {
28 1
            return \DateTime::createFromFormat('Ymd\THisZ', $matches[1], new \DateTimeZone('UTC'));
29
        }
30
31 1 View Code Duplication
        if (preg_match('/'.$this::RRULE_PARAMETER.'=([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
32 1
            return \DateTime::createFromFormat('Ymd\THis', $matches[1]);
33
        }
34
35 1 View Code Duplication
        if (preg_match('/'.$this::RRULE_PARAMETER.'=([0-9]{8})/', $rRule, $matches)) {
36 1
            return \DateTime::createFromFormat('Ymd', $matches[1]);
37
        }
38
39 1
        $this->throwExceptionOnInvalidParameter($rRule, $this::RRULE_PARAMETER);
40
41 1
        return null;
42
    }
43
}
44