|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
6
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory; |
|
7
|
|
|
use Mado\QueryBundle\Objects\PagerAdapter; |
|
8
|
|
|
use Mado\QueryBundle\Objects\PagerfantaBuilder; |
|
9
|
|
|
use Mado\QueryBundle\Queries\QueryBuilderOptions; |
|
10
|
|
|
|
|
11
|
|
|
class Pager |
|
12
|
|
|
{ |
|
13
|
|
|
private const LIMIT = 10; |
|
14
|
|
|
|
|
15
|
|
|
private const PAGE = 1; |
|
16
|
|
|
|
|
17
|
|
|
private const LIFETIME = 600; |
|
18
|
|
|
|
|
19
|
|
|
private $pagerFactory; |
|
20
|
|
|
|
|
21
|
|
|
private $router; |
|
22
|
|
|
|
|
23
|
|
|
private $pagerAdapter; |
|
24
|
|
|
|
|
25
|
|
|
private $pagerfantaBuilder; |
|
26
|
|
|
|
|
27
|
2 |
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->setPaferfantaFactory(new PagerfantaFactory()); |
|
30
|
2 |
|
$this->setRouter(new Router()); |
|
31
|
2 |
|
$this->setPagerAdapter(new PagerAdapter()); |
|
32
|
2 |
|
$this->setPagerfantaBuilder(new PagerfantaBuilder()); |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public function setPaferfantaFactory(PagerfantaFactory $pagerFactory) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->pagerFactory = $pagerFactory; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function setRouter(Router $router) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->router = $router; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function setPagerAdapter($pagerAdapter) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$this->pagerAdapter = $pagerAdapter; |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function setPagerfantaBuilder(PagerfantaBuilder $pagerfantaBuilder) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$this->pagerfantaBuilder = $pagerfantaBuilder; |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function paginateResults ( |
|
56
|
|
|
QueryBuilder $queryBuilder, |
|
57
|
|
|
QueryBuilderOptions $queryOptions, |
|
58
|
|
|
$routeName, |
|
59
|
|
|
$useResultCache |
|
60
|
|
|
) { |
|
61
|
2 |
|
$limit = $queryOptions->get('limit', self::LIMIT); |
|
62
|
2 |
|
$page = $queryOptions->get('page', self::PAGE); |
|
63
|
|
|
|
|
64
|
2 |
|
$this->pagerAdapter->initPagerAdapter($queryBuilder); |
|
65
|
|
|
|
|
66
|
2 |
|
$query = $this->pagerAdapter->getQuery(); |
|
67
|
2 |
|
if (isset($useResultCache) && $useResultCache) { |
|
68
|
1 |
|
$query->useResultCache(true, self::LIFETIME); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
$pager = $this->pagerfantaBuilder->create( |
|
72
|
2 |
|
$this->pagerAdapter->getAdapter(), |
|
73
|
2 |
|
$limit, |
|
74
|
2 |
|
$page |
|
75
|
|
|
); |
|
76
|
2 |
|
$route = $this->router->createRouter($queryOptions, $routeName); |
|
77
|
|
|
|
|
78
|
2 |
|
return $this->pagerFactory->createRepresentation($pager, $route); |
|
79
|
|
|
} |
|
80
|
|
|
} |