Passed
Push — master ( ca4e2d...533f2e )
by Derek Stephen
13:15 queued 11:23
created

DateTimeTransformer::input()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 3
c 3
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Form\Field\Transformer;
6
7
use DateTime;
8
use DateTimeInterface;
9
use DateTimeZone;
10
use Del\Form\Field\TransformerInterface;
11
12
class DateTimeTransformer implements TransformerInterface
13
{
14 3
    public function __construct(
15
        private string $dateFormat
16 3
    ) {}
17
18 2
    public function input(mixed $data): string
19
    {
20 2
        if ($data instanceof DateTimeInterface) {
21 1
            return $data->format($this->dateFormat);
22
        }
23
24 1
        return $data !== null ? $data : '';
25
    }
26
27 2
    public function output(string $value): ?DateTimeInterface
28
    {
29 2
        $date =  DateTime::createFromFormat($this->dateFormat, $value, new DateTimeZone('UTC'));
30
31 2
        if (!$date) {
0 ignored issues
show
introduced by
$date is of type DateTime, thus it always evaluated to true.
Loading history...
32
            $date = new DateTime($value, new DateTimeZone('UTC'));
33
        }
34
35 2
        return $date ?: null;
0 ignored issues
show
introduced by
$date is of type DateTime, thus it always evaluated to true.
Loading history...
36
    }
37
}
38