File tree Expand file tree Collapse file tree 4 files changed +75
-3
lines changed Expand file tree Collapse file tree 4 files changed +75
-3
lines changed Original file line number Diff line number Diff line change 14
14
use Elastica \Query ;
15
15
use Elastica \SearchableInterface ;
16
16
17
- class ElasticaAdapter implements AdapterInterface
17
+ class ElasticaAdapter implements LazyAdapterInterface
18
18
{
19
19
/**
20
20
* @var Query
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Pagerfanta package.
5
+ *
6
+ * (c) Pablo Díez <[email protected] >
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Pagerfanta \Adapter ;
13
+
14
+ /**
15
+ * LazyAdapterInterface that marks adapter lazy.
16
+ *
17
+ * @author Konstantin Myakshin <[email protected] >
18
+ */
19
+ interface LazyAdapterInterface extends AdapterInterface
20
+ {
21
+
22
+ }
Original file line number Diff line number Diff line change 12
12
namespace Pagerfanta ;
13
13
14
14
use Pagerfanta \Adapter \AdapterInterface ;
15
+ use Pagerfanta \Adapter \LazyAdapterInterface ;
15
16
use Pagerfanta \Exception \LogicException ;
16
17
use Pagerfanta \Exception \NotBooleanException ;
17
18
use Pagerfanta \Exception \NotIntegerMaxPerPageException ;
@@ -225,7 +226,9 @@ private function filterCurrentPage($currentPage)
225
226
{
226
227
$ currentPage = $ this ->toInteger ($ currentPage );
227
228
$ this ->checkCurrentPage ($ currentPage );
228
- $ currentPage = $ this ->filterOutOfRangeCurrentPage ($ currentPage );
229
+ if (!$ this ->adapter instanceof LazyAdapterInterface) {
230
+ $ currentPage = $ this ->filterOutOfRangeCurrentPage ($ currentPage );
231
+ }
229
232
230
233
return $ currentPage ;
231
234
}
@@ -316,7 +319,18 @@ private function getCurrentPageResultsFromAdapter()
316
319
$ offset = $ this ->calculateOffsetForCurrentPageResults ();
317
320
$ length = $ this ->getMaxPerPage ();
318
321
319
- return $ this ->adapter ->getSlice ($ offset , $ length );
322
+ $ slice = $ this ->adapter ->getSlice ($ offset , $ length );
323
+
324
+ if ($ this ->adapter instanceof LazyAdapterInterface) {
325
+ $ page = $ this ->filterOutOfRangeCurrentPage ($ this ->getCurrentPage ());
326
+ if ($ page != $ this ->getCurrentPage ()) {
327
+ $ this ->setCurrentPage ($ page );
328
+
329
+ return $ this ->getCurrentPageResultsFromAdapter ();
330
+ }
331
+ }
332
+
333
+ return $ slice ;
320
334
}
321
335
322
336
private function calculateOffsetForCurrentPageResults ()
Original file line number Diff line number Diff line change @@ -671,6 +671,42 @@ public function testPagerfantaShouldImplementIteratorAggregateInterface()
671
671
$ this ->assertInstanceOf ('IteratorAggregate ' , $ this ->pagerfanta );
672
672
}
673
673
674
+ public function testLazyAdapterDoNotCallsGetNbResultsOnSetCurrentPage ()
675
+ {
676
+ $ adapter = $ this ->getMock ('Pagerfanta\Adapter\LazyAdapterInterface ' );
677
+
678
+ $ adapter
679
+ ->expects ($ this ->never ())
680
+ ->method ('getNbResults ' );
681
+
682
+ $ pagerfanta = new Pagerfanta ($ adapter );
683
+ $ pagerfanta ->setMaxPerPage (10 );
684
+ $ pagerfanta ->setCurrentPage (3 );
685
+ }
686
+
687
+ /**
688
+ * @expectedException Pagerfanta\Exception\OutOfRangeCurrentPageException
689
+ */
690
+ public function testLazyAdapterCallsGetNbResultsOnGetCurrentPageResults ()
691
+ {
692
+ $ adapter = $ this ->getMock ('Pagerfanta\Adapter\LazyAdapterInterface ' );
693
+
694
+ $ adapter
695
+ ->expects ($ this ->any ())
696
+ ->method ('getSlice ' )
697
+ ->will ($ this ->returnValue (range (0 , 9 )));
698
+
699
+ $ adapter
700
+ ->expects ($ this ->any ())
701
+ ->method ('getNbResults ' )
702
+ ->will ($ this ->returnValue (20 ));
703
+
704
+ $ pagerfanta = new Pagerfanta ($ adapter );
705
+ $ pagerfanta ->setMaxPerPage (10 );
706
+ $ pagerfanta ->setCurrentPage (3 );
707
+ $ pagerfanta ->getCurrentPageResults ();
708
+ }
709
+
674
710
private function assertResetCurrentPageResults ($ callback )
675
711
{
676
712
$ this ->setAdapterNbResultsAny (100 );
You can’t perform that action at this time.
0 commit comments