Skip to content

Commit

Permalink
Aliases for better eloquentability. (Fixes #10) (#14)
Browse files Browse the repository at this point in the history
* Added aliases for liking, blocking & following.
  • Loading branch information
rennokki authored Jul 21, 2018
1 parent 0f88679 commit cdff7c2
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ $user->following(Page::class)->count(); // 1, because it follows only Zuck's pag
On-demand, you can check if your model follows some other model:
```php
$user->isFollowing($friend);
$user->follows($friend); // alias
```

**Note: Following, unfollowing or checking if following models that do not correctly implement `CanBeFollowed` and `Followable` will always return `false` and such relation will never be made.**
Expand Down Expand Up @@ -165,6 +166,7 @@ $user->blockers(); // Users that block this user.
$user->blockers(Page::class); // Pages that block this user.

$user->isBlocking($page);
$user->blocks($page); // alias to isBlocking
```

### Filtering blocked models
Expand Down Expand Up @@ -225,4 +227,5 @@ $user->likers(); // Users that like this user.
$user->likers(Page::class); // Pages that like this user.

$user->isLiking($page);
$user->likes($page); // alias to isLiking
```
2 changes: 2 additions & 0 deletions src/Contracts/Blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public function blocking($model = null);

public function isBlocking($model);

public function blocks($model);

public function block($model);

public function unblock($model);
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Follower.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public function following($model = null);

public function isFollowing($model);

public function follows($model);

public function follow($model);

public function unfollow($model);
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Liker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public function liking($model = null);

public function isLiking($model);

public function likes($model);

public function like($model);

public function unlike($model);
Expand Down
5 changes: 5 additions & 0 deletions src/Traits/CanBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function isBlocking($model)
return (bool) ! is_null($this->blocking($model->getMorphClass())->find($model->getKey()));
}

public function blocks($model)
{
return $this->isBlocking($model);
}

public function block($model)
{
if (! $model instanceof Blocker && ! $model instanceof Blocking) {
Expand Down
5 changes: 5 additions & 0 deletions src/Traits/CanFollow.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function isFollowing($model)
return (bool) ! is_null($this->following($model->getMorphClass())->find($model->getKey()));
}

public function follows($model)
{
return $this->isFollowing($model);
}

public function follow($model)
{
if (! $model instanceof Follower && ! $model instanceof Following) {
Expand Down
5 changes: 5 additions & 0 deletions src/Traits/CanLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function isLiking($model)
return (bool) ! is_null($this->liking($model->getMorphClass())->find($model->getKey()));
}

public function likes($model)
{
return $this->isLiking($model);
}

public function like($model)
{
if (! $model instanceof Liker && ! $model instanceof Liking) {
Expand Down
11 changes: 11 additions & 0 deletions tests/BlockingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testNoImplements()
$this->assertFalse($this->user->block($this->simplePage));
$this->assertFalse($this->user->unblock($this->simplePage));
$this->assertFalse($this->user->isBlocking($this->simplePage));
$this->assertFalse($this->user->blocks($this->simplePage));
}

public function testNoBlockedOrBlocked()
Expand All @@ -49,13 +50,17 @@ public function testblockUser()

$this->assertFalse($this->user->block($this->user2));
$this->assertTrue($this->user->isBlocking($this->user2));
$this->assertTrue($this->user->blocks($this->user2));

$this->assertTrue($this->user2->block($this->user3));
$this->assertFalse($this->user2->block($this->user3));
$this->assertTrue($this->user2->isBlocking($this->user3));
$this->assertTrue($this->user2->blocks($this->user3));

$this->assertFalse($this->user->isBlocking($this->user3));
$this->assertFalse($this->user3->isBlocking($this->user2));
$this->assertFalse($this->user->blocks($this->user3));
$this->assertFalse($this->user3->blocks($this->user2));

$this->assertEquals($this->user->blocking()->count(), 1);
$this->assertEquals($this->user->blockers()->count(), 0);
Expand All @@ -72,6 +77,7 @@ public function testUnblockUser()
$this->assertTrue($this->user->block($this->user2));
$this->assertTrue($this->user->unblock($this->user2));
$this->assertFalse($this->user->isBlocking($this->user2));
$this->assertFalse($this->user->blocks($this->user2));

$this->assertEquals($this->user->blocking()->count(), 0);
$this->assertEquals($this->user->blockers()->count(), 0);
Expand All @@ -84,13 +90,17 @@ public function testblockOtherModel()
$this->assertTrue($this->user->block($this->page));
$this->assertFalse($this->user->block($this->page));
$this->assertTrue($this->user->isBlocking($this->page));
$this->assertTrue($this->user->blocks($this->page));

$this->assertTrue($this->user2->block($this->page));
$this->assertTrue($this->user3->block($this->page));

$this->assertFalse($this->page->isBlocking($this->user));
$this->assertFalse($this->page->isBlocking($this->user2));
$this->assertFalse($this->page->isBlocking($this->user3));
$this->assertFalse($this->page->blocks($this->user));
$this->assertFalse($this->page->blocks($this->user2));
$this->assertFalse($this->page->blocks($this->user3));

$this->assertEquals($this->page->blocking()->count(), 0);
$this->assertEquals($this->page->blockers()->count(), 0);
Expand Down Expand Up @@ -120,6 +130,7 @@ public function testUnblockOtherModel()
$this->assertTrue($this->user->block($this->page));
$this->assertTrue($this->user->unblock($this->page));
$this->assertFalse($this->user->isBlocking($this->page));
$this->assertFalse($this->user->blocks($this->page));

$this->assertEquals($this->user->blocking()->count(), 0);
$this->assertEquals($this->user->blockers()->count(), 0);
Expand Down
11 changes: 11 additions & 0 deletions tests/FollowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testNoImplements()
$this->assertFalse($this->user->follow($this->simplePage));
$this->assertFalse($this->user->unfollow($this->simplePage));
$this->assertFalse($this->user->isFollowing($this->simplePage));
$this->assertFalse($this->user->follows($this->simplePage));
}

public function testNoFollowersOrFollowing()
Expand All @@ -49,13 +50,17 @@ public function testFollowUser()

$this->assertFalse($this->user->follow($this->user2));
$this->assertTrue($this->user->isFollowing($this->user2));
$this->assertTrue($this->user->follows($this->user2));

$this->assertTrue($this->user2->follow($this->user3));
$this->assertFalse($this->user2->follow($this->user3));
$this->assertTrue($this->user2->isFollowing($this->user3));
$this->assertTrue($this->user2->follows($this->user3));

$this->assertFalse($this->user->isFollowing($this->user3));
$this->assertFalse($this->user3->isFollowing($this->user2));
$this->assertFalse($this->user->follows($this->user3));
$this->assertFalse($this->user3->follows($this->user2));

$this->assertEquals($this->user->following()->count(), 1);
$this->assertEquals($this->user->followers()->count(), 0);
Expand All @@ -72,6 +77,7 @@ public function testUnfollowUser()
$this->assertTrue($this->user->follow($this->user2));
$this->assertTrue($this->user->unfollow($this->user2));
$this->assertFalse($this->user->isFollowing($this->user2));
$this->assertFalse($this->user->follows($this->user2));

$this->assertEquals($this->user->following()->count(), 0);
$this->assertEquals($this->user->followers()->count(), 0);
Expand All @@ -84,13 +90,17 @@ public function testFollowOtherModel()
$this->assertTrue($this->user->follow($this->page));
$this->assertFalse($this->user->follow($this->page));
$this->assertTrue($this->user->isFollowing($this->page));
$this->assertTrue($this->user->follows($this->page));

$this->assertTrue($this->user2->follow($this->page));
$this->assertTrue($this->user3->follow($this->page));

$this->assertFalse($this->page->isFollowing($this->user));
$this->assertFalse($this->page->isFollowing($this->user2));
$this->assertFalse($this->page->isFollowing($this->user3));
$this->assertFalse($this->page->follows($this->user));
$this->assertFalse($this->page->follows($this->user2));
$this->assertFalse($this->page->follows($this->user3));

$this->assertEquals($this->page->following()->count(), 0);
$this->assertEquals($this->page->followers()->count(), 0);
Expand Down Expand Up @@ -120,6 +130,7 @@ public function testUnfollowOtherModel()
$this->assertTrue($this->user->follow($this->page));
$this->assertTrue($this->user->unfollow($this->page));
$this->assertFalse($this->user->isFollowing($this->page));
$this->assertFalse($this->user->follows($this->page));

$this->assertEquals($this->user->following()->count(), 0);
$this->assertEquals($this->user->followers()->count(), 0);
Expand Down
10 changes: 10 additions & 0 deletions tests/LikingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public function testLikeUser()

$this->assertFalse($this->user->like($this->user2));
$this->assertTrue($this->user->isLiking($this->user2));
$this->assertTrue($this->user->likes($this->user2));

$this->assertTrue($this->user2->like($this->user3));
$this->assertFalse($this->user2->like($this->user3));
$this->assertTrue($this->user2->isLiking($this->user3));
$this->assertTrue($this->user2->likes($this->user3));

$this->assertFalse($this->user->isLiking($this->user3));
$this->assertFalse($this->user3->isLiking($this->user2));
$this->assertFalse($this->user->likes($this->user3));
$this->assertFalse($this->user3->likes($this->user2));

$this->assertEquals($this->user->liking()->count(), 1);
$this->assertEquals($this->user->likers()->count(), 0);
Expand All @@ -72,6 +76,7 @@ public function testUnlikeUser()
$this->assertTrue($this->user->like($this->user2));
$this->assertTrue($this->user->unlike($this->user2));
$this->assertFalse($this->user->isLiking($this->user2));
$this->assertFalse($this->user->likes($this->user2));

$this->assertEquals($this->user->liking()->count(), 0);
$this->assertEquals($this->user->likers()->count(), 0);
Expand All @@ -84,13 +89,17 @@ public function testLikeOtherModel()
$this->assertTrue($this->user->like($this->page));
$this->assertFalse($this->user->like($this->page));
$this->assertTrue($this->user->isLiking($this->page));
$this->assertTrue($this->user->likes($this->page));

$this->assertTrue($this->user2->like($this->page));
$this->assertTrue($this->user3->like($this->page));

$this->assertFalse($this->page->isLiking($this->user));
$this->assertFalse($this->page->isLiking($this->user2));
$this->assertFalse($this->page->isLiking($this->user3));
$this->assertFalse($this->page->likes($this->user));
$this->assertFalse($this->page->likes($this->user2));
$this->assertFalse($this->page->likes($this->user3));

$this->assertEquals($this->page->liking()->count(), 0);
$this->assertEquals($this->page->likers()->count(), 0);
Expand Down Expand Up @@ -120,6 +129,7 @@ public function testUnlikeOtherModel()
$this->assertTrue($this->user->like($this->page));
$this->assertTrue($this->user->unlike($this->page));
$this->assertFalse($this->user->isLiking($this->page));
$this->assertFalse($this->user->likes($this->page));

$this->assertEquals($this->user->liking()->count(), 0);
$this->assertEquals($this->user->likers()->count(), 0);
Expand Down

0 comments on commit cdff7c2

Please sign in to comment.