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

DtStartTransformer::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Recurrence\Rrule\Transformer;
4
5
use Recurrence\Rrule\Extractor\DtStartExtractor;
6
use Recurrence\Model\Exception\InvalidRruleException;
7
8
/**
9
 * Class DtStartTransformer
10
 * @package Recurrence\Rrule\Transformer
11
 */
12
class DtStartTransformer implements RruleTransformerInterface
13
{
14
    /**
15
     * @param array $values
16
     * @return \DateTime
17
     * @throws InvalidRruleException
18
     */
19
    public function transform(array $values)
20
    {
21
        $this->validate($values);
22
23
        if (count($values) == 2 && substr($values[1], -1) == 'Z') {
24
            $dtStart = \DateTime::createFromFormat('Ymd\THis\Z', $values[0], new \DateTimeZone('UTC'));
25
        } else if (count($values) == 2) {
26
            $dtStart = \DateTime::createFromFormat('Ymd\THis', $values[0]);
27
        } else {
28
            $dtStart = \DateTime::createFromFormat('Ymd', $values[0]);
29
        }
30
31
        if ($dtStart === false) {
32
            throw new InvalidRruleException(DtStartExtractor::RRULE_PARAMETER, (string) $values[0]);
33
        }
34
35
        return $dtStart;
36
    }
37
38
    /**
39
     * @param array $values
40
     * @throws InvalidRruleException
41
     */
42
    protected function validate(array $values)
43
    {
44
        if (!isset($values[0])) {
45
            throw new InvalidRruleException(DtStartExtractor::RRULE_PARAMETER);
46
        }
47
    }
48
}
49