1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://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|NULL */
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($from->getName());
40: $param->reference = $from->isPassedByReference();
41: if (PHP_VERSION_ID >= 70000) {
42: $param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
43: } elseif ($from->isArray()) {
44: $param->typeHint = 'array';
45: } elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
46: $param->typeHint = 'callable';
47: } else {
48: try {
49: $param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL;
50: } catch (\ReflectionException $e) {
51: if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
52: $param->typeHint = $m[1];
53: } else {
54: throw $e;
55: }
56: }
57: }
58: $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable();
59: $param->defaultValue = (PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable()) ? $from->getDefaultValue() : NULL;
60: return $param;
61: }
62:
63:
64: /**
65: * @param string without $
66: */
67: public function __construct($name = '')
68: {
69: $this->setName($name);
70: }
71:
72:
73: /**
74: * @param string without $
75: * @return self
76: */
77: public function setName($name)
78: {
79: $this->name = (string) $name;
80: return $this;
81: }
82:
83:
84: /**
85: * @return string
86: */
87: public function getName()
88: {
89: return $this->name;
90: }
91:
92:
93: /**
94: * @param bool
95: * @return self
96: */
97: public function setReference($state = TRUE)
98: {
99: $this->reference = (bool) $state;
100: return $this;
101: }
102:
103:
104: /**
105: * @return bool
106: */
107: public function isReference()
108: {
109: return $this->reference;
110: }
111:
112:
113: /**
114: * @param string|NULL
115: * @return self
116: */
117: public function setTypeHint($hint)
118: {
119: $this->typeHint = $hint ? (string) $hint : NULL;
120: return $this;
121: }
122:
123:
124: /**
125: * @return string|NULL
126: */
127: public function getTypeHint()
128: {
129: return $this->typeHint;
130: }
131:
132:
133: /**
134: * @param bool
135: * @return self
136: */
137: public function setOptional($state = TRUE)
138: {
139: $this->optional = (bool) $state;
140: return $this;
141: }
142:
143:
144: /**
145: * @return bool
146: */
147: public function isOptional()
148: {
149: return $this->optional;
150: }
151:
152:
153: /**
154: * @return self
155: */
156: public function setDefaultValue($val)
157: {
158: $this->defaultValue = $val;
159: return $this;
160: }
161:
162:
163: /**
164: * @return mixed
165: */
166: public function getDefaultValue()
167: {
168: return $this->defaultValue;
169: }
170:
171: }
172: