Completed
Pull Request — master (#44)
by Nate
05:11 queued 02:28
created

StringTypeAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 34
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 8 2
A write() 0 9 2
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 Tebru\Gson\JsonReadable;
12
use Tebru\Gson\JsonToken;
13
use Tebru\Gson\JsonWritable;
14
use Tebru\Gson\TypeAdapter;
15
16
/**
17
 * Class StringTypeAdapter
18
 *
19
 * @author Nate Brunette <[email protected]>
20
 */
21
class StringTypeAdapter extends TypeAdapter
22
{
23
    /**
24
     * Read the next value, convert it to its type and return it
25
     *
26
     * @param JsonReadable $reader
27
     * @return string|null
28
     */
29 3
    public function read(JsonReadable $reader): ?string
30
    {
31 3
        if ($reader->peek() === JsonToken::NULL) {
32 1
            $reader->nextNull();
33 1
            return null;
34
        }
35
36 2
        return $reader->nextString();
37
    }
38
39
    /**
40
     * Write the value to the writer for the type
41
     *
42
     * @param JsonWritable $writer
43
     * @param string|null $value
44
     * @return void
45
     */
46 2
    public function write(JsonWritable $writer, $value): void
47
    {
48 2
        if (null === $value) {
49 1
            $writer->writeNull();
50
51 1
            return;
52
        }
53
54 1
        $writer->writeString((string)$value);
55 1
    }
56
}
57