|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace SWP\Bundle\ContentBundle\Factory; |
|
15
|
|
|
|
|
16
|
|
|
use Hateoas\Configuration\Route; |
|
17
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
|
18
|
|
|
use Hateoas\Representation\PaginatedRepresentation; |
|
19
|
|
|
use Knp\Component\Pager\Pagination\AbstractPagination; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
|
|
22
|
|
|
class KnpPaginatorRepresentationFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $pageParameterName; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $limitParameterName; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $pageParameterName |
|
36
|
|
|
* @param string $limitParameterName |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($pageParameterName = null, $limitParameterName = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->pageParameterName = $pageParameterName; |
|
41
|
|
|
$this->limitParameterName = $limitParameterName; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param AbstractPagination $pagination |
|
46
|
|
|
* @param Request $request |
|
47
|
|
|
* @param string $collectionName |
|
48
|
|
|
* |
|
49
|
|
|
* @return PaginatedRepresentation |
|
50
|
|
|
*/ |
|
51
|
|
|
public function createRepresentation(AbstractPagination $pagination, Request $request, $collectionName = '_items') |
|
52
|
|
|
{ |
|
53
|
|
|
$route = new Route($request->get('_route'), $request->query->all()); |
|
54
|
|
|
$routeParameters = is_array($route->getParameters()) ? $route->getParameters() : []; |
|
55
|
|
|
$numberOfPages = 1; |
|
56
|
|
|
if ($pagination->getTotalItemCount() > 0 && $pagination->getItemNumberPerPage() > 0) { |
|
57
|
|
|
$numberOfPages = intval(ceil($pagination->getTotalItemCount() / $pagination->getItemNumberPerPage())); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new PaginatedRepresentation( |
|
61
|
|
|
new CollectionRepresentation($pagination->getItems(), $collectionName), |
|
62
|
|
|
$route->getName(), |
|
63
|
|
|
$routeParameters, |
|
64
|
|
|
$pagination->getCurrentPageNumber(), |
|
65
|
|
|
$pagination->getItemNumberPerPage(), |
|
66
|
|
|
$numberOfPages, |
|
67
|
|
|
$this->getPageParameterName(), |
|
68
|
|
|
$this->getLimitParameterName(), |
|
69
|
|
|
$route->isAbsolute(), |
|
70
|
|
|
$pagination->getTotalItemCount() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getPageParameterName() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->pageParameterName; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getLimitParameterName() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->limitParameterName; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|