diff --git a/src/Client.php b/src/Client.php index ff120f2..3d080ef 100644 --- a/src/Client.php +++ b/src/Client.php @@ -10,6 +10,7 @@ namespace ATehnix\VkClient; +use ATehnix\VkClient\Contracts\ClientInterface as ClientContract; use ATehnix\VkClient\Contracts\RequestInterface; use ATehnix\VkClient\Exceptions\VkException; use GuzzleHttp\Client as HttpClient; @@ -22,10 +23,10 @@ * * @package ATehnix\VkClient */ -class Client +class Client implements ClientContract { const API_URI = 'https://api.vk.com/method/'; - const API_VERSION = '5.53'; + const API_VERSION = '5.62'; const API_TIMEOUT = 30.0; /** @@ -62,7 +63,16 @@ class Client public function __construct($version = null, ClientInterface $http = null) { $this->version = $version ?: static::API_VERSION; - $this->http = $http ?: new HttpClient([ + $this->http = $this->resolveHttpClient($http); + } + + /** + * @param ClientInterface|null $http + * @return ClientInterface + */ + private function resolveHttpClient(ClientInterface $http = null) + { + return $http ?: new HttpClient([ 'base_uri' => static::API_URI, 'timeout' => static::API_TIMEOUT, 'http_errors' => false, @@ -98,6 +108,7 @@ public function setPassError($bool = true) /** * @param RequestInterface $request * @return array + * @throws \ATehnix\VkClient\Exceptions\VkException */ public function send(RequestInterface $request) { @@ -113,8 +124,9 @@ public function send(RequestInterface $request) * @param array $parameters * @param string|null $token * @return array + * @throws \ATehnix\VkClient\Exceptions\VkException */ - public function request($method, $parameters, $token = null) + public function request($method, $parameters = [], $token = null) { $options = $this->buildOptions($parameters, $token); $response = $this->http->request('POST', $method, $options); @@ -156,11 +168,19 @@ protected function getResponseData(ResponseInterface $response) } /** - * @param $data + * @param array|false $data * @throws VkException */ protected function checkErrors($data) { + if (json_last_error() !== JSON_ERROR_NONE) { + throw new VkException('Invalid VK response format: ' . json_last_error_msg()); + } + + if (!is_array($data)) { + throw new VkException('Invalid response format'); + } + if (isset($data['error'])) { throw self::toException($data['error']); } @@ -191,6 +211,7 @@ public static function toException($error) 15 => Exceptions\AccessDeniedVkException::class, ]; + /** @var \Exception|\Throwable $exception */ $exception = isset($map[$code]) ? $map[$code] : $map[0]; return new $exception($message, $code); diff --git a/src/Contracts/ClientInterface.php b/src/Contracts/ClientInterface.php new file mode 100644 index 0000000..506b8ad --- /dev/null +++ b/src/Contracts/ClientInterface.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ATehnix\VkClient\Contracts; + +use ATehnix\VkClient\Contracts\RequestInterface; +use ATehnix\VkClient\Exceptions\VkException; + +/** + * Interface ClientInterface + * + * @package ATehnix\VkClient\Contracts + */ +interface ClientInterface +{ + /** + * @param RequestInterface $request + * @return array + * @throws VkException + */ + public function send(RequestInterface $request); + + /** + * @param string $method + * @param array $parameters + * @param string|null $token + * @return array + * @throws VkException + */ + public function request($method, $parameters = [], $token = null); +}