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
17: {
18: use Nette\SmartObject;
19:
20: /** @var string */
21: private $name = '';
22:
23: /** @var bool */
24: private $reference = FALSE;
25:
26: /** @var string|NULL */
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($from->getName());
42: $param->reference = $from->isPassedByReference();
43: if (PHP_VERSION_ID >= 70000) {
44: $param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
45: } elseif ($from->isArray() || $from->isCallable()) {
46: $param->typeHint = $from->isArray() ? 'array' : '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 = $from->isDefaultValueAvailable();
59: $param->defaultValue = $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: /** @deprecated */
74: public function setName($name)
75: {
76: $this->name = (string) $name;
77: return $this;
78: }
79:
80:
81: /**
82: * @return string
83: */
84: public function getName()
85: {
86: return $this->name;
87: }
88:
89:
90: /**
91: * @param bool
92: * @return self
93: */
94: public function setReference($state = TRUE)
95: {
96: $this->reference = (bool) $state;
97: return $this;
98: }
99:
100:
101: /**
102: * @return bool
103: */
104: public function isReference()
105: {
106: return $this->reference;
107: }
108:
109:
110: /**
111: * @param string|NULL
112: * @return self
113: */
114: public function setTypeHint($hint)
115: {
116: $this->typeHint = $hint ? (string) $hint : NULL;
117: return $this;
118: }
119:
120:
121: /**
122: * @return string|NULL
123: */
124: public function getTypeHint()
125: {
126: return $this->typeHint;
127: }
128:
129:
130: /**
131: * @param bool
132: * @return self
133: */
134: public function setOptional($state = TRUE)
135: {
136: $this->optional = (bool) $state;
137: return $this;
138: }
139:
140:
141: /**
142: * @return bool
143: */
144: public function isOptional()
145: {
146: return $this->optional;
147: }
148:
149:
150: /**
151: * @return self
152: */
153: public function setDefaultValue($val)
154: {
155: $this->defaultValue = $val;
156: return $this;
157: }
158:
159:
160: /**
161: * @return mixed
162: */
163: public function getDefaultValue()
164: {
165: return $this->defaultValue;
166: }
167:
168: }
169: