id = (int) $data['id']; $this->path = (string) $data['path']; $this->method = (string) strtolower($data['method']); if ($data['header'] == 'null') { $this->header = null; } else { $klass = sprintf("%s\\%s", __NAMESPACE__, $data['header']); $this->header = new $klass; } if ($data['payload'] == 'null') { $this->payload = null; } else { $klass = sprintf("%s\\%s", __NAMESPACE__, $data['payload']); $this->payload = new $klass; } if ($data['response'] == 'null') { $this->response = null; } else { $klass = sprintf("%s\\%s", __NAMESPACE__, $data['response']); $this->response = new $klass; } $this->desc = (string) $data['desc']; } public function setHeader($data) { if ($this->header === null) { throw new ApiException(ErrorCode::PROTO_TRY_TO_SET_VALUE_ON_NULL); } if (is_array($data) || $data instanceof $this->header) { return $this->header->set($data); } return false; } public function setPayload($data) { if ($this->payload === null) { return false; } if (is_array($data) || $data instanceof $this->payload) { return $this->payload->set($data); } return $this; } public function setResponse($data, $value = null) { if ($this->response === null) { throw new ApiException(ErrorCode::PROTO_TRY_TO_SET_VALUE_ON_NULL); } if (is_array($data) || $data instanceof $this->response) { $this->response->set($data); } elseif (is_string($data)) { $this->response->set([$data => $value]); } return $this; } public function parse() { return $this->response->iterate(); } } final class Api { private static $api = [ '/api/token/get' => [ 'payload' => 'token_req_info', 'header' => 'app_info', 'id' => '2', 'path' => '/api/token/get', 'method' => 'post', 'response' => 'token_info', 'desc' => '获取服务端token', ], '/api/servers/list' => [ 'payload' => 'null', 'header' => 'app_info', 'id' => '1', 'path' => '/api/servers/list', 'method' => 'post', 'response' => 'service_list', 'desc' => '获取当前客户端可用服务端列表', ], '/api/token/refresh' => [ 'payload' => 'token_info', 'header' => 'app_info', 'id' => '3', 'path' => '/api/token/refresh', 'method' => 'post', 'response' => 'token_info', 'desc' => '刷新服务端token', ], ]; public static function get($path) { if (!isset(self::$api[$path])) { throw new ApiException(ErrorCode::PROTO_PATH_NOT_EXIST); } return new _Api(self::$api[$path]); } }