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

DateTimeLocalTransformer::output()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 1
nop 1
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 DateTimeLocalTransformer implements TransformerInterface
13
{
14
    public function __construct(
15
        private string $dateFormat
16
    ) {}
17
18
    public function input(mixed $data): string
19
    {
20
        if ($data instanceof DateTimeInterface) {
21
            return $data->format($this->dateFormat);
22
        }
23
24
        return $data !== null ? $data : '';
25
    }
26
27
    public function output(string $value): ?DateTimeInterface
28
    {
29
        /** @@todo local timezone? */
30
        $date = new DateTime($value, new DateTimeZone('UTC'));
31
32
        return $date ?: null;
0 ignored issues
show
introduced by
$date is of type DateTime, thus it always evaluated to true.
Loading history...
33
    }
34
}
35