11<?php namespace web \rest ;
22
33use io \streams \{InputStream , Streams };
4- use lang \IllegalArgumentException ;
5- use lang \reflect \ TargetInvocationException ;
4+ use lang \reflection \{ Method , TargetException } ;
5+ use lang \{ IllegalArgumentException , Reflection , Type } ;
66use web \Request ;
77
88class Delegate {
@@ -31,67 +31,66 @@ static function __static() {
3131 * Creates a new delegate
3232 *
3333 * @param object $instance
34- * @param lang.reflect .Method $method
34+ * @param string| lang.reflection .Method $method
3535 * @param string $source Default source
3636 */
3737 public function __construct ($ instance , $ method , $ source ) {
3838 $ this ->instance = $ instance ;
39- $ this ->method = $ method ;
40- foreach ($ method ->getParameters () as $ param ) {
39+ $ this ->method = $ method instanceof Method ? $ method : Reflection:: type ( $ instance )-> method ( $ method ) ;
40+ foreach ($ this -> method ->parameters () as $ param ) {
4141
4242 // Source explicitely set by annotation
43- foreach ($ param ->getAnnotations () as $ source => $ name ) {
44- if (isset ( self ::$ SOURCES [$ source ]) ) {
45- $ this ->param ($ param , $ name ?? $ param ->getName (), $ source );
43+ foreach ($ param ->annotations () as $ annotation ) {
44+ if ($ accessor = self ::$ SOURCES [$ annotation -> name ()] ?? null ) {
45+ $ this ->param ($ param , $ name ?? $ param ->name (), $ accessor );
4646 continue 2 ;
4747 }
4848 }
4949
5050 // Source derived from parameter type
51- $ type = $ param ->getType ();
52- if (' var ' === $ type-> getName () ) {
51+ $ type = $ param ->constraint ()-> type ();
52+ if (Type:: $ VAR === $ type ) {
5353 // NOOP
5454 } else if ($ type ->isAssignableFrom (InputStream::class)) {
5555 $ source = 'stream ' ;
5656 } else if ($ type ->isAssignableFrom (Request::class)) {
5757 $ source = 'request ' ;
5858 }
59- $ this ->param ($ param , $ param ->getName (), $ source );
59+ $ this ->param ($ param , $ param ->name (), self :: $ SOURCES [ $ source] );
6060 }
6161 }
6262
6363 /**
64- * Adds parameter request reader for a given parameter
64+ * Adds parameter request accessor for a given parameter
6565 *
66- * @param lang.reflect .Parameter $param
66+ * @param lang.reflection .Parameter $param
6767 * @param string $name
68- * @param function(web.Request, web.rest.format.EntityFormat, string): var $source
68+ * @param function(web.Request, web.rest.format.EntityFormat, string): var $accessor
6969 * @return void
70+ * @throws lang.IllegalArgumentException
7071 */
71- private function param ($ param , $ name , $ source ) {
72- $ extract = self ::$ SOURCES [$ source ];
73-
74- if ($ param ->isOptional ()) {
75- $ default = $ param ->getDefaultValue ();
76- $ read = function ($ req , $ format ) use ($ extract , $ name , $ default ) {
77- return $ extract ($ req , $ format , $ name ) ?? $ default ;
72+ private function param ($ param , $ name , $ accessor ) {
73+ if ($ param ->optional ()) {
74+ $ default = $ param ->default ();
75+ $ read = function ($ req , $ format ) use ($ accessor , $ name , $ default ) {
76+ return $ accessor ($ req , $ format , $ name ) ?? $ default ;
7877 };
7978 } else {
80- $ read = function ($ req , $ format ) use ($ extract , $ name ) {
81- if (null === ($ value = $ extract ($ req , $ format , $ name ))) {
79+ $ read = function ($ req , $ format ) use ($ accessor , $ name ) {
80+ if (null === ($ value = $ accessor ($ req , $ format , $ name ))) {
8281 throw new IllegalArgumentException ('Missing argument ' .$ name );
8382 }
8483 return $ value ;
8584 };
8685 }
87- $ this ->params [$ name ]= ['type ' => $ param ->getType (), 'read ' => $ read ];
86+ $ this ->params [$ name ]= ['type ' => $ param ->constraint ()-> type (), 'read ' => $ read ];
8887 }
8988
9089 /** @return string */
91- public function name () { return nameof ($ this ->instance ).':: ' .$ this ->method ->getName (); }
90+ public function name () { return nameof ($ this ->instance ).':: ' .$ this ->method ->name (); }
9291
93- /** @return [:var] */
94- public function annotations () { return $ this ->method ->getAnnotations (); }
92+ /** @return lang.reflection.Annotations */
93+ public function annotations () { return $ this ->method ->annotations (); }
9594
9695 /** @return [:var] */
9796 public function params () { return $ this ->params ; }
@@ -106,7 +105,7 @@ public function params() { return $this->params; }
106105 public function invoke ($ args ) {
107106 try {
108107 return $ this ->method ->invoke ($ this ->instance , $ args );
109- } catch (TargetInvocationException $ e ) {
108+ } catch (TargetException $ e ) {
110109 throw $ e ->getCause ();
111110 }
112111 }
0 commit comments