PagingInfo.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\persistence;
12 
13 /**
14  * PagingInfo contains information about a paged list.
15  *
16  * @author ingo herwig <ingo@wemove.com>
17  */
18 class PagingInfo {
19 
20  const SIZE_INFINITE = -1;
21 
22  private $pageSize = 10;
23  private $page = 1;
24  private $offset = 0;
25  private $totalCount = 0;
26  private $ignoreTotalCount = false;
27 
28  /**
29  * Creates a PagingInfo object. The ignoreTotalCount parameter may be
30  * set to true, if the count is to be ignored. This may speed up loading
31  * of objects, because an extra count query may be omitted.
32  * @param $pageSize The pageSize (PagingInfo::SIZE_INFINITE to set no page size)
33  * @param $ignoreTotalCount Boolean whether this instance ignores the
34  * total count or not (optional, default: _false_)
35  */
36  public function __construct($pageSize, $ignoreTotalCount=false) {
37  $this->pageSize = intval($pageSize);
38  if ($this->pageSize == self::SIZE_INFINITE) {
39  $this->pageSize = PHP_INT_MAX;
40  }
41  $this->ignoreTotalCount = $ignoreTotalCount;
42  }
43 
44  /**
45  * Set the number of list items.
46  * @param $totalCount The number of list items.
47  */
48  public function setTotalCount($totalCount) {
49  $this->totalCount = intval($totalCount);
50  }
51 
52  /**
53  * Get the number of list items.
54  * @return Number
55  */
56  public function getTotalCount() {
57  return $this->totalCount;
58  }
59 
60  /**
61  * Set the current page (1-based) (also sets the offset).
62  * @param $page The current page.
63  */
64  public function setPage($page) {
65  $this->page = intval($page);
66  $this->offset = ($page - 1) * $this->pageSize;
67  }
68 
69  /**
70  * Get the current page (1-based).
71  * @return Number
72  */
73  public function getPage() {
74  return $this->page;
75  }
76 
77  /**
78  * Get the size of a pages.
79  * @return Number
80  */
81  public function getPageSize() {
82  return $this->pageSize;
83  }
84 
85  /**
86  * Get the number of pages.
87  * @return Number
88  */
89  public function getPageCount() {
90  return ceil($this->totalCount / $this->pageSize);
91  }
92 
93  /**
94  * Set the current offset (also selects the page).
95  * @param $offset The current list offset.
96  */
97  public function setOffset($offset) {
98  $this->offset = $offset;
99  $this->page = ceil(intval($offset) / $this->pageSize) + 1;
100  }
101 
102  /**
103  * Get the current offset.
104  * @return Number
105  */
106  public function getOffset() {
107  return $this->offset;
108  }
109 
110  /**
111  * Determine if we are on the first page.
112  * @return Boolean
113  */
114  public function isOnFirstPage() {
115  return $this->page == 1;
116  }
117 
118  /**
119  * Determine if we are on the first page.
120  * @return Boolean
121  */
122  public function isOnLastPage() {
123  return $this->page == $this->getPageCount;
124  }
125 
126  /**
127  * Check if this instance iignores the total count.
128  * @return Boolean
129  */
130  public function isIgnoringTotalCount() {
131  return $this->ignoreTotalCount;
132  }
133 
134  /**
135  * Get the pages for a pagination navigation
136  * @param $urlPattern Url string to use containing literal {page}, that will be replaced
137  * @param $maxDisplayPages Maximum number of pages to display (optional, default: 10)
138  * @return Array with keys 'first', 'last', 'current', 'prev', 'next', 'pages' and arrays with 'url', 'num' as values or null, if page count <= 1
139  */
140  public function getPagination($urlPattern, $maxDisplayPages=10) {
141  if ($this->getPageCount() <= 1) {
142  return null;
143  }
144 
145  // calculate pages
146  $getPage = function($val) use ($urlPattern) {
147  return ['num' => $val, 'url' => str_replace('{page}', $val, $urlPattern)];
148  };
149 
150  $first = 1;
151  $last = $this->getPageCount();
152  $page = $this->getPage();
153 
154  $halfRange = floor($maxDisplayPages/2);
155  $startPage = ($page < $halfRange) ? $first : max([$page-$halfRange, $first]);
156  $endPage = $maxDisplayPages-1 + $startPage;
157  $endPage = ($last < $endPage) ? $last : $endPage;
158  $diff = $startPage - $endPage + $maxDisplayPages-1;
159  $startPage -= ($startPage - $diff > 0) ? $diff : 0;
160 
161  $pages = array_map($getPage, range($startPage, $endPage));
162 
163  return [
164  'first' => $getPage($first),
165  'last' => $getPage($last),
166  'current' => $getPage($page),
167  'prev' => $page > $startPage ? $getPage($page-1) : null,
168  'next' => $page < $endPage ? $getPage($page+1) : null,
169  'pages' => $pages,
170  ];
171  }
172 }
173 ?>
setPage($page)
Set the current page (1-based) (also sets the offset).
Definition: PagingInfo.php:64
isIgnoringTotalCount()
Check if this instance iignores the total count.
Definition: PagingInfo.php:130
getPage()
Get the current page (1-based).
Definition: PagingInfo.php:73
isOnFirstPage()
Determine if we are on the first page.
Definition: PagingInfo.php:114
Persistence layer related interfaces and classes.
Definition: namespaces.php:42
setOffset($offset)
Set the current offset (also selects the page).
Definition: PagingInfo.php:97
setTotalCount($totalCount)
Set the number of list items.
Definition: PagingInfo.php:48
getPageCount()
Get the number of pages.
Definition: PagingInfo.php:89
__construct($pageSize, $ignoreTotalCount=false)
Creates a PagingInfo object.
Definition: PagingInfo.php:36
getPagination($urlPattern, $maxDisplayPages=10)
Get the pages for a pagination navigation.
Definition: PagingInfo.php:140
PagingInfo contains information about a paged list.
Definition: PagingInfo.php:18
isOnLastPage()
Determine if we are on the first page.
Definition: PagingInfo.php:122
getTotalCount()
Get the number of list items.
Definition: PagingInfo.php:56
getOffset()
Get the current offset.
Definition: PagingInfo.php:106
getPageSize()
Get the size of a pages.
Definition: PagingInfo.php:81