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

CustomWrappedTypeAdapter::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 2
crap 3
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\Internal\DefaultJsonDeserializationContext;
12
use Tebru\Gson\Internal\DefaultJsonSerializationContext;
13
use Tebru\Gson\Internal\TypeAdapterProvider;
14
use Tebru\Gson\JsonDeserializer;
15
use Tebru\Gson\JsonSerializer;
16
use Tebru\Gson\Context\ReaderContext;
17
use Tebru\Gson\TypeAdapter;
18
use Tebru\Gson\TypeAdapterFactory;
19
use Tebru\Gson\Context\WriterContext;
20
use Tebru\PhpType\TypeToken;
21
22
/**
23
 * Class CustomWrappedTypeAdapter
24
 *
25
 * Wraps a [@see JsonSerializer] or [@see JsonDeserializer] and delegates if either is null
26
 *
27
 * @author Nate Brunette <[email protected]>
28
 */
29
class CustomWrappedTypeAdapter extends TypeAdapter
30
{
31
    /**
32
     * @var TypeToken
33
     */
34
    protected $type;
35
36
    /**
37
     * @var TypeAdapterProvider
38
     */
39
    protected $typeAdapterProvider;
40
41
    /**
42
     * @var JsonSerializer
43
     */
44
    protected $serializer;
45
46
    /**
47
     * @var JsonDeserializer
48
     */
49
    protected $deserializer;
50
51
    /**
52
     * @var TypeAdapterFactory
53
     */
54
    protected $skip;
55
56
    /**
57
     * Cached instance of the delegate type adapter
58
     *
59
     * @var TypeAdapter
60
     */
61
    protected $delegateTypeAdapter;
62
63
    /**
64
     * Constructor
65
     *
66
     * @param TypeToken $type
67
     * @param TypeAdapterProvider $typeAdapterProvider
68
     * @param JsonSerializer|null $serializer
69
     * @param JsonDeserializer|null $deserializer
70
     * @param TypeAdapterFactory|null $skip
71
     */
72 9
    public function __construct(
73
        TypeToken $type,
74
        TypeAdapterProvider $typeAdapterProvider,
75
        JsonSerializer $serializer = null,
76
        JsonDeserializer $deserializer = null,
77
        TypeAdapterFactory $skip = null
78
    ) {
79 9
        $this->type = $type;
80 9
        $this->typeAdapterProvider = $typeAdapterProvider;
81 9
        $this->serializer = $serializer;
82 9
        $this->deserializer = $deserializer;
83 9
        $this->skip = $skip;
84 9
    }
85
86
    /**
87
     * Read the next value, convert it to its type and return it
88
     *
89
     * @param mixed $value
90
     * @param ReaderContext $context
91
     * @return mixed
92
     */
93 6
    public function read($value, ReaderContext $context)
94
    {
95 6
        if ($this->deserializer === null) {
96 1
            $this->delegateTypeAdapter = $this->delegateTypeAdapter ?? $this->typeAdapterProvider->getAdapter($this->type, $this->skip);
97
98 1
            return $this->delegateTypeAdapter->read($value, $context);
99
        }
100
101 5
        if ($value === null) {
102 1
            return null;
103
        }
104
105 4
        return $this->deserializer->deserialize(
106 4
            $value,
107 4
            $this->type,
108 4
            new DefaultJsonDeserializationContext($this->typeAdapterProvider, $context)
109
        );
110
    }
111
112
    /**
113
     * Write the value to the writer for the type
114
     *
115
     * @param mixed $value
116
     * @param WriterContext $context
117
     * @return mixed
118
     */
119 3
    public function write($value, WriterContext $context)
120
    {
121 3
        if ($this->serializer === null) {
122 1
            $this->delegateTypeAdapter = $this->delegateTypeAdapter ?? $this->typeAdapterProvider->getAdapter($this->type, $this->skip);
123
124 1
            return $this->delegateTypeAdapter->write($value, $context);
125
        }
126
127 2
        if ($value === null) {
128 1
            return null;
129
        }
130
131 1
        return $this->serializer->serialize(
132 1
            $value,
133 1
            $this->type,
134 1
            new DefaultJsonSerializationContext($this->typeAdapterProvider, $context)
135
        );
136
    }
137
}
138