1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NMethodReflection extends ReflectionMethod
22: {
23:
24: 25: 26: 27: 28:
29: public static function from($class, $method)
30: {
31: return new self(is_object($class) ? get_class($class) : $class, $method);
32: }
33:
34:
35:
36: 37: 38:
39: public function getDefaultParameters()
40: {
41: return self::buildDefaultParameters(parent::getParameters());
42: }
43:
44:
45:
46: 47: 48: 49: 50: 51:
52: public function invokeNamedArgs($object, $args)
53: {
54: return $this->invokeArgs($object, self::combineArgs($this->getDefaultParameters(), $args));
55: }
56:
57:
58:
59: 60: 61:
62: public function toCallback()
63: {
64: return new NCallback(parent::getDeclaringClass()->getName(), $this->getName());
65: }
66:
67:
68:
69: public function __toString()
70: {
71: return 'Method ' . parent::getDeclaringClass()->getName() . '::' . $this->getName() . '()';
72: }
73:
74:
75:
76:
77:
78:
79:
80: 81: 82:
83: public function getDeclaringClass()
84: {
85: return new NClassReflection(parent::getDeclaringClass()->getName());
86: }
87:
88:
89:
90: 91: 92:
93: public function getPrototype()
94: {
95: $prototype = parent::getPrototype();
96: return new NMethodReflection($prototype->getDeclaringClass()->getName(), $prototype->getName());
97: }
98:
99:
100:
101: 102: 103:
104: public function getExtension()
105: {
106: return ($name = $this->getExtensionName()) ? new NExtensionReflection($name) : NULL;
107: }
108:
109:
110:
111: public function getParameters()
112: {
113: $me = array(parent::getDeclaringClass()->getName(), $this->getName());
114: foreach ($res = parent::getParameters() as $key => $val) {
115: $res[$key] = new NParameterReflection($me, $val->getName());
116: }
117: return $res;
118: }
119:
120:
121:
122:
123:
124:
125:
126: 127: 128: 129: 130:
131: public function hasAnnotation($name)
132: {
133: $res = NAnnotationsParser::getAll($this);
134: return !empty($res[$name]);
135: }
136:
137:
138:
139: 140: 141: 142: 143:
144: public function getAnnotation($name)
145: {
146: $res = NAnnotationsParser::getAll($this);
147: return isset($res[$name]) ? end($res[$name]) : NULL;
148: }
149:
150:
151:
152: 153: 154: 155:
156: public function getAnnotations()
157: {
158: return NAnnotationsParser::getAll($this);
159: }
160:
161:
162:
163: 164: 165: 166:
167: public function getDescription()
168: {
169: return $this->getAnnotation('description');
170: }
171:
172:
173:
174:
175:
176:
177:
178: 179: 180:
181: public function getReflection()
182: {
183: return new NClassReflection($this);
184: }
185:
186:
187:
188: public function __call($name, $args)
189: {
190: return NObjectMixin::call($this, $name, $args);
191: }
192:
193:
194:
195: public function &__get($name)
196: {
197: return NObjectMixin::get($this, $name);
198: }
199:
200:
201:
202: public function __set($name, $value)
203: {
204: return NObjectMixin::set($this, $name, $value);
205: }
206:
207:
208:
209: public function __isset($name)
210: {
211: return NObjectMixin::has($this, $name);
212: }
213:
214:
215:
216: public function __unset($name)
217: {
218: NObjectMixin::remove($this, $name);
219: }
220:
221:
222:
223:
224:
225:
226:
227:
228: public static function buildDefaultParameters($params)
229: {
230: $res = array();
231: foreach ($params as $param) {
232: $res[$param->getName()] = $param->isDefaultValueAvailable()
233: ? $param->getDefaultValue()
234: : NULL;
235:
236: if ($param->isArray()) {
237: settype($res[$param->getName()], 'array');
238: }
239: }
240: return $res;
241: }
242:
243:
244:
245:
246: public static function combineArgs($params, $args)
247: {
248: $res = array();
249: $i = 0;
250: foreach ($params as $name => $def) {
251: if (isset($args[$name])) {
252: $val = $args[$name];
253: if ($def !== NULL) {
254: settype($val, gettype($def));
255: }
256: $res[$i++] = $val;
257: } else {
258: $res[$i++] = $def;
259: }
260: }
261: return $res;
262: }
263:
264: }
265: