DtStartTransformer::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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