Completed
Push — v0.7 ( 7c9b41...5c4e91 )
by Nate
02:30
created

Gson::fromNormalized()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 4
cts 4
cp 1
rs 9.2222
cc 6
nc 5
nop 2
crap 6
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
use Tebru\Gson\Internal\DefaultReaderContext;
12
use Tebru\Gson\Internal\DefaultWriterContext;
13
use Tebru\Gson\Internal\ObjectConstructor\CreateFromInstance;
14
use Tebru\Gson\Internal\ObjectConstructorAware;
15
use Tebru\Gson\Internal\TypeAdapterProvider;
16
use Tebru\PhpType\TypeToken;
17
18
/**
19
 * Class Gson
20
 *
21
 * @author Nate Brunette <[email protected]>
22
 */
23
class Gson
24
{
25
    /**
26
     * A service to fetch the correct [@see TypeAdapter] for a given type
27
     *
28
     * @var TypeAdapterProvider
29
     */
30
    private $typeAdapterProvider;
31
32
    /**
33
     * @var ReaderContext
34
     */
35
    private $readerContext;
36
37
    /**
38
     * @var WriterContext
39
     */
40
    private $writerContext;
41
42
    /**
43
     * Constructor
44
     *
45 51
     * @param TypeAdapterProvider $typeAdapterProvider
46
     * @param ReaderContext $readerContext
47 51
     * @param WriterContext $writerContext
48 51
     */
49 51
    public function __construct(
50
        TypeAdapterProvider $typeAdapterProvider,
51
        ReaderContext $readerContext,
52
        WriterContext $writerContext
53
    ) {
54
        $this->typeAdapterProvider = $typeAdapterProvider;
55
        $this->readerContext = $readerContext;
56 55
        $this->writerContext = $writerContext;
57
    }
58 55
59
    /**
60
     * Create a new builder object
61
     *
62
     * @return GsonBuilder
63
     */
64
    public static function builder(): GsonBuilder
65
    {
66
        return new GsonBuilder();
67
    }
68
69
    /**
70 20
     * Convenience method to convert an object to a json_decode'd array
71
     *
72 20
     * Optionally accepts a type to force serialization to
73 20
     *
74
     * @param mixed $object
75 20
     * @param null|string $type
76
     * @return mixed
77
     */
78
    public function toNormalized($object, ?string $type = null)
79
    {
80
        if (is_scalar($object) && $this->writerContext->enableScalarAdapters()) {
81
            return $object;
82
        }
83
84
        $typeToken = $type === null ? TypeToken::createFromVariable($object) : TypeToken::create($type);
85 27
        $typeAdapter = $this->typeAdapterProvider->getAdapter($typeToken);
86
87 27
        return $typeAdapter->write($object, $this->writerContext ?? new DefaultWriterContext());
88 27
    }
89 27
90 27
    /**
91 27
     * Converts an object to a json string
92
     *
93 27
     * Optionally accepts a type to force serialization to
94 2
     *
95
     * @param mixed $object
96
     * @param null|string $type
97 27
     * @return string
98
     */
99
    public function toJson($object, ?string $type = null): string
100
    {
101
        return json_encode($this->toNormalized($object, $type));
102
    }
103
104
    /**
105
     * Converts a json string to a valid json type
106
     *
107
     * @param mixed $decodedJson
108
     * @param object|string $type
109 3
     * @return mixed
110
     */
111 3
    public function fromNormalized($decodedJson, $type)
112 3
    {
113
        if (is_scalar($decodedJson) && $this->readerContext->enableScalarAdapters()) {
114 3
            return $decodedJson;
115
        }
116
117
        $isObject = is_object($type);
118
        $typeToken = $isObject ? TypeToken::create(get_class($type)) : TypeToken::create($type);
119
        $typeAdapter = $this->typeAdapterProvider->getAdapter($typeToken);
120
        $context = $this->readerContext ?? new DefaultReaderContext();
121
        $context->setUsesExistingObject($isObject)->setPayload($decodedJson);
122
123
        if ($isObject && $typeAdapter instanceof ObjectConstructorAware) {
124
            $typeAdapter->setObjectConstructor(new CreateFromInstance($type));
125
        }
126 5
127
        return $typeAdapter->read($decodedJson, $context);
128 5
    }
129
130
    /**
131
     * Converts a json string to a valid json type
132
     *
133
     * @param string $json
134
     * @param object|string $type
135
     * @return mixed
136
     */
137
    public function fromJson(string $json, $type)
138 1
    {
139
        return $this->fromNormalized(json_decode($json, true), $type);
140 1
    }
141
}
142