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