1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Tebru\PhpType; |
10
|
|
|
|
11
|
|
|
use stdClass; |
12
|
|
|
use Tebru\PhpType\Exception\MalformedTypeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class TypeToken |
16
|
|
|
* |
17
|
|
|
* Wrapper around core php types and custom types. It can be used as simply as |
18
|
|
|
* |
19
|
|
|
* new TypeToken('string'); |
20
|
|
|
* |
21
|
|
|
* To create a string type. |
22
|
|
|
* |
23
|
|
|
* This class also allows us to fake generic types. The syntax to |
24
|
|
|
* represent generics uses angle brackets <>. |
25
|
|
|
* |
26
|
|
|
* For example: |
27
|
|
|
* |
28
|
|
|
* array<int> |
29
|
|
|
* |
30
|
|
|
* Would represent an array of ints. |
31
|
|
|
* |
32
|
|
|
* array<string, int> |
33
|
|
|
* |
34
|
|
|
* Would represent an array using string keys and int values. |
35
|
|
|
* |
36
|
|
|
* They can be combined, like so |
37
|
|
|
* |
38
|
|
|
* array<string, array<int>> |
39
|
|
|
* |
40
|
|
|
* To represent a array with string keys and an array of ints as values. |
41
|
|
|
* |
42
|
|
|
* @author Nate Brunette <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
final class TypeToken |
45
|
|
|
{ |
46
|
|
|
public const STRING = 'string'; |
47
|
|
|
public const INTEGER = 'integer'; |
48
|
|
|
public const FLOAT = 'float'; |
49
|
|
|
public const BOOLEAN = 'boolean'; |
50
|
|
|
public const HASH = 'array'; |
51
|
|
|
public const OBJECT = 'object'; |
52
|
|
|
public const NULL = 'null'; |
53
|
|
|
public const RESOURCE = 'resource'; |
54
|
|
|
public const WILDCARD = '?'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The full initial type |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $fullTypeString; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The core php type (string, int, etc) or class if object |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $rawType; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The core php type (string, int, object, etc) |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
private $phpType; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* An array of parent classes and interfaces that a class implements |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
private $parents = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generic types, if they exist |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
private $genericTypes = []; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Constructor |
93
|
|
|
* |
94
|
|
|
* @param string $type |
95
|
|
|
*/ |
96
|
40 |
|
public function __construct(string $type) |
97
|
|
|
{ |
98
|
40 |
|
$type = \trim($type); |
99
|
40 |
|
$this->fullTypeString = $type; |
100
|
40 |
|
$this->parseType($type); |
101
|
39 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a new instance from a variable |
105
|
|
|
* |
106
|
|
|
* @param mixed $variable |
107
|
|
|
* @return TypeToken |
108
|
|
|
*/ |
109
|
8 |
|
public static function createFromVariable($variable): TypeToken |
110
|
|
|
{ |
111
|
8 |
|
return \is_object($variable) ? new self(\get_class($variable)) : new self(\gettype($variable)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the class if an object, or the type as a string |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
8 |
|
public function getRawType(): string |
120
|
|
|
{ |
121
|
8 |
|
return $this->rawType; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the core php type |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
public function getPhpType(): string |
130
|
|
|
{ |
131
|
2 |
|
return $this->phpType; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns an array of generic types |
136
|
|
|
* |
137
|
|
|
* @return TypeToken[] |
138
|
|
|
*/ |
139
|
32 |
|
public function getGenerics(): array |
140
|
|
|
{ |
141
|
32 |
|
return $this->genericTypes; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns true if the type matches the class, parent, full type, or one of the interfaces |
146
|
|
|
* |
147
|
|
|
* @param string $type |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
4 |
|
public function isA(string $type): bool |
151
|
|
|
{ |
152
|
4 |
|
if ($this->rawType === $type) { |
153
|
4 |
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
if ($this->fullTypeString === $type) { |
157
|
1 |
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
if (\in_array($type, $this->parents, true)) { |
161
|
1 |
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns true if this is a string |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
1 |
|
public function isString(): bool |
173
|
|
|
{ |
174
|
1 |
|
return $this->phpType === self::STRING; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns true if this is an integer |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
2 |
|
public function isInteger(): bool |
183
|
|
|
{ |
184
|
2 |
|
return $this->phpType === self::INTEGER; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns true if this is a float |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
2 |
|
public function isFloat(): bool |
193
|
|
|
{ |
194
|
2 |
|
return $this->phpType === self::FLOAT; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns true if this is a boolean |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
2 |
|
public function isBoolean(): bool |
203
|
|
|
{ |
204
|
2 |
|
return $this->phpType === self::BOOLEAN; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns true if this is an array |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
4 |
|
public function isArray(): bool |
213
|
|
|
{ |
214
|
4 |
|
return $this->phpType === self::HASH; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns true if this is an object |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
39 |
|
public function isObject(): bool |
223
|
|
|
{ |
224
|
39 |
|
return $this->phpType === self::OBJECT; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns true if this is null |
229
|
|
|
* |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
2 |
|
public function isNull(): bool |
233
|
|
|
{ |
234
|
2 |
|
return $this->phpType === self::NULL; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Returns true if this is a resource |
239
|
|
|
* |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
1 |
|
public function isResource(): bool |
243
|
|
|
{ |
244
|
1 |
|
return $this->phpType === self::RESOURCE; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Returns true if the type could be anything |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
1 |
|
public function isWildcard(): bool |
253
|
|
|
{ |
254
|
1 |
|
return $this->phpType === self::WILDCARD; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return the initial type including generics |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
15 |
|
public function __toString(): string |
263
|
|
|
{ |
264
|
15 |
|
return $this->fullTypeString; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Recursively parse type. If generics are found, this will create |
269
|
|
|
* new PhpTypes. |
270
|
|
|
* |
271
|
|
|
* @param string $type |
272
|
|
|
* @return void |
273
|
|
|
* @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be processed |
274
|
|
|
*/ |
275
|
40 |
|
private function parseType(string $type): void |
276
|
|
|
{ |
277
|
40 |
|
$start = \strpos($type, '<'); |
278
|
40 |
|
if ($start === false) { |
279
|
39 |
|
$this->setTypes($type); |
280
|
39 |
|
return; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// get start and end positions of generic |
284
|
8 |
|
$end = \strrpos($type, '>'); |
285
|
8 |
|
if ($end === false) { |
286
|
1 |
|
throw new MalformedTypeException('Could not find ending ">" for generic type'); |
287
|
|
|
} |
288
|
|
|
|
289
|
7 |
|
$originalType = $type; |
290
|
|
|
|
291
|
|
|
// get generic types |
292
|
7 |
|
$generics = \substr($type, $start + 1, $end - $start - 1); |
293
|
|
|
|
294
|
|
|
// iterate over subtype to determine if format is <type> or <key, type> |
295
|
7 |
|
$depth = 0; |
296
|
7 |
|
$type = ''; |
297
|
7 |
|
foreach (\str_split($generics) as $char) { |
298
|
|
|
// stepping into another generic type |
299
|
7 |
|
if ($char === '<') { |
300
|
2 |
|
$depth++; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
// stepping out of generic type |
304
|
7 |
|
if ($char === '>') { |
305
|
2 |
|
$depth--; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// if the character is not a comma, or we're not on the first level |
309
|
|
|
// write the character to the buffer and continue loop |
310
|
7 |
|
if ($char !== ',' || $depth !== 0) { |
311
|
7 |
|
$type .= $char; |
312
|
7 |
|
continue; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
// add new type to list |
316
|
4 |
|
$this->genericTypes[] = new TypeToken($type); |
317
|
|
|
|
318
|
|
|
// reset type buffer |
319
|
4 |
|
$type = ''; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// add final type |
323
|
7 |
|
$this->genericTypes[] = new TypeToken($type); |
324
|
|
|
|
325
|
|
|
// set the main type |
326
|
7 |
|
$this->setTypes(\substr($originalType, 0, $start)); |
327
|
7 |
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Create a type enum and set the class if necessary |
331
|
|
|
* |
332
|
|
|
* @param string $rawType |
333
|
|
|
* @return void |
334
|
|
|
*/ |
335
|
39 |
|
private function setTypes(string $rawType): void |
336
|
|
|
{ |
337
|
39 |
|
$this->phpType = $this->getNormalizedType($rawType); |
338
|
|
|
|
339
|
|
|
// if we're not working with an object, set the raw type to |
340
|
|
|
// the core php type so we can make sure it's normalized |
341
|
39 |
|
if (!$this->isObject()) { |
342
|
32 |
|
$this->rawType = $this->phpType; |
343
|
|
|
|
344
|
|
|
// if there aren't any generics, overwrite full type as well |
345
|
32 |
|
if ($this->getGenerics() === []) { |
346
|
32 |
|
$this->fullTypeString = $this->rawType; |
347
|
|
|
} |
348
|
32 |
|
return; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// use \stdClass as the class name for generic objects |
352
|
10 |
|
$this->rawType = self::OBJECT === $rawType ? stdClass::class : $rawType; |
353
|
|
|
|
354
|
|
|
// if we're dealing with a real class, get parents and interfaces so |
355
|
|
|
// it's easy to check if the type is an instance of another |
356
|
10 |
|
if (\class_exists($rawType)) { |
357
|
9 |
|
$this->parents = \array_merge(\class_parents($this->rawType), \class_implements($this->rawType)); |
358
|
|
|
} |
359
|
10 |
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Get a normalized core php type |
363
|
|
|
* |
364
|
|
|
* @param string $type |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
39 |
|
private function getNormalizedType(string $type): string |
368
|
|
|
{ |
369
|
|
|
switch ($type) { |
370
|
39 |
|
case 'string': |
371
|
8 |
|
return self::STRING; |
372
|
35 |
|
case 'int': |
373
|
32 |
|
case 'integer': |
374
|
10 |
|
return self::INTEGER; |
375
|
30 |
|
case 'double': |
376
|
28 |
|
case 'float': |
377
|
3 |
|
return self::FLOAT; |
378
|
27 |
|
case 'bool': |
379
|
26 |
|
case 'boolean': |
380
|
6 |
|
return self::BOOLEAN; |
381
|
23 |
|
case 'array': |
382
|
10 |
|
return self::HASH; |
383
|
15 |
|
case 'null': |
384
|
14 |
|
case 'NULL': |
385
|
3 |
|
return self::NULL; |
386
|
12 |
|
case 'resource': |
387
|
1 |
|
return self::RESOURCE; |
388
|
11 |
|
case '?': |
389
|
1 |
|
return self::WILDCARD; |
390
|
|
|
default: |
391
|
10 |
|
return self::OBJECT; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|