Skip to content

Commit 1218be8

Browse files
authored
Add ability to use simplePaginate with Laravel Scout. (#443)
Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent bcc1ee3 commit 1218be8

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

src/Builder.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,43 @@ public function get()
257257
* @param int $perPage
258258
* @param string $pageName
259259
* @param int|null $page
260+
* @return \Illuminate\Contracts\Pagination\Paginator
261+
*/
262+
public function simplePaginate($perPage = null, $pageName = 'page', $page = null)
263+
{
264+
$engine = $this->engine();
265+
266+
$page = $page ?: Paginator::resolveCurrentPage($pageName);
267+
268+
$perPage = $perPage ?: $this->model->getPerPage();
269+
270+
$results = $this->model->newCollection($engine->map(
271+
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
272+
)->all());
273+
274+
$total = $engine->getTotalCount($rawResults);
275+
276+
$hasMorePages = ($perPage * $page) < $engine->getTotalCount($rawResults);
277+
278+
$paginator = Container::getInstance()->makeWith(Paginator::class, [
279+
'items' => $results,
280+
'perPage' => $perPage,
281+
'currentPage' => $page,
282+
'options' => [
283+
'path' => Paginator::resolveCurrentPath(),
284+
'pageName' => $pageName,
285+
],
286+
])->hasMorePagesWhen($hasMorePages);
287+
288+
return $paginator->appends('query', $this->query);
289+
}
290+
291+
/**
292+
* Paginate the given query into a paginator.
293+
*
294+
* @param int $perPage
295+
* @param string $pageName
296+
* @param int|null $page
260297
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
261298
*/
262299
public function paginate($perPage = null, $pageName = 'page', $page = null)

tests/BuilderTest.php

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,89 @@ public function test_pagination_correctly_handles_paginated_results()
3030
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());
3131

3232
$engine->shouldReceive('paginate');
33-
$engine->shouldReceive('map')->andReturn($results = Collection::make([new stdClass]));
34-
$engine->shouldReceive('getTotalCount');
33+
$engine->shouldReceive('map')->andReturn($results = Collection::times(15, function () {
34+
return new stdClass;
35+
}));
36+
$engine->shouldReceive('getTotalCount')->andReturn(16);
3537

3638
$model->shouldReceive('newCollection')->andReturn($results);
3739

38-
$builder->paginate();
40+
$paginated = $builder->paginate();
41+
42+
$this->assertSame($results->all(), $paginated->items());
43+
$this->assertSame(16, $paginated->total());
44+
$this->assertSame(15, $paginated->perPage());
45+
$this->assertSame(1, $paginated->currentPage());
46+
$this->assertSame([
47+
'path' => 'http://localhost/foo',
48+
'pageName' => 'page',
49+
], $paginated->getOptions());
50+
}
51+
52+
public function test_simple_pagination_correctly_handles_paginated_results()
53+
{
54+
Paginator::currentPageResolver(function () {
55+
return 1;
56+
});
57+
Paginator::currentPathResolver(function () {
58+
return 'http://localhost/foo';
59+
});
60+
61+
$builder = new Builder($model = m::mock(), 'zonda');
62+
$model->shouldReceive('getPerPage')->andReturn(15);
63+
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());
64+
65+
$engine->shouldReceive('paginate');
66+
$engine->shouldReceive('map')->andReturn($results = Collection::times(15, function () {
67+
return new stdClass;
68+
}));
69+
$engine->shouldReceive('getTotalCount')->andReturn(16);
70+
71+
$model->shouldReceive('newCollection')->andReturn($results);
72+
73+
$paginated = $builder->simplePaginate();
74+
75+
$this->assertSame($results->all(), $paginated->items());
76+
$this->assertTrue($paginated->hasMorePages());
77+
$this->assertSame(15, $paginated->perPage());
78+
$this->assertSame(1, $paginated->currentPage());
79+
$this->assertSame([
80+
'path' => 'http://localhost/foo',
81+
'pageName' => 'page',
82+
], $paginated->getOptions());
83+
}
84+
85+
public function test_simple_pagination_correctly_handles_paginated_results_without_more_pages()
86+
{
87+
Paginator::currentPageResolver(function () {
88+
return 1;
89+
});
90+
Paginator::currentPathResolver(function () {
91+
return 'http://localhost/foo';
92+
});
93+
94+
$builder = new Builder($model = m::mock(), 'zonda');
95+
$model->shouldReceive('getPerPage')->andReturn(15);
96+
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());
97+
98+
$engine->shouldReceive('paginate');
99+
$engine->shouldReceive('map')->andReturn($results = Collection::times(10, function () {
100+
return new stdClass;
101+
}));
102+
$engine->shouldReceive('getTotalCount')->andReturn(10);
103+
104+
$model->shouldReceive('newCollection')->andReturn($results);
105+
106+
$paginated = $builder->simplePaginate();
107+
108+
$this->assertSame($results->all(), $paginated->items());
109+
$this->assertFalse($paginated->hasMorePages());
110+
$this->assertSame(15, $paginated->perPage());
111+
$this->assertSame(1, $paginated->currentPage());
112+
$this->assertSame([
113+
'path' => 'http://localhost/foo',
114+
'pageName' => 'page',
115+
], $paginated->getOptions());
39116
}
40117

41118
public function test_macroable()

0 commit comments

Comments
 (0)