PagingInfo.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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  private $_pageSize = 10;
21  private $_page = 0;
22  private $_offset = 0;
23  private $_totalCount = 0;
24  private $_ignoreTotalCount = false;
25 
26  /**
27  * Creates a PagingInfo object. The ignoreTotalCount parameter may be
28  * set to true, if the count is to be ignored. This may speed up loading
29  * of objects, because an extra count query may be omitted.
30  * @param $pageSize The pageSize (-1 to set no page size)
31  * @param $ignoreTotalCount Boolean whether this instance ignores the
32  * total count or not (optional, default: _false_)
33  */
34  public function __construct($pageSize, $ignoreTotalCount=false) {
35  $this->_pageSize = intval($pageSize);
36  $this->_ignoreTotalCount = $ignoreTotalCount;
37  }
38 
39  /**
40  * Set the number of list items.
41  * @param $totalCount The number of list items.
42  */
43  public function setTotalCount($totalCount) {
44  $this->_totalCount = intval($totalCount);
45  }
46 
47  /**
48  * Get the number of list items.
49  * @return Number
50  */
51  public function getTotalCount() {
52  return $this->_totalCount;
53  }
54 
55  /**
56  * Set the current page (1-based) (also sets the offset).
57  * @param $page The current page.
58  */
59  public function setPage($page) {
60  $this->_page = intval($page);
61  $this->_offset = ($page - 1) * $this->_pageSize;
62  }
63 
64  /**
65  * Get the current page (1-based).
66  * @return Number
67  */
68  public function getPage() {
69  return $this->_page;
70  }
71 
72  /**
73  * Get the size of a pages.
74  * @return Number
75  */
76  public function getPageSize() {
77  return $this->_pageSize;
78  }
79 
80  /**
81  * Get the number of pages.
82  * @return Number
83  */
84  public function getPageCount() {
85  return ceil($this->_totalCount / $this->_pageSize);
86  }
87 
88  /**
89  * Set the current offset (also selects the page).
90  * @param $offset The current list offset.
91  */
92  public function setOffset($offset) {
93  $this->_offset = $offset;
94  $this->_page = ceil(intval($offset) / $this->_pageSize) + 1;
95  }
96 
97  /**
98  * Get the current offset.
99  * @return Number
100  */
101  public function getOffset() {
102  return $this->_offset;
103  }
104 
105  /**
106  * Determine if we are on the first page.
107  * @return Boolean
108  */
109  public function isOnFirstPage() {
110  return $this->_page == 1;
111  }
112 
113  /**
114  * Determine if we are on the first page.
115  * @return Boolean
116  */
117  public function isOnLastPage() {
118  return $this->_page == $this->getPageCount;
119  }
120 
121  /**
122  * Check if this instance iignores the total count.
123  * @return Boolean
124  */
125  public function isIgnoringTotalCount() {
126  return $this->_ignoreTotalCount;
127  }
128 }
129 ?>
setOffset($offset)
Set the current offset (also selects the page).
Definition: PagingInfo.php:92
getOffset()
Get the current offset.
Definition: PagingInfo.php:101
getPageSize()
Get the size of a pages.
Definition: PagingInfo.php:76
Persistence layer related interfaces and classes.
Definition: namespaces.php:42
setTotalCount($totalCount)
Set the number of list items.
Definition: PagingInfo.php:43
PagingInfo contains information about a paged list.
Definition: PagingInfo.php:18
isOnLastPage()
Determine if we are on the first page.
Definition: PagingInfo.php:117
isIgnoringTotalCount()
Check if this instance iignores the total count.
Definition: PagingInfo.php:125
__construct($pageSize, $ignoreTotalCount=false)
Creates a PagingInfo object.
Definition: PagingInfo.php:34
getPage()
Get the current page (1-based).
Definition: PagingInfo.php:68
getPageCount()
Get the number of pages.
Definition: PagingInfo.php:84
getTotalCount()
Get the number of list items.
Definition: PagingInfo.php:51
setPage($page)
Set the current page (1-based) (also sets the offset).
Definition: PagingInfo.php:59
isOnFirstPage()
Determine if we are on the first page.
Definition: PagingInfo.php:109