Passed
Pull Request — master (#9)
by Vincent
05:08 queued 01:37
created

DateTimeNormalizer::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bdf\Serializer\Normalizer;
4
5
use Bdf\Serializer\Context\DenormalizationContext;
6
use Bdf\Serializer\Context\NormalizationContext;
7
use Bdf\Serializer\Type\Type;
8
use DateTime;
9
use DateTimeImmutable;
10
use DateTimeInterface;
11
use DateTimeZone;
12
13
/**
14
 * DateTimeNormalizer
15
 *
16
 * @implements NormalizerInterface<\DateTime|\DateTimeImmutable>
17
 */
18
class DateTimeNormalizer implements NormalizerInterface
19
{
20
    /**
21
     * The default date format used for normalization
22
     *
23
     * @var string
24
     */
25
    private $defaultFormat;
26
27
    /**
28
     * DateTimeNormalizer constructor.
29
     *
30
     * @param string $defaultFormat   The default date format used by normalization
31
     */
32 178
    public function __construct(string $defaultFormat = DateTime::ATOM)
33
    {
34 178
        $this->defaultFormat = $defaultFormat;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 16
    public function normalize($data, NormalizationContext $context)
41
    {
42 16
        $timezone = $context->option(NormalizationContext::TIMEZONE);
43
44 16
        if ($timezone !== null) {
45
            // Dont change date instance if it is not immutable
46 6
            if (!$data instanceof DateTimeImmutable) {
47 6
                $data = clone $data;
48
            }
49
50 6
            $data = $data->setTimezone(new DateTimeZone($timezone));
51
        }
52
53 16
        return $data->format($context->option(NormalizationContext::DATETIME_FORMAT, $this->defaultFormat));
0 ignored issues
show
Bug introduced by
$this->defaultFormat of type string is incompatible with the type Bdf\Serializer\Context\T expected by parameter $default of Bdf\Serializer\Context\Context::option(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        return $data->format($context->option(NormalizationContext::DATETIME_FORMAT, /** @scrutinizer ignore-type */ $this->defaultFormat));
Loading history...
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @psalm-suppress InvalidNullableReturnType
60
     */
61 22
    public function denormalize($data, Type $type, DenormalizationContext $context)
62
    {
63
        // @fixme does this case really occurs ? No other normalizer handle this case
64 22
        if ($data === null) {
65
            /** @psalm-suppress NullableReturnStatement */
66
            return null;
67
        }
68
69 22
        $className = $type->name();
70 22
        $format = $context->option(DenormalizationContext::DATETIME_FORMAT);
71 22
        $timezoneHint = $context->option(DenormalizationContext::TIMEZONE_HINT);
72 22
        $timezone = $context->option(DenormalizationContext::TIMEZONE);
73
74 22
        if ($timezoneHint !== null) {
75 4
            $timezoneHint = new DateTimeZone($timezoneHint);
76
        }
77
78 22
        if ($format !== null) {
79 4
            $date = $className::createFromFormat($format, $data, $timezoneHint);
80
        } else {
81
            /** @psalm-suppress UnsafeInstantiation */
82 20
            $date = new $className($data, $timezoneHint);
83
        }
84
85 22
        if ($timezone !== null) {
86
            /** @psalm-suppress PossiblyFalseReference */
87 6
            $date = $date->setTimezone(new DateTimeZone($timezone));
88
        }
89
90 22
        return $date;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 150
    public function supports(string $className): bool
97
    {
98 150
        return is_subclass_of($className, DateTimeInterface::class);
99
    }
100
}
101