Completed
Push — v0.7 ( 194ebe...12ee9b )
by Nate
01:59
created

DateTimeTypeAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 75
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 24 3
A write() 0 10 2
A __construct() 0 4 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\TypeAdapter;
10
11
use DateTimeInterface;
12
use Tebru\Gson\Exception\JsonSyntaxException;
13
use Tebru\Gson\JsonReadable;
14
use Tebru\Gson\JsonToken;
15
use Tebru\Gson\JsonWritable;
16
use Tebru\Gson\TypeAdapter;
17
use Tebru\PhpType\TypeToken;
18
19
/**
20
 * Class DateTimeTypeAdapter
21
 *
22
 * @author Nate Brunette <[email protected]>
23
 */
24
class DateTimeTypeAdapter extends TypeAdapter
25
{
26
    /**
27
     * @var TypeToken
28
     */
29
    private $type;
30
31
    /**
32
     * @var string
33
     */
34
    private $format;
35
36
    /**
37
     * Constructor
38
     *
39
     * @param TypeToken $type
40
     * @param string $format
41
     */
42 7
    public function __construct(TypeToken $type, string $format)
43
    {
44 7
        $this->type = $type;
45 7
        $this->format = $format;
46 7
    }
47
48
    /**
49
     * Read the next value, convert it to its type and return it
50
     *
51
     * @param JsonReadable $reader
52
     * @return DateTimeInterface|null
53
     * @throws \Tebru\Gson\Exception\JsonSyntaxException If the DateTime could not be created from format
54
     */
55 4
    public function read(JsonReadable $reader): ?DateTimeInterface
56
    {
57 4
        if ($reader->peek() === JsonToken::NULL) {
58 1
            $reader->nextNull();
59 1
            return null;
60
        }
61
62 3
        $formattedDateTime = $reader->nextString();
63
64 3
        $class = $this->type->getRawType();
65
66
        /** @noinspection PhpUndefinedMethodInspection */
67 3
        $dateTime = $class::createFromFormat($this->format, $formattedDateTime);
68
69 3
        if ($dateTime !== false) {
70 2
            return $dateTime;
71
        }
72
73 1
        throw new JsonSyntaxException(\sprintf(
74 1
            'Could not create "%s" class from "%s" using format "%s" at "%s"',
75 1
            $class,
76 1
            $formattedDateTime,
77 1
            $this->format,
78 1
            $reader->getPath()
79
        ));
80
    }
81
82
    /**
83
     * Write the value to the writer for the type
84
     *
85
     * @param JsonWritable $writer
86
     * @param DateTimeInterface $value
87
     * @return void
88
     */
89 3
    public function write(JsonWritable $writer, $value): void
90
    {
91 3
        if (null === $value) {
92 1
            $writer->writeNull();
93
94 1
            return;
95
        }
96
97 2
        $dateTime = $value->format($this->format);
98 2
        $writer->writeString($dateTime);
99 2
    }
100
}
101