Passed
Push — master ( b4f2b9...02a3b1 )
by Nate
05:21
created

JsonPrimitive   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
dl 0
loc 144
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2
ccs 25
cts 29
cp 0.8621
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 2
A isString() 0 4 1
A isInteger() 0 4 1
A isFloat() 0 4 1
A isNumber() 0 4 2
A isBoolean() 0 4 1
A asString() 0 4 1
A asInteger() 0 4 1
A asFloat() 0 4 1
A asBoolean() 0 4 1
A getValue() 0 4 1
A jsonSerialize() 0 4 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\Element;
8
9
/**
10
 * Class JsonPrimitive
11
 *
12
 * Represents a json primitive (number, string, boolean)
13
 *
14
 * @author Nate Brunette <[email protected]>
15
 */
16
class JsonPrimitive extends JsonElement
17
{
18
    /**
19
     * The value of the primitive
20
     *
21
     * @var mixed
22
     */
23
    private $value;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param mixed $value
29
     */
30 6
    protected function __construct($value)
31
    {
32 6
        $this->value = $value;
33 6
    }
34
35
    /**
36
     * Factory constructor that handles nulls
37
     *
38
     * @param mixed $value
39
     * @return JsonNull|JsonPrimitive
40
     */
41 7
    public static function create($value)
42
    {
43 7
        if (null === $value) {
44 1
            return new JsonNull();
45
        }
46
47 6
        return new self($value);
48
    }
49
50
    /**
51
     * Returns true if the value is a string
52
     *
53
     * @return bool
54
     */
55 5
    public function isString(): bool
56
    {
57 5
        return is_string($this->value);
58
    }
59
60
    /**
61
     * Returns true if the value is an integer
62
     *
63
     * @return bool
64
     */
65 5
    public function isInteger(): bool
66
    {
67 5
        return is_int($this->value);
68
    }
69
70
    /**
71
     * Returns true if the value is a float
72
     *
73
     * @return bool
74
     */
75 5
    public function isFloat(): bool
76
    {
77 5
        return is_float($this->value);
78
    }
79
80
    /**
81
     * Returns true if the value is an integer or float
82
     *
83
     * @return bool
84
     */
85 5
    public function isNumber(): bool
86
    {
87 5
        return $this->isInteger() || $this->isFloat();
88
    }
89
90
    /**
91
     * Returns true if the value is a boolean
92
     *
93
     * @return bool
94
     */
95 5
    public function isBoolean(): bool
96
    {
97 5
        return is_bool($this->value);
98
    }
99
100
    /**
101
     * Cast the value to a string
102
     *
103
     * @return string
104
     */
105 1
    public function asString(): string
106
    {
107 1
        return (string) $this->value;
108
    }
109
110
    /**
111
     * Cast the value to an integer
112
     *
113
     * @return int
114
     */
115 1
    public function asInteger(): int
116
    {
117 1
        return (int) $this->value;
118
    }
119
120
    /**
121
     * Cast the value to a float
122
     *
123
     * @return float
124
     */
125 2
    public function asFloat(): float
126
    {
127 2
        return (float) $this->value;
128
    }
129
130
    /**
131
     * Cast the value to a boolean
132
     *
133
     * @return bool
134
     */
135 2
    public function asBoolean(): bool
136
    {
137 2
        return (bool) $this->value;
138
    }
139
140
    /**
141
     * Return whatever the current value is
142
     *
143
     * @return mixed
144
     */
145
    public function getValue()
146
    {
147
        return $this->value;
148
    }
149
150
    /**
151
     * Specify data which should be serialized to JSON
152
     *
153
     * @return mixed
154
     */
155
    public function jsonSerialize()
156
    {
157
        return $this->value;
158
    }
159
}
160