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