Completed
Push — caching ( 6c9b2f )
by Nate
03:39
created

CustomWrappedTypeAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 37
c 0
b 0
f 0
dl 0
loc 122
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 17 3
A write() 0 17 3
A canCache() 0 14 4
A __construct() 0 10 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 Tebru\Gson\Context\ReaderContext;
12
use Tebru\Gson\Context\WriterContext;
13
use Tebru\Gson\Internal\DefaultJsonDeserializationContext;
14
use Tebru\Gson\Internal\DefaultJsonSerializationContext;
15
use Tebru\Gson\JsonDeserializer;
16
use Tebru\Gson\JsonSerializer;
17
use Tebru\Gson\TypeAdapter;
18
use Tebru\Gson\TypeAdapterFactory;
19
use Tebru\PhpType\TypeToken;
20
21
/**
22
 * Class CustomWrappedTypeAdapter
23
 *
24
 * Wraps a [@see JsonSerializer] or [@see JsonDeserializer] and delegates if either is null
25
 *
26
 * @author Nate Brunette <[email protected]>
27
 */
28
class CustomWrappedTypeAdapter extends TypeAdapter
29
{
30
    /**
31
     * @var TypeToken
32
     */
33
    protected $type;
34
35
    /**
36
     * @var JsonSerializer
37
     */
38
    protected $serializer;
39
40
    /**
41
     * @var JsonDeserializer
42
     */
43
    protected $deserializer;
44
45
    /**
46
     * @var TypeAdapterFactory
47
     */
48
    protected $skip;
49
50
    /**
51
     * Cached instance of the delegate type adapter
52
     *
53
     * @var TypeAdapter
54
     */
55
    protected $delegateTypeAdapter;
56
57
    /**
58
     * Constructor
59
     *
60
     * @param TypeToken $type
61
     * @param JsonSerializer|null $serializer
62
     * @param JsonDeserializer|null $deserializer
63
     * @param TypeAdapterFactory|null $skip
64
     */
65
    public function __construct(
66
        TypeToken $type,
67
        JsonSerializer $serializer = null,
68
        JsonDeserializer $deserializer = null,
69
        TypeAdapterFactory $skip = null
70
    ) {
71
        $this->type = $type;
72
        $this->serializer = $serializer;
73
        $this->deserializer = $deserializer;
74
        $this->skip = $skip;
75
    }
76
77
    /**
78
     * Read the next value, convert it to its type and return it
79
     *
80
     * @param mixed $value
81
     * @param ReaderContext $context
82
     * @return mixed
83
     */
84
    public function read($value, ReaderContext $context)
85
    {
86
        $provider = $context->getTypeAdapterProvider();
87
        if ($this->deserializer === null) {
88
            $this->delegateTypeAdapter = $this->delegateTypeAdapter ?? $provider->getAdapter($this->type, $this->skip);
89
90
            return $this->delegateTypeAdapter->read($value, $context);
91
        }
92
93
        if ($value === null) {
94
            return null;
95
        }
96
97
        return $this->deserializer->deserialize(
98
            $value,
99
            $this->type,
100
            new DefaultJsonDeserializationContext($provider, $context)
101
        );
102
    }
103
104
    /**
105
     * Write the value to the writer for the type
106
     *
107
     * @param mixed $value
108
     * @param WriterContext $context
109
     * @return mixed
110
     */
111
    public function write($value, WriterContext $context)
112
    {
113
        $provider = $context->getTypeAdapterProvider();
114
        if ($this->serializer === null) {
115
            $this->delegateTypeAdapter = $this->delegateTypeAdapter ?? $provider->getAdapter($this->type, $this->skip);
116
117
            return $this->delegateTypeAdapter->write($value, $context);
118
        }
119
120
        if ($value === null) {
121
            return null;
122
        }
123
124
        return $this->serializer->serialize(
125
            $value,
126
            $this->type,
127
            new DefaultJsonSerializationContext($provider, $context)
128
        );
129
    }
130
131
    /**
132
     * Return true if object can be written to disk
133
     *
134
     * @return bool
135
     */
136
    public function canCache(): bool
137
    {
138
        $cacheSerializer = true;
139
        $cacheDeserializer = true;
140
141
        if ($this->serializer !== null) {
142
            $cacheSerializer = $this->serializer->canCache();
143
        }
144
145
        if ($this->deserializer !== null) {
146
            $cacheDeserializer = $this->deserializer->canCache();
147
        }
148
149
        return $cacheSerializer && $cacheDeserializer;
150
    }
151
}
152