Completed
Push — master ( ddd80f...831d45 )
by Nate
03:32
created

Gson::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 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;
10
11
use Tebru\Gson\Element\JsonElement;
12
use Tebru\Gson\Internal\ObjectConstructor\CreateFromInstance;
13
use Tebru\Gson\Internal\ObjectConstructorAware;
14
use Tebru\Gson\Internal\TypeAdapterProvider;
15
use Tebru\PhpType\TypeToken;
16
17
/**
18
 * Class Gson
19
 *
20
 * @author Nate Brunette <[email protected]>
21
 */
22
class Gson
23
{
24
    /**
25
     * A service to fetch the correct [@see TypeAdapter] for a given type
26
     *
27
     * @var TypeAdapterProvider
28
     */
29
    private $typeAdapterProvider;
30
31
    /**
32
     * True if we should serialize nulls
33
     *
34
     * @var bool
35
     */
36
    private $serializeNull;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param TypeAdapterProvider $typeAdapterProvider
42
     * @param bool $serializeNull
43
     */
44 35
    public function __construct(TypeAdapterProvider $typeAdapterProvider, bool $serializeNull)
45
    {
46 35
        $this->typeAdapterProvider = $typeAdapterProvider;
47 35
        $this->serializeNull = $serializeNull;
48 35
    }
49
50
    /**
51
     * Create a new builder object
52
     *
53
     * @return GsonBuilder
54
     */
55 38
    public static function builder(): GsonBuilder
56
    {
57 38
        return new GsonBuilder();
58
    }
59
60
    /**
61
     * Converts an object to a json string
62
     *
63
     * @param mixed $object
64
     * @return string
65
     * @throws \InvalidArgumentException
66
     */
67 17
    public function toJson($object): string
68
    {
69 17
        $type = TypeToken::createFromVariable($object);
70 17
        $typeAdapter = $this->typeAdapterProvider->getAdapter($type);
71
72 17
        return $typeAdapter->writeToJson($object, $this->serializeNull);
73
    }
74
75
    /**
76
     * Converts a json string to a valid json type
77
     *
78
     * @param string $json
79
     * @param object|string $type
80
     * @return mixed
81
     * @throws \InvalidArgumentException
82
     * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be parsed
83
     * @throws \Tebru\Gson\Exception\JsonParseException If the json cannot be decoded
84
     */
85 20
    public function fromJson(string $json, $type)
86
    {
87 20
        $isObject = is_object($type);
88 20
        $typeToken = $isObject ? new TypeToken(get_class($type)) : new TypeToken($type);
89 20
        $typeAdapter = $this->typeAdapterProvider->getAdapter($typeToken);
90
91 20
        if ($isObject && $typeAdapter instanceof ObjectConstructorAware) {
92 2
            $typeAdapter->setObjectConstructor(new CreateFromInstance($type));
93
        }
94
95 20
        return $typeAdapter->readFromJson($json);
96
    }
97
98
    /**
99
     * Converts an object to a [@see JsonElement]
100
     *
101
     * This is a convenience method that first converts an object to json utilizing all of the
102
     * type adapters, then converts that json to a JsonElement.  From here you can modify the
103
     * JsonElement and call json_encode() on it to get json.
104
     *
105
     * @param mixed $object
106
     * @return JsonElement
107
     * @throws \InvalidArgumentException
108
     * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be parsed
109
     * @throws \Tebru\Gson\Exception\JsonParseException If the json cannot be decoded
110
     */
111 2
    public function toJsonElement($object): JsonElement
112
    {
113 2
        return $this->fromJson($this->toJson($object), JsonElement::class);
114
    }
115
116
    /**
117
     * Convenience method to convert an object to a json_decode'd array
118
     *
119
     * @param object $object
120
     * @return array
121
     * @throws \InvalidArgumentException
122
     */
123 4
    public function toArray($object): array
124
    {
125 4
        return (array)json_decode($this->toJson($object), true);
126
    }
127
128
    /**
129
     * Convenience method to deserialize an array into an object
130
     *
131
     * @param array $jsonArray
132
     * @param mixed $type
133
     * @return mixed
134
     * @throws \Tebru\PhpType\Exception\MalformedTypeException
135
     * @throws \Tebru\Gson\Exception\JsonParseException
136
     * @throws \InvalidArgumentException
137
     */
138 1
    public function fromArray(array $jsonArray, $type)
139
    {
140 1
        return $this->fromJson(json_encode($jsonArray), $type);
141
    }
142
}
143