Completed
Pull Request — master (#44)
by Nate
04:08 queued 02:15
created

IntegerTypeAdapter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 3
cts 3
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;
10
11
/**
12
 * Class IntegerTypeAdapter
13
 *
14
 * @author Nate Brunette <[email protected]>
15
 */
16
class IntegerTypeAdapter extends TypeAdapter
17
{
18
    /**
19
     * Read the next value, convert it to its type and return it
20
     *
21
     * @param JsonReadable $reader
22
     * @return int|null
23
     */
24
    public function read(JsonReadable $reader): ?int
25
    {
26
        if ($reader->peek() === JsonToken::NULL) {
27
            $reader->nextNull();
28
            return null;
29 2
        }
30
31 2
        return $reader->nextInteger();
32 1
    }
33 1
34
    /**
35
     * Write the value to the writer for the type
36 1
     *
37
     * @param JsonWritable $writer
38
     * @param int|null $value
39
     * @return void
40
     */
41
    public function write(JsonWritable $writer, $value): void
42
    {
43
        if (null === $value) {
44
            $writer->writeNull();
45
46 2
            return;
47
        }
48 2
49 1
        $writer->writeInteger((int)$value);
50
    }
51
}
52