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
c 0
b 0
f 0
dl 0
loc 27
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 Riesenia\Pohoda\Common\OptionsResolver\Normalizers;
4
5
/**
6
 * Abstract class for normalization of dates
7
 *
8
 * Just set the final format in abstract
9
 */
10
abstract class AbstractDates extends AbstractNormalizer
11
{
12
    /**
13
     * The final format
14
     *
15
     * @return string
16
     */
17
    abstract protected function getFormat(): string;
18
19 63
    public function normalize(mixed $options, mixed $value): string
20
    {
21
        // param is used for nullable
22 63
        if ($this->nullable && empty($value)) {
23 8
            return '';
24
        }
25
26 63
        if ($value instanceof \DateTimeInterface) {
27 13
            return $value->format($this->getFormat());
28
        }
29
30 55
        $time = \strtotime(\strval($value));
31
32 55
        if (!$time) {
33 1
            throw new \DomainException('Not a valid date: ' . $value);
34
        }
35
36 54
        return \date($this->getFormat(), $time);
37
    }
38
}
39