1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Adapter; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Exception\VariableNotFoundException; |
6
|
|
|
use Brendt\Stitcher\Factory\AdapterFactory; |
7
|
|
|
use Brendt\Stitcher\Site\Page; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The PaginationAdapter takes a page with a collection of entries and generates pagination for that collection. |
11
|
|
|
* |
12
|
|
|
* Sample configuration: |
13
|
|
|
* |
14
|
|
|
* /examples: |
15
|
|
|
* template: examples/overview |
16
|
|
|
* data: |
17
|
|
|
* collection: collection.yml |
18
|
|
|
* adapters: |
19
|
|
|
* pagination: |
20
|
|
|
* variable: collection |
21
|
|
|
* entriesPerPage: 4 |
22
|
|
|
*/ |
23
|
|
|
class PaginationAdapter extends AbstractAdapter |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Page $page |
28
|
|
|
* @param null $filter |
29
|
|
|
* |
30
|
|
|
* @return array|Page[] |
31
|
|
|
* @throws VariableNotFoundException |
32
|
|
|
*/ |
33
|
|
|
public function transformPage(Page $page, $filter = null) : array { |
34
|
|
|
$config = $page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER); |
35
|
|
|
|
36
|
|
|
if (!isset($config['variable'])) { |
37
|
|
|
return [$page]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$variable = $config['variable']; |
41
|
|
|
|
42
|
|
|
if (!$source = $page->getVariable($variable)) { |
43
|
|
|
throw new VariableNotFoundException("Variable \"{$variable}\" was not set as a data variable for page \"{$page->getId()}\""); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$pageId = rtrim($page->getId(), '/'); |
47
|
|
|
$entries = $this->getData($source); |
48
|
|
|
$entriesPerPage = isset($config['entriesPerPage']) ? $config['entriesPerPage'] : 10; |
49
|
|
|
$pageCount = (int) ceil(count($entries) / $entriesPerPage); |
50
|
|
|
|
51
|
|
|
$i = 0; |
52
|
|
|
$result = []; |
53
|
|
|
|
54
|
|
|
while ($i < $pageCount) { |
55
|
|
|
$pageEntries = array_splice($entries, 0, $entriesPerPage); |
56
|
|
|
$pageIndex = $i + 1; |
57
|
|
|
|
58
|
|
|
if ($filter && $pageIndex !== (int) $filter) { |
59
|
|
|
$i += 1; |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$url = "{$pageId}/page-{$pageIndex}"; |
64
|
|
|
$pagination = $this->createPagination($pageId, $pageIndex, $pageCount, $entries); |
65
|
|
|
$entriesPage = clone $page; |
66
|
|
|
$entriesPage |
67
|
|
|
->removeAdapter(AdapterFactory::PAGINATION_ADAPTER) |
68
|
|
|
->setVariableValue($variable, $pageEntries) |
69
|
|
|
->setVariableIsParsed($variable) |
70
|
|
|
->setVariableValue('pagination', $pagination) |
71
|
|
|
->setVariableIsParsed('pagination') |
72
|
|
|
->setId($url); |
73
|
|
|
|
74
|
|
|
$result[$url] = $entriesPage; |
75
|
|
|
$i += 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($firstPage = reset($result)) { |
79
|
|
|
$mainPage = clone $firstPage; |
80
|
|
|
$mainPage->setId($pageId); |
81
|
|
|
$result[$pageId] = $mainPage; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a pagination array. |
89
|
|
|
* |
90
|
|
|
* @param $pageId |
91
|
|
|
* @param $pageIndex |
92
|
|
|
* @param $pageCount |
93
|
|
|
* @param $entries |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function createPagination($pageId, $pageIndex, $pageCount, $entries) { |
98
|
|
|
$next = count($entries) ? $pageIndex + 1 : null; |
99
|
|
|
$nextUrl = $next ? "{$pageId}/page-{$next}" : null; |
100
|
|
|
$previous = $pageIndex > 1 ? $pageIndex - 1 : null; |
101
|
|
|
$previousUrl = $previous ? "{$pageId}/page-{$previous}" : null; |
102
|
|
|
|
103
|
|
|
return [ |
104
|
|
|
'current' => $pageIndex, |
105
|
|
|
'previous' => $previous ? [ |
106
|
|
|
'url' => $previousUrl, |
107
|
|
|
'index' => $previous, |
108
|
|
|
] : null, |
109
|
|
|
'next' => $next ? [ |
110
|
|
|
'url' => $nextUrl, |
111
|
|
|
'index' => $next, |
112
|
|
|
] : null, |
113
|
|
|
'pages' => $pageCount, |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|