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