Skip to content

Commit

Permalink
Add method to get groups for a specific friend
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaynesov committed Oct 12, 2016
1 parent 208f687 commit fd36a45
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ $user->ungroupFriend($friend, 'family');
$user->ungroupFriend($friend);
```

#### Get groups for specific friend
```php
$user->getGroupsFor($friend);
```

#### Get the number of Friends in specific group
```php
$user->getFriendsCount($group_name);
Expand Down
24 changes: 24 additions & 0 deletions src/Traits/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,30 @@ public function getFriendsCount($group = '')
return $friendsCount;
}


/**
* Get groups for Friend
*
* @param Model $model
* @return array
*/
public function getGroupsFor(Model $model) {

$result = [];
$friendship = $this->getFriendship($model);
if(!empty($friendship)) {
$groups = $friendship->groups()
->where('friend_id', $model->getKey())
->where('friend_type', $model->getMorphClass())
->get();
if(false === $groups->isEmpty())
$result = $groups->pluck('group_slug')->all();
}

return $result;

}

/**
* @param Model $recipient
*
Expand Down
30 changes: 30 additions & 0 deletions tests/FriendshipsGroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,35 @@ public function it_returns_user_friends_by_group_per_page()
$this->containsOnlyInstancesOf(\App\User::class, $sender->getFriends(0, 'acquaintances'));
}

/** @test */
public function returns_groups_for_specific_friend() {

$sender = createUser();
$recipients = createUser([], 2);
$groupsAvailable = config('friendships.groups', []);

foreach ($recipients as $recipient) {
$sender->befriend($recipient);
$recipient->acceptFriendRequest($sender);
}

$sender->groupFriend($recipients[0], 'acquaintances');
$sender->groupFriend($recipients[0], 'close_friends');

$sender->groupFriend($recipients[1], 'family');

$recipients[0]->groupFriend($sender, 'close_friends');

$rec1 = $sender->getGroupsFor($recipients[1]);

$this->assertCount(2, $sender->getGroupsFor($recipients[0]));
$this->assertCount(1, $recipients[0]->getGroupsFor($sender));
$this->assertCount(0, $recipients[1]->getGroupsFor($sender));

$this->assertInternalType('array', $rec1);
$this->assertContains($rec1[0], $groupsAvailable);

}


}

0 comments on commit fd36a45

Please sign in to comment.