DateTimeToTimestampTransformer::transformToHttp()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.2222
cc 6
nc 10
nop 2
crap 6
1
<?php
2
3
namespace Bdf\Form\Leaf\Date\Transformer;
4
5
use Attribute;
6
use Bdf\Form\ElementInterface;
7
use Bdf\Form\Leaf\Date\DateTimeElement;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use DateTime;
10
use DateTimeInterface;
11
use DateTimeZone;
12
use InvalidArgumentException;
13
14
/**
15
 * Transform a DateTime instance from a form element to a timestamp to a model
16
 */
17
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
18
final class DateTimeToTimestampTransformer implements TransformerInterface
19
{
20
    /**
21
     * @var class-string<DateTimeInterface>|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DateTimeInterface>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DateTimeInterface>|null.
Loading history...
22
     */
23
    private $className;
24
25
    /**
26
     * @var DateTimeZone|null
27
     */
28
    private $timezone;
29
30
31
    /**
32
     * DateTimeToTimestampTransformer constructor.
33
     *
34
     * @param class-string<DateTimeInterface>|null $className The date time class name to use when retrieving value from model. If null, will use the class defined in the input element
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DateTimeInterface>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DateTimeInterface>|null.
Loading history...
35
     * @param DateTimeZone|null $timezone The timezone to set when retrieving value from model. If null will use the element's timezone
36
     */
37 5
    public function __construct(?string $className = null, ?DateTimeZone $timezone = null)
38
    {
39 5
        $this->className = $className;
40 5
        $this->timezone = $timezone;
41 5
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @psalm-suppress UndefinedInterfaceMethod
47
     * @psalm-suppress PossiblyUndefinedMethod
48
     */
49 5
    public function transformToHttp($value, ElementInterface $input): ?DateTimeInterface
50
    {
51 5
        if ($value === null) {
52 1
            return null;
53
        }
54
55 5
        if (!is_numeric($value)) {
56 1
            throw new InvalidArgumentException('Expected a numeric.');
57
        }
58
59 4
        $className = $this->className ?? ($input instanceof DateTimeElement ? $input->dateTimeClassName() : DateTime::class);
60 4
        $timezone = $this->timezone ?? ($input instanceof DateTimeElement ? $input->timezone() : null);
61
62
        /** @var DateTimeInterface $dateTime */
63 4
        $dateTime = new $className;
64
65 4
        if ($timezone) {
66 1
            $dateTime = $dateTime->setTimezone($timezone);
67
        }
68
69 4
        return $dateTime->setTimestamp($value);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function transformFromHttp($value, ElementInterface $input): ?int
76
    {
77 1
        if (!$value instanceof DateTimeInterface) {
78 1
            return null;
79
        }
80
81 1
        return $value->getTimestamp();
82
    }
83
}
84