1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Apie\TypeJuggling; |
5
|
|
|
|
6
|
|
|
use Apie\TypeJuggling\Exceptions\UnimplementedPhpDocBlockException; |
7
|
|
|
use phpDocumentor\Reflection\Type; |
8
|
|
|
use phpDocumentor\Reflection\Types as Types; |
9
|
|
|
|
10
|
|
|
class TypeUtils |
11
|
|
|
{ |
12
|
|
|
private function __construct() |
13
|
|
|
{ |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function fromObjectToTypeUtilInterface(string $fieldName, $object): TypeUtilInterface |
17
|
|
|
{ |
18
|
|
|
if ($object === null) { |
19
|
|
|
return new NullObject(); |
20
|
|
|
} |
21
|
|
|
switch (gettype($object)) { |
22
|
|
|
case 'boolean': |
23
|
|
|
return new Boolean($fieldName); |
24
|
|
|
case 'integer': |
25
|
|
|
return new Integer($fieldName); |
26
|
|
|
case 'double': |
27
|
|
|
return new FloatingPoint($fieldName); |
28
|
|
|
case 'string': |
29
|
|
|
return new StringLiteral($fieldName); |
30
|
|
|
case 'array': |
31
|
|
|
return new PrimitiveArray($fieldName); |
32
|
|
|
case 'object': |
33
|
|
|
return new AnotherValueObject($fieldName, get_class($object)); |
34
|
|
|
} |
35
|
|
|
throw new UnimplementedPhpDocBlockException($fieldName, new Types\Void_()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function fromReflectionTypeToTypeUtilInterface(string $fieldName, \ReflectionType $reflectionType): TypeUtilInterface |
39
|
|
|
{ |
40
|
|
|
$class = $reflectionType->getName(); |
|
|
|
|
41
|
|
|
switch ($class) { |
42
|
|
|
case 'bool': |
43
|
|
|
$object = new Boolean($fieldName); |
44
|
|
|
break; |
45
|
|
|
case 'int': |
46
|
|
|
$object = new Integer($fieldName); |
47
|
|
|
break; |
48
|
|
|
case 'string': |
49
|
|
|
$object = new StringLiteral($fieldName); |
50
|
|
|
break; |
51
|
|
|
case 'float': |
52
|
|
|
$object = new FloatingPoint($fieldName); |
53
|
|
|
break; |
54
|
|
|
case 'array': |
55
|
|
|
$object = new PrimitiveArray($fieldName); |
56
|
|
|
break; |
57
|
|
|
default: |
58
|
|
|
$object = new AnotherValueObject($fieldName, $class); |
59
|
|
|
} |
60
|
|
|
if ($reflectionType->allowsNull()) { |
61
|
|
|
return new Compound($fieldName, $object, new NullObject()); |
62
|
|
|
} |
63
|
|
|
return $object; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function fromTypeToTypeUtilInterface(string $fieldName, Type $type): TypeUtilInterface |
67
|
|
|
{ |
68
|
|
|
if ($type instanceof Types\Boolean) { |
69
|
|
|
return new Boolean($fieldName); |
70
|
|
|
} |
71
|
|
|
if ($type instanceof Types\Float_) { |
72
|
|
|
return new FloatingPoint($fieldName); |
73
|
|
|
} |
74
|
|
|
if ($type instanceof Types\Array_) { |
75
|
|
|
return new PrimitiveArray($fieldName); |
76
|
|
|
} |
77
|
|
|
if ($type instanceof Types\Object_) { |
78
|
|
|
$className = $type->getFqsen(); |
79
|
|
|
return new AnotherValueObject($fieldName, $className); |
80
|
|
|
} |
81
|
|
|
if ($type instanceof Types\Integer) { |
82
|
|
|
return new Integer($fieldName); |
83
|
|
|
} |
84
|
|
|
if ($type instanceof Types\String_) { |
85
|
|
|
return new StringLiteral($fieldName); |
86
|
|
|
} |
87
|
|
|
if ($type instanceof Types\Mixed_) { |
88
|
|
|
return new MixedTypehint($fieldName); |
89
|
|
|
} |
90
|
|
|
if ($type instanceof Types\Null_) { |
91
|
|
|
return new NullObject(); |
92
|
|
|
} |
93
|
|
|
if ($type instanceof Types\Compound) { |
94
|
|
|
$compoundTypes = []; |
95
|
|
|
foreach ($type as $compoundItemType) { |
96
|
|
|
$compoundTypes[] = self::fromTypeToTypeUtilInterface($fieldName, $compoundItemType); |
97
|
|
|
} |
98
|
|
|
return new Compound($fieldName, ...$compoundTypes); |
99
|
|
|
} |
100
|
|
|
throw new UnimplementedPhpDocBlockException($fieldName, $type); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|