|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hateoas\Representation\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use Hateoas\Configuration\Route; |
|
5
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
|
6
|
|
|
use Hateoas\Representation\PaginatedRepresentation; |
|
7
|
|
|
use Knp\Component\Pager\Pagination\SlidingPagination; |
|
8
|
|
|
|
|
9
|
|
View Code Duplication |
class KnpPaginatorFactory |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var |
|
13
|
|
|
*/ |
|
14
|
|
|
private $pageParameterName; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var |
|
18
|
|
|
*/ |
|
19
|
|
|
private $limitParameterName; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $pageParameterName |
|
24
|
|
|
* @param string $limitParameterName |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($pageParameterName = null, $limitParameterName = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->pageParameterName = $pageParameterName; |
|
29
|
|
|
$this->limitParameterName = $limitParameterName; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param SlidingPagination $slidingPagination |
|
34
|
|
|
* @param Route $route |
|
35
|
|
|
* @param null $inline |
|
36
|
|
|
* @return PaginatedRepresentation |
|
37
|
|
|
*/ |
|
38
|
|
|
public function createRepresentation(SlidingPagination $slidingPagination, Route $route, $inline = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if (null === $inline) { |
|
41
|
|
|
$inline = new CollectionRepresentation($slidingPagination->getItems()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return new PaginatedRepresentation( |
|
45
|
|
|
$inline, |
|
46
|
|
|
$route->getName(), |
|
47
|
|
|
$route->getParameters(), |
|
|
|
|
|
|
48
|
|
|
$slidingPagination->getCurrentPageNumber(), |
|
49
|
|
|
$slidingPagination->getItemNumberPerPage(), |
|
50
|
|
|
$slidingPagination->getPaginationData()['pageCount'], |
|
51
|
|
|
$this->getPageParameterName(), |
|
52
|
|
|
$this->getLimitParameterName(), |
|
53
|
|
|
$route->isAbsolute(), |
|
54
|
|
|
$slidingPagination->getTotalItemCount() |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getPageParameterName() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->pageParameterName; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getLimitParameterName() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->limitParameterName; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.