Passed
Branch master (3daac1)
by Vincent
07:53
created

DateTimeElement::toPhp()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 8.8333
cc 7
nc 12
nop 1
crap 7
1
<?php
2
3
namespace Bdf\Form\Leaf\Date;
4
5
use Bdf\Form\Choice\ChoiceInterface;
6
use Bdf\Form\Leaf\LeafElement;
7
use Bdf\Form\Transformer\TransformerInterface;
8
use Bdf\Form\Validator\ValueValidatorInterface;
9
use DateTime;
10
use DateTimeInterface;
11
use DateTimeZone;
12
use InvalidArgumentException;
13
14
/**
15
 * Handle DateTime form element
16
 * The element use a formatted string as http value, and can return any implementation of DateTimeInterface
17
 *
18
 * @method DateTimeInterface value()
19
 */
20
final class DateTimeElement extends LeafElement
21
{
22
    /**
23
     * @var class-string<DateTimeInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DateTimeInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DateTimeInterface>.
Loading history...
24
     */
25
    private $className;
26
27
    /**
28
     * @var string
29
     */
30
    private $format;
31
32
    /**
33
     * @var DateTimeZone|null
34
     */
35
    private $timezone;
36
37
    /**
38
     * DateTimeType constructor.
39
     *
40
     * @param ValueValidatorInterface|null $validator
41
     * @param TransformerInterface|null $transformer
42
     * @param ChoiceInterface|null $choices
43
     * @param class-string<DateTimeInterface> $className The date time class name to use
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DateTimeInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DateTimeInterface>.
Loading history...
44
     * @param string $format The time format string
45
     * @param DateTimeZone|null $timezone Timezone to use. Use null to not define a timezone
46
     */
47 46
    public function __construct(?ValueValidatorInterface $validator = null, ?TransformerInterface $transformer = null, ?ChoiceInterface $choices = null, string $className = DateTime::class, string $format = DateTime::ATOM, ?DateTimeZone $timezone = null)
48
    {
49 46
        parent::__construct($validator, $transformer, $choices);
50
51 46
        $this->className = $className;
52 46
        $this->format = $format;
53 46
        $this->timezone = $timezone;
54 46
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 32
    protected function toPhp($httpValue): ?DateTimeInterface
60
    {
61 32
        if ($httpValue === null) {
62 3
            return null;
63
        }
64
65
        switch (true) {
66 29
            case $httpValue instanceof $this->className:
67 1
                $dateTime = $httpValue; // Clone ?
68 1
                break;
69
70 28
            case $httpValue instanceof DateTimeInterface:
71 1
                $httpValue = $httpValue->format($this->format);
72
                // No break
73
74
            default:
75 28
                if (!method_exists($this->className, 'createFromFormat')) {
76 1
                    throw new \LogicException('Invalid DateTime class name "'.$this->className.'" : method createFromFormat() is not found.');
77
                }
78
79 27
                $dateTime = ($this->className)::createFromFormat($this->format, $httpValue, $this->timezone);
80
        }
81
82 28
        if ($dateTime === false) {
83 1
            throw new InvalidArgumentException('Invalid date format');
84
        }
85
86 27
        if ($this->timezone !== null) {
87 2
            $dateTime->setTimezone($this->timezone);
88
        }
89
90 27
        return $dateTime;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 10
    protected function toHttp($phpValue)
97
    {
98 10
        if (!$phpValue instanceof DateTimeInterface) {
99 2
            return null;
100
        }
101
102 8
        return $phpValue->format($this->format);
103
    }
104
}
105