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