1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
29: class Parameter extends Nette\Object
30: {
31:
32: private $name;
33:
34:
35: private $reference;
36:
37:
38: private $typeHint;
39:
40:
41: private $optional;
42:
43:
44: public $defaultValue;
45:
46:
47:
48: public static function from(\ReflectionParameter $from)
49: {
50: $param = new static;
51: $param->name = $from->getName();
52: $param->reference = $from->isPassedByReference();
53: try {
54: $param->typeHint = $from->isArray() ? 'array' : ($from->getClass() ? '\\' . $from->getClass()->getName() : '');
55: } catch (\ReflectionException $e) {
56: if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
57: $param->typeHint = '\\' . $m[1];
58: } else {
59: throw $e;
60: }
61: }
62: $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable();
63: $param->defaultValue = (PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable()) ? $from->getDefaultValue() : NULL;
64:
65: $namespace = $from->getDeclaringClass()->getNamespaceName();
66: $namespace = $namespace ? "\\$namespace\\" : "\\";
67: if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
68: $param->typeHint = substr($param->typeHint, strlen($namespace));
69: }
70: return $param;
71: }
72:
73: }
74: