1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Utils\PhpGenerator;
13:
14: use Nette;
15:
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class Method extends Nette\Object
32: {
33:
34: public $name;
35:
36:
37: public $parameters = array();
38:
39:
40: public $uses = array();
41:
42:
43: public $body;
44:
45:
46: public $static;
47:
48:
49: public $visibility;
50:
51:
52: public $final;
53:
54:
55: public $abstract;
56:
57:
58: public $returnReference;
59:
60:
61: public $documents = array();
62:
63:
64:
65: public function addParameter($name, $defaultValue = NULL)
66: {
67: $param = new Parameter;
68: if (func_num_args() > 1) {
69: $param->setOptional(TRUE)->setDefaultValue($defaultValue);
70: }
71: return $this->parameters[] = $param->setName($name);
72: }
73:
74:
75:
76: public function addUse($name)
77: {
78: $param = new Parameter;
79: return $this->uses[] = $param->setName($name);
80: }
81:
82:
83:
84: public function setBody($statement, array $args = NULL)
85: {
86: $this->body = func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement;
87: return $this;
88: }
89:
90:
91:
92: public function addBody($statement, array $args = NULL)
93: {
94: $this->body .= (func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement) . "\n";
95: return $this;
96: }
97:
98:
99: public function __call($name, $args)
100: {
101: return Nette\ObjectMixin::callProperty($this, $name, $args);
102: }
103:
104:
105:
106: public function __toString()
107: {
108: $parameters = array();
109: foreach ($this->parameters as $param) {
110: $parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '')
111: . ($param->reference ? '&' : '')
112: . '$' . $param->name
113: . ($param->optional ? ' = ' . Helpers::dump($param->defaultValue) : '');
114: }
115: $uses = array();
116: foreach ($this->uses as $param) {
117: $uses[] = ($param->reference ? '&' : '') . '$' . $param->name;
118: }
119: return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
120: . ($this->abstract ? 'abstract ' : '')
121: . ($this->final ? 'final ' : '')
122: . ($this->visibility ? $this->visibility . ' ' : '')
123: . ($this->static ? 'static ' : '')
124: . 'function'
125: . ($this->returnReference ? ' &' : '')
126: . ($this->name ? ' ' . $this->name : '')
127: . '(' . implode(', ', $parameters) . ')'
128: . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
129: . ($this->abstract || $this->body === FALSE ? ';'
130: : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(trim($this->body), 1) . "\n}");
131: }
132:
133: }
134: