AbstractDates   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 9
dl 0
loc 27
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 18 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