AbstractDates::normalize()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 4
nop 2
crap 5
1
<?php
2
3
namespace kalanis\Pohoda\Common\OptionsResolver\Normalizers;
4
5
use kalanis\PohodaException;
6
7
/**
8
 * Abstract class for normalization of dates
9
 *
10
 * Just set the final format in abstract
11
 */
12
abstract class AbstractDates extends AbstractNormalizer
13
{
14
    /**
15
     * The final format
16
     *
17
     * @return string
18
     */
19
    abstract protected function getFormat(): string;
20
21 62
    public function normalize(mixed $options, mixed $value): string
22
    {
23
        // param is used for nullable
24 62
        if ($this->nullable && empty($value)) {
25 6
            return '';
26
        }
27
28 61
        if ($value instanceof \DateTimeInterface) {
29 11
            return $value->format($this->getFormat());
30
        }
31
32 53
        $time = \strtotime(\strval($value));
33
34 53
        if (!$time) {
35 1
            throw new PohodaException('Not a valid date: ' . $value);
36
        }
37
38 52
        return \date($this->getFormat(), $time);
39
    }
40
}
41