1 | <?php |
||
43 | final class 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 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
87 | */ |
||
88 | public function __construct(string $type) |
||
94 | |||
95 | /** |
||
96 | * Create a new instance from a variable |
||
97 | * |
||
98 | * @param mixed $variable |
||
99 | * @return PhpType |
||
100 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
101 | */ |
||
102 | public static function createFromVariable($variable): PhpType |
||
106 | |||
107 | /** |
||
108 | * Recursively parse type. If generics are found, this will create |
||
109 | * new PhpTypes. |
||
110 | * |
||
111 | * @param string $type |
||
112 | * @return void |
||
113 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
114 | */ |
||
115 | private function parseType(string $type): void |
||
168 | |||
169 | /** |
||
170 | * Create a type enum and set the class if necessary |
||
171 | * |
||
172 | * @param string $type |
||
173 | * @return void |
||
174 | */ |
||
175 | private function setType(string $type): void |
||
185 | |||
186 | /** |
||
187 | * Returns an array of generic types |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getGenerics(): array |
||
195 | |||
196 | /** |
||
197 | * Returns the class as a string or null if there isn't a class |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getClass(): ?string |
||
205 | |||
206 | /** |
||
207 | * Returns true if this is a string |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | public function isString(): bool |
||
215 | |||
216 | /** |
||
217 | * Returns true if this is an integer |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isInteger(): bool |
||
225 | |||
226 | /** |
||
227 | * Returns true if this is a float |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function isFloat(): bool |
||
235 | |||
236 | /** |
||
237 | * Returns true if this is a boolean |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isBoolean(): bool |
||
245 | |||
246 | /** |
||
247 | * Returns true if this is an array |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function isArray(): bool |
||
255 | |||
256 | /** |
||
257 | * Returns true if this is an object |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function isObject(): bool |
||
265 | |||
266 | /** |
||
267 | * Returns true if this is null |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function isNull(): bool |
||
275 | |||
276 | /** |
||
277 | * Returns true if this is a resource |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function isResource(): bool |
||
285 | |||
286 | /** |
||
287 | * Returns true if the type could be anything |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function isWildcard(): bool |
||
295 | |||
296 | /** |
||
297 | * Returns an array of extra options |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getOptions(): array |
||
305 | |||
306 | /** |
||
307 | * Sets extra options on this type |
||
308 | * |
||
309 | * @param array $options |
||
310 | * @return void |
||
311 | */ |
||
312 | public function setOptions(array $options): void |
||
316 | |||
317 | /** |
||
318 | * Return the initial type including generics |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function __toString(): string |
||
326 | } |
||
327 |