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