|
1
|
|
|
<?php |
|
2
|
|
|
namespace rtens\domin\reflection\types; |
|
3
|
|
|
|
|
4
|
|
|
use watoki\reflect\ClassResolver; |
|
5
|
|
|
use watoki\reflect\Type; |
|
6
|
|
|
use watoki\reflect\type\StringType; |
|
7
|
|
|
|
|
8
|
|
|
class TypeFactory extends \watoki\reflect\TypeFactory { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @param string $hint |
|
12
|
|
|
* @param \ReflectionClass $class |
|
13
|
|
|
* @return Type |
|
14
|
|
|
*/ |
|
15
|
|
|
public function fromTypeHint($hint, \ReflectionClass $class) { |
|
16
|
|
|
$resolver = new ClassResolver($class); |
|
17
|
|
|
|
|
18
|
|
|
if (strtolower(substr($hint, -3)) == '-id') { |
|
19
|
|
|
$target = $resolver->resolve(substr($hint, 0, -3)); |
|
20
|
|
|
return new IdentifierType($target, new StringType()); |
|
21
|
|
|
|
|
22
|
|
|
} else if (strpos($hint, '::') && substr($hint, -1) == '*') { |
|
23
|
|
|
list($container, $constant) = explode('::', substr($hint, 0, -1)); |
|
24
|
|
|
if ($container == 'self') { |
|
25
|
|
|
$reflection = $class; |
|
26
|
|
|
} else { |
|
27
|
|
|
$reflection = new \ReflectionClass($resolver->resolve($container)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$options = []; |
|
31
|
|
|
foreach ($reflection->getConstants() as $name => $value) { |
|
32
|
|
|
if (substr($name, 0, strlen($constant)) == $constant) { |
|
33
|
|
|
$options[$value] = ucfirst($value); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return new EnumerationType($options, new StringType()); |
|
38
|
|
|
|
|
39
|
|
|
} else if (preg_match('#\[\d+;\d+(;\d+)?\]#', $hint)) { |
|
40
|
|
|
|
|
41
|
|
|
$parts = explode(';', substr($hint, 1, -1)); |
|
42
|
|
|
$min = array_shift($parts); |
|
43
|
|
|
$max = array_shift($parts); |
|
44
|
|
|
$step = $parts ? array_shift($parts) : 1; |
|
45
|
|
|
|
|
46
|
|
|
return new RangeType($min, $max, $step); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return parent::fromTypeHint($hint, $class); |
|
50
|
|
|
} |
|
51
|
|
|
} |