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

DateTimeTypeAdapter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
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