From 16ee969bdb2b8e6428a2ddeb54df2f328e16ab0b Mon Sep 17 00:00:00 2001 From: trogwar Date: Thu, 30 Jun 2016 07:23:24 +0300 Subject: [PATCH] Implement two basic VKAPI methods: `users.get` and `friends.get` --- src/Vkontakte.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/Vkontakte.php b/src/Vkontakte.php index 0f1fae7..7c4a037 100644 --- a/src/Vkontakte.php +++ b/src/Vkontakte.php @@ -173,4 +173,63 @@ class Vkontakte extends AbstractProvider return new User($response, $response['id']); } + // ========== API methods ========== + + /** + * @see https://vk.com/dev/users.get + * + * @param integer[] $ids + * @param AccessToken|null $token Current user if empty + * @param array $params + * + * @return User[] + */ + public function usersGet(array $ids = [], AccessToken $token = null, array $params = []) + { + if (empty($ids) && !$token) + throw new \InvalidArgumentException('Some of parameters usersIds OR access_token are required'); + + $default = [ + 'user_ids' => implode(',', $ids), + 'fields' => $this->userFields, + 'access_token' => $token ? $token->getToken() : null, + 'v' => $this->version, + ]; + $params = array_merge($default, $params); + $query = $this->buildQueryString($params); + $url = "$this->baseUri/users.get?$query"; + + $response = $this->getResponse($this->createRequest(static::METHOD_GET, $url, $token, []))['response']; + $users = !empty($response['items']) ? $response['items'] : $response; + $array2user = function ($userData) { return new User($userData); }; + + return array_map($array2user, $users); + } + /** + * @see https://vk.com/dev/friends.get + * + * @param integer $userId + * @param AccessToken|null $token + * @param array $params + * + * @return User[] + */ + public function friendsGet($userId, AccessToken $token = null, array $params = []) + { + $default = [ + 'user_id' => $userId, + 'fields' => $this->userFields, + 'access_token' => $token ? $token->getToken() : null, + 'v' => $this->version, + ]; + $params = array_merge($default, $params); + $query = $this->buildQueryString($params); + $url = "$this->baseUri/friends.get?$query"; + + $response = $this->getResponse($this->createRequest(static::METHOD_GET, $url, $token, []))['response']; + $friends = !empty($response['items']) ? $response['items'] : $response; + $array2friend = function ($friendData) { return new User($friendData); }; + + return array_map($array2friend, $friends); + } }