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