1 | <?php |
||
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) |
|
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() |
|
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | 8 | public function getLimitParameterName() |
|
102 | } |
||
103 |