From 4babedb16921dbb9974eda405d106a46e3b12090 Mon Sep 17 00:00:00 2001 From: Roland Gaida Date: Wed, 7 Oct 2015 13:40:25 +0200 Subject: [PATCH] Updates README.md, adds funtion Client::getGroupMembers(), minor changes in requesting group details --- README.md | 9 +++++- src/Client.php | 88 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 32eddbf..298258f 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,10 @@ public function unlikeDirectMessage($other_user_id, $message_id) ##### Group methods ```php -public function getAllGroups($page=1, $per_page=10) +public function getAllGroups() +public function getGroups($page = 1, $per_page = 10) +public function getGroupByName($name) +public function getGroupById($group_id) public function getFormerGroups() public function createGroup($name, $description='', $image_url='', $share=false) public function getGroupDetails($group_id) @@ -52,12 +55,16 @@ public function getMyLikes($group_id) public function getMyHits($group_id) public function addMembersToGroup($group_id, array $members) public function getAddMembersToGroupResult($group_id, $results_id) +public function getGroupMembers($group_id) public function updateMyGroupMembership($group_id, $nickname) public function removeGroupMember($group_id, $user_id) public function getLatestGroupMessages($group_id, $limit=20) public function getGroupMessagesBefore($group_id, $message_id, $limit=20) public function getGroupMessagesAfter($group_id, $message_id, $limit=20) public function getGroupMessagesSince($group_id, $message_id, $limit=20) +public function getGroupNameById($id) +public function getGroupIdByName($name) +public function isMemberOfGroup($grp) public function sendGroupMessage($group_id, $text, array $attachments=array(), $source_guid=null) ``` diff --git a/src/Client.php b/src/Client.php index e90f741..79fb493 100644 --- a/src/Client.php +++ b/src/Client.php @@ -250,24 +250,6 @@ public function unlikeDirectMessage($other_user_id, $message_id) { // GROUP METHODS - /** - * Gets a group id by the group name - * - * @param string $name Group name - * - * @return mixed Group id or FALSE - */ - public function getGroupIdByName($name) { - $res = $this->getAllGroups(); - - if($res['meta']['code'] == self::HTTP_OK) - foreach($res['response'] as $group) { - if ($group['name'] == $name) return $group['id']; - } - - return FALSE; - } - /** * Checks if the authenticated user is a member * of a certain group @@ -289,20 +271,53 @@ public function isMemberOfGroup($grp) { return FALSE; } + function getGroupById($group_id) { + return $this->get("/groups/$group_id"); + } + + function getGroupByName($name) { + $res = $this->getAllGroups(); + + if($res['meta']['code'] == self::HTTP_OK) + foreach($res['response'] as $group) { + if ($group['name'] == $name) + return array( + 'meta' => $res['meta'], + 'response' => $group + ); + } + + return $res; + } + + /** + * Gets a group id by the group name + * + * @param string $name Group name + * + * @return mixed Group id or FALSE + */ + public function getGroupIdByName($name) { + $res = $this->getGroupByName($name); + + if($res['meta']['code'] == self::HTTP_OK) + return $res['response']['id']; + + return FALSE; + } + /** * Gets a group name by its id * - * @param int $id Group id + * @param int $group_id Group id * * @return mixed Group name or FALSE */ - public function getGroupNameById($id) { - $res = $this->getAllGroups(); + public function getGroupNameById($group_id) { + $res = $this->getGroupById($group_id); if($res['meta']['code'] == self::HTTP_OK) - foreach($res['response'] as $group) { - if ($group['id'] == $id) return $group['name']; - } + return $res['response']['name']; return FALSE; } @@ -366,14 +381,16 @@ public function createGroup($name, $description='', $image_url='', $share=false) } /** - * Retrieves a specific group + * Retrieves group details * - * @param string $group_id Group id + * @param string $group Group id or group name * * @return mixed */ - public function getGroupDetails($group_id) { - return $this->get("/groups/$group_id"); + public function getGroupDetails($group) { + if (is_numeric($group)) return $this->getGroupById($group); + if (is_string($group)) return $this->getGroupByName($group); + return array(); } /** @@ -562,6 +579,21 @@ public function updateMyGroupMembership($group_id, $nickname) { return $this->post("/groups/$group_id/memberships/update", $payload); } + /** + * Gets all members of a group + * + * @param int $group_id Group id + * + * @return array Group members or empty array + */ + public function getGroupMembers($group_id) { + $group = $this->getGroupDetails($group_id); + if (is_array($group['response']['members'])) + return $group['response']['members']; + + return array(); + } + /** * Removes a member (or yourself) from a group *