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