Skip to content

Commit

Permalink
Fixed #50
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 1, 2022
1 parent a2611bc commit 15b9acc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ composer.lock
.php_cs.cache
.phpunit.result.cache
.phplint-cache
.php-cs-fixer.cache
14 changes: 10 additions & 4 deletions src/Traits/Liker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Overtrue\LaravelLike\Traits;

use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use Overtrue\LaravelLike\Like;

trait Liker
Expand Down Expand Up @@ -125,26 +127,30 @@ public function attachLikeStatus($likeables, callable $resolver = null)
$likeables = $likeables->getCollection();
break;
case $likeables instanceof Paginator:
case $likeables instanceof CursorPaginator:
$likeables = \collect($likeables->items());
break;
case $likeables instanceof LazyCollection:
$likeables = \collect(\iterator_to_array($likeables->getIterator()));
break;
case \is_array($likeables):
$likeables = \collect($likeables);
$toArray = true;
break;
}

\abort_if(!($likeables instanceof Collection), 422, 'Invalid $likeables type.');
\abort_if(!($likeables instanceof Enumerable), 422, 'Invalid $likeables type');

$liked = $this->likes()->get()->keyBy(function ($item) {
return \sprintf('%s-%s', $item->likeable_type, $item->likeable_id);
return \sprintf('%s:%s', $item->likeable_type, $item->likeable_id);
});

$likeables->map(function ($likeable) use ($liked, $resolver) {
$resolver = $resolver ?? fn ($m) => $m;
$likeable = $resolver($likeable);

if ($likeable && \in_array(Likeable::class, \class_uses_recursive($likeable))) {
$key = \sprintf('%s-%s', $likeable->getMorphClass(), $likeable->getKey());
$key = \sprintf('%s:%s', $likeable->getMorphClass(), $likeable->getKey());
$likeable->setAttribute('has_liked', $liked->has($key));
}
});
Expand Down
31 changes: 24 additions & 7 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,31 +183,48 @@ public function test_liker_can_attach_like_status_to_votable_collection()
$user->like($post2);

$posts = Post::oldest('id')->get();

$user->attachLikeStatus($posts);

$posts = $posts->toArray();

// user has up liked post1
$this->assertTrue($posts[0]['has_liked']);

// user has down liked post2
$this->assertTrue($posts[1]['has_liked']);

// user hasn't liked post3
$this->assertFalse($posts[2]['has_liked']);

// paginator
$posts = Post::oldest('id')->paginate();
$user->attachLikeStatus($posts);
$posts = $posts->toArray()['data'];
$this->assertTrue($posts[0]['has_liked']);
$this->assertTrue($posts[1]['has_liked']);
$this->assertFalse($posts[2]['has_liked']);

// cursor paginator
$posts = Post::oldest('id')->cursorPaginate();
$user->attachLikeStatus($posts);
$posts = $posts->toArray()['data'];
$this->assertTrue($posts[0]['has_liked']);
$this->assertTrue($posts[1]['has_liked']);
$this->assertFalse($posts[2]['has_liked']);

// cursor
$posts = Post::oldest('id')->cursor();
$posts = $user->attachLikeStatus($posts);
$posts = $posts->toArray();
$this->assertTrue($posts[0]['has_liked']);
$this->assertTrue($posts[1]['has_liked']);
$this->assertFalse($posts[2]['has_liked']);

// custom resolver
$posts = [['post' => $post1], ['post' => $post2], ['post' => $post3]];

$posts = $user->attachLikeStatus($posts, fn ($i) => $i['post']);

// user has up liked post1
$this->assertTrue($posts[0]['post']['has_liked']);

// user has down liked post2
$this->assertTrue($posts[1]['post']['has_liked']);

// user hasn't liked post3
$this->assertFalse($posts[2]['post']['has_liked']);
}
Expand Down

0 comments on commit 15b9acc

Please sign in to comment.