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

DateTimeLocalTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 6
c 1
b 0
f 0
dl 0
loc 21
rs 10

3 Methods

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