1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Method extends Member
19: {
20:
21: private $parameters = [];
22:
23:
24: private $uses = [];
25:
26:
27: private $body = '';
28:
29:
30: private $static = FALSE;
31:
32:
33: private $final = FALSE;
34:
35:
36: private $abstract = FALSE;
37:
38:
39: private $returnReference = FALSE;
40:
41:
42: private $variadic = FALSE;
43:
44:
45: private $namespace;
46:
47:
48: private $returnType;
49:
50:
51: private $returnNullable;
52:
53:
54: 55: 56:
57: public static function from($from)
58: {
59: return (new Factory)->fromFunctionReflection(
60: $from instanceof \ReflectionFunctionAbstract ? $from : Nette\Utils\Callback::toReflection($from)
61: );
62: }
63:
64:
65: 66: 67:
68: public function __construct($name = NULL)
69: {
70: $this->setName($name);
71: }
72:
73:
74: 75: 76:
77: public function __toString()
78: {
79: $parameters = [];
80: foreach ($this->parameters as $param) {
81: $variadic = $this->variadic && $param === end($this->parameters);
82: $hint = $param->getTypeHint();
83: $parameters[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '')
84: . ($param->isReference() ? '&' : '')
85: . ($variadic ? '...' : '')
86: . '$' . $param->getName()
87: . ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
88: }
89: $uses = [];
90: foreach ($this->uses as $param) {
91: $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
92: }
93:
94: return Helpers::formatDocComment($this->getComment() . "\n")
95: . ($this->abstract ? 'abstract ' : '')
96: . ($this->final ? 'final ' : '')
97: . ($this->getVisibility() ? $this->getVisibility() . ' ' : '')
98: . ($this->static ? 'static ' : '')
99: . 'function '
100: . ($this->returnReference ? '&' : '')
101: . $this->getName()
102: . '(' . implode(', ', $parameters) . ')'
103: . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
104: . ($this->returnType ? ': ' . ($this->returnNullable ? '?' : '')
105: . ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType) : '')
106: . ($this->abstract || $this->body === FALSE ? ';'
107: : ($this->getName() ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
108: }
109:
110:
111: 112: 113: 114:
115: public function setParameters(array $val)
116: {
117: $this->parameters = [];
118: foreach ($val as $v) {
119: if (!$v instanceof Parameter) {
120: throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].');
121: }
122: $this->parameters[$v->getName()] = $v;
123: }
124: return $this;
125: }
126:
127:
128: 129: 130:
131: public function getParameters()
132: {
133: return $this->parameters;
134: }
135:
136:
137: 138: 139: 140:
141: public function addParameter($name, $defaultValue = NULL)
142: {
143: $param = new Parameter($name);
144: if (func_num_args() > 1) {
145: $param->setOptional(TRUE)->setDefaultValue($defaultValue);
146: }
147: return $this->parameters[$name] = $param;
148: }
149:
150:
151: 152: 153:
154: public function setUses(array $val)
155: {
156: $this->uses = $val;
157: return $this;
158: }
159:
160:
161: 162: 163:
164: public function getUses()
165: {
166: return $this->uses;
167: }
168:
169:
170: 171: 172:
173: public function addUse($name)
174: {
175: return $this->uses[] = new Parameter($name);
176: }
177:
178:
179: 180: 181:
182: public function setBody($code, array $args = NULL)
183: {
184: $this->body = $args === NULL ? $code : Helpers::formatArgs($code, $args);
185: return $this;
186: }
187:
188:
189: 190: 191:
192: public function getBody()
193: {
194: return $this->body;
195: }
196:
197:
198: 199: 200:
201: public function addBody($code, array $args = NULL)
202: {
203: $this->body .= ($args === NULL ? $code : Helpers::formatArgs($code, $args)) . "\n";
204: return $this;
205: }
206:
207:
208: 209: 210: 211:
212: public function setStatic($val)
213: {
214: $this->static = (bool) $val;
215: return $this;
216: }
217:
218:
219: 220: 221:
222: public function isStatic()
223: {
224: return $this->static;
225: }
226:
227:
228: 229: 230: 231:
232: public function setFinal($val)
233: {
234: $this->final = (bool) $val;
235: return $this;
236: }
237:
238:
239: 240: 241:
242: public function isFinal()
243: {
244: return $this->final;
245: }
246:
247:
248: 249: 250: 251:
252: public function setAbstract($val)
253: {
254: $this->abstract = (bool) $val;
255: return $this;
256: }
257:
258:
259: 260: 261:
262: public function isAbstract()
263: {
264: return $this->abstract;
265: }
266:
267:
268: 269: 270: 271:
272: public function setReturnReference($val)
273: {
274: $this->returnReference = (bool) $val;
275: return $this;
276: }
277:
278:
279: 280: 281:
282: public function getReturnReference()
283: {
284: return $this->returnReference;
285: }
286:
287:
288: 289: 290: 291:
292: public function setReturnNullable($val)
293: {
294: $this->returnNullable = (bool) $val;
295: return $this;
296: }
297:
298:
299: 300: 301:
302: public function getReturnNullable()
303: {
304: return $this->returnNullable;
305: }
306:
307:
308: 309: 310: 311:
312: public function setVariadic($val)
313: {
314: $this->variadic = (bool) $val;
315: return $this;
316: }
317:
318:
319: 320: 321:
322: public function isVariadic()
323: {
324: return $this->variadic;
325: }
326:
327:
328: 329: 330:
331: public function setNamespace(PhpNamespace $val = NULL)
332: {
333: $this->namespace = $val;
334: return $this;
335: }
336:
337:
338: 339: 340: 341:
342: public function setReturnType($val)
343: {
344: $this->returnType = $val ? (string) $val : NULL;
345: return $this;
346: }
347:
348:
349: 350: 351:
352: public function getReturnType()
353: {
354: return $this->returnType;
355: }
356:
357: }
358: