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 | * An array of interfaces that a class implements |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $interfaces = []; |
||
72 | |||
73 | /** |
||
74 | * Generic types, if they exist |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | private $genericTypes = []; |
||
79 | |||
80 | /** |
||
81 | * Various options a type might need to reference |
||
82 | * |
||
83 | * For example, a DateTime object might want to store formatting options |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private $options = []; |
||
88 | |||
89 | /** |
||
90 | * Constructor |
||
91 | * |
||
92 | * @param string $type |
||
93 | * @param array $options |
||
94 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
95 | */ |
||
96 | 51 | public function __construct(string $type, array $options = []) |
|
103 | |||
104 | /** |
||
105 | * Create a new instance from a variable |
||
106 | * |
||
107 | * @param mixed $variable |
||
108 | * @return PhpType |
||
109 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
110 | */ |
||
111 | 8 | public static function createFromVariable($variable): PhpType |
|
115 | |||
116 | /** |
||
117 | * Recursively parse type. If generics are found, this will create |
||
118 | * new PhpTypes. |
||
119 | * |
||
120 | * @param string $type |
||
121 | * @return void |
||
122 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
123 | */ |
||
124 | 51 | private function parseType(string $type): void |
|
177 | |||
178 | /** |
||
179 | * Create a type enum and set the class if necessary |
||
180 | * |
||
181 | * @param string $type |
||
182 | * @return void |
||
183 | */ |
||
184 | 50 | private function setType(string $type): void |
|
195 | |||
196 | /** |
||
197 | * Returns an array of generic types |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 4 | public function getGenerics(): array |
|
205 | |||
206 | /** |
||
207 | * Returns the class if an object, or the type as a string |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | 14 | public function getType(): ?string |
|
215 | |||
216 | /** |
||
217 | * Returns true if the type matches the class, parent, full type, or one of the interfaces |
||
218 | * |
||
219 | * @param string $type |
||
220 | * @return bool |
||
221 | */ |
||
222 | 6 | public function isA(string $type): bool |
|
239 | |||
240 | /** |
||
241 | * Returns true if this is a string |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 1 | public function isString(): bool |
|
249 | |||
250 | /** |
||
251 | * Returns true if this is an integer |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 2 | public function isInteger(): bool |
|
259 | |||
260 | /** |
||
261 | * Returns true if this is a float |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | 2 | public function isFloat(): bool |
|
269 | |||
270 | /** |
||
271 | * Returns true if this is a boolean |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | 2 | public function isBoolean(): bool |
|
279 | |||
280 | /** |
||
281 | * Returns true if this is an array |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | 4 | public function isArray(): bool |
|
289 | |||
290 | /** |
||
291 | * Returns true if this is an object |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | 50 | public function isObject(): bool |
|
299 | |||
300 | /** |
||
301 | * Returns true if this is null |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | 2 | public function isNull(): bool |
|
309 | |||
310 | /** |
||
311 | * Returns true if this is a resource |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | 1 | public function isResource(): bool |
|
319 | |||
320 | /** |
||
321 | * Returns true if the type could be anything |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | 1 | public function isWildcard(): bool |
|
329 | |||
330 | /** |
||
331 | * Returns an array of extra options |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | 1 | public function getOptions(): array |
|
339 | |||
340 | /** |
||
341 | * Returns a unique identifying key for this type based on |
||
342 | * the full type and options |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | 3 | public function getUniqueKey(): string |
|
352 | |||
353 | /** |
||
354 | * Return the initial type including generics |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | 16 | public function __toString(): string |
|
362 | } |
||
363 |