1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Common Component. |
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
|
|
|
|
15
|
|
|
namespace SWP\Component\Common\Factory; |
16
|
|
|
|
17
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
18
|
|
|
use Hateoas\Configuration\Route; |
19
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
20
|
|
|
use Hateoas\Representation\PaginatedRepresentation; |
21
|
|
|
use Knp\Component\Pager\Pagination\AbstractPagination; |
22
|
|
|
use SWP\Component\Common\Pagination\PaginationInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
|
25
|
|
|
class KnpPaginatorRepresentationFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $pageParameterName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $limitParameterName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $pageParameterName |
39
|
|
|
* @param string $limitParameterName |
40
|
|
|
*/ |
41
|
8 |
|
public function __construct($pageParameterName = PaginationInterface::PAGE_PARAMETER_NAME, $limitParameterName = PaginationInterface::LIMIT_PARAMETER_NAME) |
42
|
|
|
{ |
43
|
8 |
|
$this->pageParameterName = $pageParameterName; |
44
|
8 |
|
$this->limitParameterName = $limitParameterName; |
45
|
8 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param AbstractPagination $pagination |
49
|
|
|
* @param Request $request |
50
|
|
|
* @param string $collectionName |
51
|
|
|
* |
52
|
|
|
* @return PaginatedRepresentation |
53
|
|
|
*/ |
54
|
8 |
|
public function createRepresentation(AbstractPagination $pagination, Request $request, $collectionName = '_items') |
55
|
|
|
{ |
56
|
8 |
|
$route = new Route($request->get('_route', 'homepage'), $request->query->all()); |
57
|
8 |
|
$routeParameters = is_array($route->getParameters()) ? $route->getParameters() : []; |
58
|
8 |
|
$numberOfPages = 1; |
59
|
8 |
|
if ($pagination->getTotalItemCount() > 0 && $pagination->getItemNumberPerPage() > 0) { |
60
|
7 |
|
$numberOfPages = intval(ceil($pagination->getTotalItemCount() / $pagination->getItemNumberPerPage())); |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
$items = $pagination->getItems(); |
64
|
|
|
switch (true) { |
65
|
8 |
|
case $items instanceof ArrayCollection: |
66
|
|
|
$items = $items->toArray(); |
67
|
|
|
break; |
68
|
8 |
|
case $items instanceof \ArrayObject: |
69
|
|
|
$items = $items->getArrayCopy(); |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
return new PaginatedRepresentation( |
74
|
8 |
|
new CollectionRepresentation(array_values($items), $collectionName), |
75
|
8 |
|
$route->getName(), |
76
|
|
|
$routeParameters, |
77
|
8 |
|
$pagination->getCurrentPageNumber(), |
78
|
8 |
|
$pagination->getItemNumberPerPage(), |
79
|
|
|
$numberOfPages, |
80
|
8 |
|
$this->getPageParameterName(), |
81
|
8 |
|
$this->getLimitParameterName(), |
82
|
8 |
|
$route->isAbsolute(), |
83
|
8 |
|
$pagination->getTotalItemCount() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
8 |
|
public function getPageParameterName() |
91
|
|
|
{ |
92
|
8 |
|
return $this->pageParameterName; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
8 |
|
public function getLimitParameterName() |
99
|
|
|
{ |
100
|
8 |
|
return $this->limitParameterName; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|