Completed
Push — master ( 25e509...cbf4a9 )
by Nate
02:51
created

Gson::toJsonElement()   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
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Gson;
8
9
use Tebru\Gson\Element\JsonElement;
10
use Tebru\Gson\Internal\Data\Property;
11
use Tebru\Gson\Internal\Data\PropertyCollectionFactory;
12
use Tebru\Gson\Internal\TypeAdapterProvider;
13
14
/**
15
 * Class Gson
16
 *
17
 * @author Nate Brunette <[email protected]>
18
 */
19
class Gson
20
{
21
    /**
22
     * A service to fetch the correct [@see TypeAdapter] for a given type
23
     *
24
     * @var TypeAdapterProvider
25
     */
26
    private $typeAdapterProvider;
27
28
    /**
29
     * A factory that reflects over class properties and returns a collection
30
     * of [@see Property] objects
31
     *
32
     * @var \Tebru\Gson\Internal\Data\PropertyCollectionFactory
33
     */
34
    private $propertyCollectionFactory;
35
36
    /**
37
     * True if we should serialize nulls
38
     *
39
     * @var bool
40
     */
41
    private $serializeNull;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param TypeAdapterProvider $typeAdapterProvider
47
     * @param PropertyCollectionFactory $propertyCollectionFactory
48
     * @param bool $serializeNull
49
     */
50 25
    public function __construct(
51
        TypeAdapterProvider $typeAdapterProvider,
52
        PropertyCollectionFactory $propertyCollectionFactory,
53
        bool $serializeNull
54
    ) {
55 25
        $this->typeAdapterProvider = $typeAdapterProvider;
56 25
        $this->propertyCollectionFactory = $propertyCollectionFactory;
57 25
        $this->serializeNull = $serializeNull;
58 25
    }
59
60
    /**
61
     * Create a new builder object
62
     *
63
     * @return GsonBuilder
64
     */
65 28
    public static function builder(): GsonBuilder
66
    {
67 28
        return new GsonBuilder();
68
    }
69
70
    /**
71
     * Converts an object to a json string
72
     *
73
     * @param mixed $object
74
     * @return string
75
     * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed
76
     * @throws \InvalidArgumentException if the type cannot be handled by a type adapter
77
     */
78 11
    public function toJson($object): string
79
    {
80 11
        $phpType = PhpType::createFromVariable($object);
81 11
        $typeAdapter = $this->typeAdapterProvider->getAdapter($phpType);
82
83 11
        return $typeAdapter->writeToJson($object, $this->serializeNull);
84
    }
85
86
    /**
87
     * Converts a json string to a valid json type
88
     *
89
     * @param string $json
90
     * @param object|string $type
91
     * @return mixed
92
     * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed
93
     * @throws \InvalidArgumentException if the type cannot be handled by a type adapter
94
     * @throws \RuntimeException If the value is not valid
95
     * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed
96
     * @throws \InvalidArgumentException if the type cannot be handled by a type adapter
97
     */
98 15
    public function fromJson(string $json, $type)
99
    {
100 15
        $phpType = is_object($type) ? new PhpType(get_class($type)) : new PhpType($type);
101 15
        $typeAdapter = $this->typeAdapterProvider->getAdapter($phpType);
102 15
        $instance = $typeAdapter->readFromJson($json);
103
104 15
        if (is_string($type)) {
105 14
            return $instance;
106
        }
107
108 1
        $properties = $this->propertyCollectionFactory->create($phpType, $this->typeAdapterProvider);
109
110
        /** @var Property $property */
111 1
        foreach ($properties as $property) {
112 1
            $property->set($type, $property->get($instance));
113
        }
114
115 1
        return $type;
116
    }
117
118
    /**
119
     * Converts an object to a [@see JsonElement]
120
     *
121
     * This is a convenience method that first converts an object to json utilizing all of the
122
     * type adapters, then converts that json to a JsonElement.  From here you can modify the
123
     * JsonElement and call json_encode() on it to get json.
124
     *
125
     * @param mixed $object
126
     * @return JsonElement
127
     * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed
128
     * @throws \InvalidArgumentException if the type cannot be handled by a type adapter
129
     * @throws \RuntimeException If the value is not valid
130
     * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed
131
     * @throws \InvalidArgumentException if the type cannot be handled by a type adapter
132
     */
133 1
    public function toJsonElement($object): JsonElement
134
    {
135 1
        return $this->fromJson($this->toJson($object), JsonElement::class);
136
    }
137
}
138