|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spatie\Typed; |
|
6
|
|
|
|
|
7
|
|
|
use Spatie\Typed\Excpetions\InferredTypeError; |
|
8
|
|
|
use Spatie\Typed\Types\NullType; |
|
9
|
|
|
use Spatie\Typed\Types\ArrayType; |
|
10
|
|
|
use Spatie\Typed\Types\FloatType; |
|
11
|
|
|
use Spatie\Typed\Types\UnionType; |
|
12
|
|
|
use Spatie\Typed\Types\StringType; |
|
13
|
|
|
use Spatie\Typed\Types\BooleanType; |
|
14
|
|
|
use Spatie\Typed\Types\GenericType; |
|
15
|
|
|
use Spatie\Typed\Types\IntegerType; |
|
16
|
|
|
use Spatie\Typed\Types\CallableType; |
|
17
|
|
|
use Spatie\Typed\Types\CollectionType; |
|
18
|
|
|
|
|
19
|
|
|
class T |
|
20
|
|
|
{ |
|
21
|
|
|
public static function array(): ArrayType |
|
22
|
|
|
{ |
|
23
|
|
|
return new ArrayType(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function bool(): BooleanType |
|
27
|
|
|
{ |
|
28
|
|
|
return new BooleanType(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function boolean(): BooleanType |
|
32
|
|
|
{ |
|
33
|
|
|
return new BooleanType(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function callable(): CallableType |
|
37
|
|
|
{ |
|
38
|
|
|
return new CallableType(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function collection(): CollectionType |
|
42
|
|
|
{ |
|
43
|
|
|
return new CollectionType(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function float(): FloatType |
|
47
|
|
|
{ |
|
48
|
|
|
return new FloatType(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function double(): FloatType |
|
52
|
|
|
{ |
|
53
|
|
|
return new FloatType(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function generic(string $type): GenericType |
|
57
|
|
|
{ |
|
58
|
|
|
return new GenericType($type); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function int(): IntegerType |
|
62
|
|
|
{ |
|
63
|
|
|
return new IntegerType(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function integer(): IntegerType |
|
67
|
|
|
{ |
|
68
|
|
|
return new IntegerType(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function string(): StringType |
|
72
|
|
|
{ |
|
73
|
|
|
return new StringType(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function union(Type ...$types): UnionType |
|
77
|
|
|
{ |
|
78
|
|
|
return new UnionType(...$types); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function nullable(Type $type): NullType |
|
82
|
|
|
{ |
|
83
|
|
|
return new NullType($type); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public static function infer($value): Type |
|
87
|
|
|
{ |
|
88
|
|
|
if (is_callable($value)) { |
|
89
|
|
|
return new CallableType(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (is_object($value)) { |
|
93
|
|
|
return new GenericType(get_class($value)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$type = gettype($value); |
|
97
|
|
|
|
|
98
|
|
|
if (! method_exists(self::class, $type)) { |
|
99
|
|
|
throw InferredTypeError::cannotInferType($type); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return forward_static_call(self::class . '::' . $type); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|