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
c 0
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 4
nop 2
crap 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