From 15b9acc632011bca78f20246d951aa046313905f Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 1 Apr 2022 10:22:23 +0800 Subject: [PATCH] Fixed #50 --- .gitignore | 1 + src/Traits/Liker.php | 14 ++++++++++---- tests/FeatureTest.php | 31 ++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index f67ddb1..2f76896 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ composer.lock .php_cs.cache .phpunit.result.cache .phplint-cache +.php-cs-fixer.cache diff --git a/src/Traits/Liker.php b/src/Traits/Liker.php index 69254c6..2a64549 100644 --- a/src/Traits/Liker.php +++ b/src/Traits/Liker.php @@ -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 @@ -125,18 +127,22 @@ 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) { @@ -144,7 +150,7 @@ public function attachLikeStatus($likeables, callable $resolver = null) $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)); } }); diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index bcc4932..4d3bf16 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -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']); }