1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette;
11:
12:
13: /**
14: * Method parameter description.
15: */
16: class Parameter extends Nette\Object
17: {
18: /** @var string */
19: private $name;
20:
21: /** @var bool */
22: private $reference = FALSE;
23:
24: /** @var string */
25: private $typeHint;
26:
27: /** @var bool */
28: private $optional = FALSE;
29:
30: /** @var mixed */
31: public $defaultValue;
32:
33:
34: /**
35: * @return self
36: */
37: public static function from(\ReflectionParameter $from)
38: {
39: $param = new static;
40: $param->name = $from->getName();
41: $param->reference = $from->isPassedByReference();
42: try {
43: $param->typeHint = $from->isArray() ? 'array' : ($from->getClass() ? '\\' . $from->getClass()->getName() : '');
44: } catch (\ReflectionException $e) {
45: if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
46: $param->typeHint = '\\' . $m[1];
47: } else {
48: throw $e;
49: }
50: }
51: $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable();
52: $param->defaultValue = (PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable()) ? $from->getDefaultValue() : NULL;
53:
54: $namespace = $from->getDeclaringClass()->getNamespaceName();
55: $namespace = $namespace ? "\\$namespace\\" : '\\';
56: if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
57: $param->typeHint = substr($param->typeHint, strlen($namespace));
58: }
59: return $param;
60: }
61:
62:
63: /**
64: * @param string without $
65: * @return self
66: */
67: public function setName($name)
68: {
69: $this->name = (string) $name;
70: return $this;
71: }
72:
73:
74: /**
75: * @return string
76: */
77: public function getName()
78: {
79: return $this->name;
80: }
81:
82:
83: /**
84: * @param bool
85: * @return self
86: */
87: public function setReference($state = TRUE)
88: {
89: $this->reference = (bool) $state;
90: return $this;
91: }
92:
93:
94: /**
95: * @return bool
96: */
97: public function isReference()
98: {
99: return $this->reference;
100: }
101:
102:
103: /**
104: * @param string
105: * @return self
106: */
107: public function setTypeHint($hint)
108: {
109: $this->typeHint = (string) $hint;
110: return $this;
111: }
112:
113:
114: /**
115: * @return string
116: */
117: public function getTypeHint()
118: {
119: return $this->typeHint;
120: }
121:
122:
123: /**
124: * @param bool
125: * @return self
126: */
127: public function setOptional($state = TRUE)
128: {
129: $this->optional = (bool) $state;
130: return $this;
131: }
132:
133:
134: /**
135: * @return bool
136: */
137: public function isOptional()
138: {
139: return $this->optional;
140: }
141:
142:
143: /**
144: * @return self
145: */
146: public function setDefaultValue($val)
147: {
148: $this->defaultValue = $val;
149: return $this;
150: }
151:
152:
153: /**
154: * @return mixed
155: */
156: public function getDefaultValue()
157: {
158: return $this->defaultValue;
159: }
160:
161: }
162: