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 PhpMethod extends Object
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 PhpParameter;
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:
76: public function addUse($name)
77: {
78: $param = new PhpParameter;
79: return $this->uses[] = $param->setName($name);
80: }
81:
82:
83:
84:
85: public function setBody($statement, array $args = NULL)
86: {
87: $this->body = func_num_args() > 1 ? PhpHelpers::formatArgs($statement, $args) : $statement;
88: return $this;
89: }
90:
91:
92:
93:
94: public function addBody($statement, array $args = NULL)
95: {
96: $this->body .= (func_num_args() > 1 ? PhpHelpers::formatArgs($statement, $args) : $statement) . "\n";
97: return $this;
98: }
99:
100:
101:
102: public function __call($name, $args)
103: {
104: return ObjectMixin::callProperty($this, $name, $args);
105: }
106:
107:
108:
109:
110: public function __toString()
111: {
112: $parameters = array();
113: foreach ($this->parameters as $param) {
114: $parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '')
115: . ($param->reference ? '&' : '')
116: . '$' . $param->name
117: . ($param->optional ? ' = ' . PhpHelpers::dump($param->defaultValue) : '');
118: }
119: $uses = array();
120: foreach ($this->uses as $param) {
121: $uses[] = ($param->reference ? '&' : '') . '$' . $param->name;
122: }
123: return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
124: . ($this->abstract ? 'abstract ' : '')
125: . ($this->final ? 'final ' : '')
126: . ($this->visibility ? $this->visibility . ' ' : '')
127: . ($this->static ? 'static ' : '')
128: . 'function'
129: . ($this->returnReference ? ' &' : '')
130: . ($this->name ? ' ' . $this->name : '')
131: . '(' . implode(', ', $parameters) . ')'
132: . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
133: . ($this->abstract || $this->body === FALSE ? ';'
134: : ($this->name ? "\n" : ' ') . "{\n" . Strings::indent(trim($this->body), 1) . "\n}");
135: }
136:
137: }
138: