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

DateTimeTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
eloc 8
c 3
b 0
f 1
dl 0
loc 24
rs 10
ccs 10
cts 11
cp 0.9091

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A output() 0 9 3
A input() 0 7 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