Skip to content

Commit

Permalink
Add cursorPaginate support.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 1, 2022
1 parent 3ee7a1a commit 804751c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/Traits/Favoriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Overtrue\LaravelFavorite\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;

/**
* @property \Illuminate\Database\Eloquent\Collection $favorites
Expand Down Expand Up @@ -70,26 +73,30 @@ public function attachFavoriteStatus($favoriteables, callable $resolver = null)
$favoriteables = $favoriteables->getCollection();
break;
case $favoriteables instanceof Paginator:
case $favoriteables instanceof CursorPaginator:
$favoriteables = \collect($favoriteables->items());
break;
case $favoriteables instanceof LazyCollection:
$favoriteables = \collect($favoriteables->all());
break;
case \is_array($favoriteables):
$favoriteables = \collect($favoriteables);
$toArray = true;
break;
}

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

$favorited = $this->favorites()->get()->keyBy(function ($item) {
return \sprintf('%s-%s', $item->favoriteable_type, $item->favoriteable_id);
return \sprintf('%s:%s', $item->favoriteable_type, $item->favoriteable_id);
});

$favoriteables->map(function ($favoriteable) use ($favorited, $resolver) {
$resolver = $resolver ?? fn ($m) => $m;
$favoriteable = $resolver($favoriteable);

if ($favoriteable && \in_array(Favoriteable::class, \class_uses_recursive($favoriteable))) {
$key = \sprintf('%s-%s', $favoriteable->getMorphClass(), $favoriteable->getKey());
$key = \sprintf('%s:%s', $favoriteable->getMorphClass(), $favoriteable->getKey());
$favoriteable->setAttribute('has_favorited', $favorited->has($key));
}
});
Expand Down
34 changes: 28 additions & 6 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,32 +185,54 @@ public function test_favoriter_can_attach_favorite_status_to_votable_collection(
$user->favorite($post1);
$user->favorite($post2);

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

$user->attachFavoriteStatus($posts);

$posts = $posts->toArray();

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

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

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

// paginator
$posts = Post::oldest('id')->paginate();
$user->attachFavoriteStatus($posts);
$posts = $posts->toArray()['data'];

$this->assertTrue($posts[0]['has_favorited']);
$this->assertTrue($posts[1]['has_favorited']);
$this->assertFalse($posts[2]['has_favorited']);

// cursor paginator
$posts = Post::oldest('id')->cursorPaginate();
$user->attachFavoriteStatus($posts);
$posts = $posts->toArray()['data'];

$this->assertTrue($posts[0]['has_favorited']);
$this->assertTrue($posts[1]['has_favorited']);
$this->assertFalse($posts[2]['has_favorited']);

// cursor lazy collection
$posts = Post::oldest('id')->cursor();
$posts = $user->attachFavoriteStatus($posts);
$posts = $posts->toArray();

$this->assertTrue($posts[0]['has_favorited']);
$this->assertTrue($posts[1]['has_favorited']);
$this->assertFalse($posts[2]['has_favorited']);

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

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

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

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

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

0 comments on commit 804751c

Please sign in to comment.