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
|
|
|
|