1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArgumentResolver\Argument; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\ClassUtils; |
6
|
|
|
|
7
|
|
|
class ArgumentDescriptor |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Get argument descriptions of a callable. |
11
|
|
|
* |
12
|
|
|
* @param $callable |
13
|
|
|
* |
14
|
|
|
* @return ArgumentDescriptions |
15
|
|
|
*/ |
16
|
|
|
public function getDescriptions($callable) |
17
|
|
|
{ |
18
|
|
|
$reflection = $this->getReflection($callable); |
19
|
|
|
$descriptions = new ArgumentDescriptions(); |
20
|
|
|
|
21
|
|
|
foreach ($reflection->getParameters() as $parameter) { |
22
|
|
|
$descriptions->add(new ArgumentDescription( |
23
|
|
|
$parameter->name, |
24
|
|
|
$parameter->getPosition(), |
25
|
|
|
$this->getParameterType($parameter), |
26
|
|
|
!$parameter->isOptional(), |
27
|
|
|
$parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null |
28
|
|
|
)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $descriptions; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $value |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getValueType($value) |
40
|
|
|
{ |
41
|
|
|
if (is_object($value)) { |
42
|
|
|
return $this->getObjectClass($value); |
43
|
|
|
} elseif (is_array($value)) { |
44
|
|
|
return ArgumentDescription::TYPE_ARRAY; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return ArgumentDescription::TYPE_SCALAR; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \ReflectionParameter $parameter |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
private function getParameterType(\ReflectionParameter $parameter) |
56
|
|
|
{ |
57
|
|
|
if (null !== ($class = $parameter->getClass())) { |
58
|
|
|
return $class->name; |
59
|
|
|
} elseif ($parameter->isArray()) { |
60
|
|
|
return ArgumentDescription::TYPE_ARRAY; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return ArgumentDescription::TYPE_SCALAR; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $callable |
68
|
|
|
* |
69
|
|
|
* @return \ReflectionFunctionAbstract |
70
|
|
|
*/ |
71
|
|
|
private function getReflection($callable) |
72
|
|
|
{ |
73
|
|
|
if ($callable instanceof \ReflectionFunctionAbstract) { |
74
|
|
|
return $callable; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!is_callable($callable)) { |
78
|
|
|
throw new \RuntimeException('Got a non-callable'); |
79
|
|
|
} elseif (is_array($callable)) { |
80
|
|
|
$reflectionClass = new \ReflectionClass($callable[0]); |
81
|
|
|
|
82
|
|
|
return $reflectionClass->getMethod($callable[1]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new \ReflectionFunction($callable); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get object class. |
90
|
|
|
* |
91
|
|
|
* It uses the Doctrine's `ClassUtils::getClass` method if exists, else the native `get_class` function. |
92
|
|
|
* |
93
|
|
|
* @param object $object |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function getObjectClass($object) |
98
|
|
|
{ |
99
|
|
|
return class_exists('Doctrine\Common\Util\ClassUtils') ? ClassUtils::getClass($object) : get_class($object); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|