예제 #1
0
 /**
  * @param object           $controller
  * @param ReflectionMethod $method
  * @param RequestParams    $requestParams
  *
  * @throws JsonRpcException
  *
  * @return mixed
  */
 private function executeMethod($controller, ReflectionMethod $method, RequestParams $requestParams = null)
 {
     $params = null;
     $areParamsNamed = null;
     if ($requestParams) {
         $params = $requestParams->all() ?: null;
         if ($params) {
             $areParamsNamed = $requestParams->areParamsNamed();
         }
     }
     $args = [];
     foreach ($method->getParameters() as $parameter) {
         $class = $parameter->getClass();
         if ($class) {
             if ($class->name === RequestParams::class) {
                 $args[] = $requestParams;
             } else {
                 if ($class->isSubclassOf(ParamsValidator::class)) {
                     /* @var ParamsValidator $object */
                     $args[] = $object = $class->newInstance($requestParams->all(), $requestParams->areParamsNamed());
                     $object->setContainer($this->container);
                     $object->validate();
                 } else {
                     try {
                         $args[] = $this->container->make($class->name);
                     } catch (ReflectionException $e) {
                         throw new InternalErrorException($e->getMessage(), 0, $e);
                     }
                 }
             }
         } else {
             if ($params !== null) {
                 /* @var array $params */
                 if ($areParamsNamed) {
                     $name = $parameter->getName();
                     if (array_key_exists($name, $params)) {
                         $args[] = $params[$name];
                         unset($params[$name]);
                         continue;
                     }
                 } else {
                     if (count($params)) {
                         $args[] = array_shift($params);
                         continue;
                     }
                 }
             }
             try {
                 $args[] = $parameter->getDefaultValue();
             } catch (ReflectionException $e) {
                 throw new InvalidParamsException("\"{$parameter->getName()}\" is required", 0, $e);
             }
         }
     }
     return $method->invokeArgs($controller, $args);
 }
예제 #2
0
 public function __construct(array $params, $areParamsNamed)
 {
     if ($this->convertToArray) {
         $params = json_decode(json_encode($params), true);
     }
     $p = $params;
     if (!$areParamsNamed && count($this->names) > 0) {
         $p = [];
         foreach ($this->names as $key => $param) {
             if (is_numeric($key)) {
                 $key = $param;
                 $param = null;
             }
             $p[$key] = count($params) > 0 ? array_shift($params) : $param;
         }
         $areParamsNamed = true;
     }
     parent::__construct($p, $areParamsNamed);
 }
예제 #3
0
 /**
  * @param object $requestData
  *
  * @throws Exception\JsonRpcException
  *
  * @return Request
  */
 public function createRequest($requestData)
 {
     if (!is_object($requestData)) {
         throw new Exception\BadRequestException();
     }
     if (!isset($requestData->jsonrpc) || $requestData->jsonrpc !== '2.0') {
         throw new Exception\BadRequestException();
     }
     if (empty($requestData->method) || !is_string($requestData->method)) {
         throw new Exception\BadRequestException();
     }
     $params = null;
     if (!empty($requestData->params)) {
         if (is_array($requestData->params)) {
             $params = RequestParams::constructPositional($requestData->params);
         } elseif (is_object($requestData->params)) {
             $params = RequestParams::constructNamed((array) $requestData->params);
         } else {
             throw new Exception\BadRequestException();
         }
     }
     return new Request($requestData->method, $params, isset($requestData->id) ? $requestData->id : null);
 }