/**
  * 用于客户端验证的中间件。
  *
  * @param Service        $api       被访问的 API。
  * @param Validator|null $validator 验证器。
  *
  * @return callable
  */
 public static function validation(Service $api, Validator $validator = null)
 {
     $validator = $validator ?: new \Validator();
     return function (callable $handler) use($api, $validator) {
         return function (CommandInterface $command, RequestInterface $request = null) use($api, $validator, $handler) {
             $operation = $api->getOperation($command->getName());
             $validator->validate($command->getName(), $operation->getInput(), $command->toArray());
             return $handler($command, $request);
         };
     };
 }
Exemple #2
0
 /**
  * 为指定操作获取一个结果分页器。
  *
  * @param string $name 迭代器使用的操作名称。
  * @param array  $args 每个命令所使用的命令参数。
  *
  * @return ResultPaginator
  * @throws \UnexpectedValueException 如果迭代器配置无效。
  */
 public function getPaginator($name, array $args = [])
 {
     $config = $this->api->getPaginatorConfig($name);
     return new ResultPaginator($this, $name, $args, $config);
 }
 public static function applyApiProvider(callable $value, array &$arguments, HandlerList $list)
 {
     $api = new Service(ApiProvider::resolve($value, 'api', $arguments['service'], $arguments['version']), $value);
     $arguments['api'] = $api;
     // todo add endpoint
     $arguments['serializer'] = Service::createSerializer($api, $arguments['endpoint']);
     // todo createParser
     $arguments['parser'] = Service::createParser($api);
     // todo get protocol
     $arguments['errorParser'] = Service::createErrorParser($api->getProtocol());
     $list->prependBuild(Middleware::requestBuilder($arguments['serializer']), 'builder');
 }