1 | <?php |
||
43 | final class DefaultPhpType implements PhpType |
||
44 | { |
||
45 | /** |
||
46 | * The initial type |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $fullType; |
||
51 | |||
52 | /** |
||
53 | * An enum representing core php types |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | private $type; |
||
58 | |||
59 | /** |
||
60 | * If the type is an object, this will be the object's class name |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $class; |
||
65 | |||
66 | /** |
||
67 | * Generic types, if they exist |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $genericTypes = []; |
||
72 | |||
73 | /** |
||
74 | * Various options a type might need to reference |
||
75 | * |
||
76 | * For example, a DateTime object might want to store formatting options |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | private $options = []; |
||
81 | |||
82 | /** |
||
83 | * Constructor |
||
84 | * |
||
85 | * @param string $type |
||
86 | * @param array $options |
||
87 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
88 | */ |
||
89 | 40 | public function __construct(string $type, array $options = []) |
|
96 | |||
97 | /** |
||
98 | * Create a new instance from a variable |
||
99 | * |
||
100 | * @param mixed $variable |
||
101 | * @return PhpType |
||
102 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
103 | */ |
||
104 | 8 | public static function createFromVariable($variable): PhpType |
|
108 | |||
109 | /** |
||
110 | * Recursively parse type. If generics are found, this will create |
||
111 | * new PhpTypes. |
||
112 | * |
||
113 | * @param string $type |
||
114 | * @return void |
||
115 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
116 | */ |
||
117 | 40 | private function parseType(string $type): void |
|
170 | |||
171 | /** |
||
172 | * Create a type enum and set the class if necessary |
||
173 | * |
||
174 | * @param string $type |
||
175 | * @return void |
||
176 | */ |
||
177 | 39 | private function setType(string $type): void |
|
187 | |||
188 | /** |
||
189 | * Returns an array of generic types |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | 4 | public function getGenerics(): array |
|
197 | |||
198 | /** |
||
199 | * Returns the class if an object, or the type as a string |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 5 | public function getType(): ?string |
|
207 | |||
208 | /** |
||
209 | * Returns true if this is a string |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 1 | public function isString(): bool |
|
217 | |||
218 | /** |
||
219 | * Returns true if this is an integer |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | 2 | public function isInteger(): bool |
|
227 | |||
228 | /** |
||
229 | * Returns true if this is a float |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 2 | public function isFloat(): bool |
|
237 | |||
238 | /** |
||
239 | * Returns true if this is a boolean |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 2 | public function isBoolean(): bool |
|
247 | |||
248 | /** |
||
249 | * Returns true if this is an array |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | 4 | public function isArray(): bool |
|
257 | |||
258 | /** |
||
259 | * Returns true if this is an object |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 39 | public function isObject(): bool |
|
267 | |||
268 | /** |
||
269 | * Returns true if this is null |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | 2 | public function isNull(): bool |
|
277 | |||
278 | /** |
||
279 | * Returns true if this is a resource |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | 1 | public function isResource(): bool |
|
287 | |||
288 | /** |
||
289 | * Returns true if the type could be anything |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | 1 | public function isWildcard(): bool |
|
297 | |||
298 | /** |
||
299 | * Returns an array of extra options |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | 1 | public function getOptions(): array |
|
307 | |||
308 | /** |
||
309 | * Returns a unique identifying key for this type based on |
||
310 | * the full type and options |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | 3 | public function getUniqueKey(): string |
|
320 | |||
321 | /** |
||
322 | * Return the initial type including generics |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 16 | public function __toString(): string |
|
330 | } |
||
331 |