Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
dmj
php-paginator
Commits
b29a30c3
Commit
b29a30c3
authored
Mar 22, 2017
by
David Maus
Browse files
Initial Commit
parents
Changes
4
Hide whitespace changes
Inline
Side-by-side
.dir-locals.el
0 → 100644
View file @
b29a30c3
((
nil
.
((
php-project-name
.
"HAB Paginator"
))))
.gitignore
0 → 100644
View file @
b29a30c3
/vendor/
composer.lock
*~
\#*
composer.json
0 → 100644
View file @
b29a30c3
{
"name"
:
"hab/php-paginator"
,
"autoload"
:
{
"psr-4"
:
{
"HAB\\Paginator\\"
:
"lib/"
}
}
}
lib/Paginator.php
0 → 100644
View file @
b29a30c3
<?php
/**
* This file is part of HAB Paginator.
*
* HAB Paginator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HAB Paginator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HAB Paginator. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2017 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace
HAB\Paginator
;
use
Countable
;
/**
* Paginator.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2017 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class
Paginator
implements
Countable
{
/**
* Current page.
*
* @var integer
*/
private
$currentPage
;
/**
* Total number of items.
*
* @var integer
*/
private
$numberOfItems
;
/**
* Number of items per page.
*
* @var integer
*/
private
$itemsPerPage
=
25
;
/**
* Number of pages.
*
* @var integer
*/
private
$numberOfPages
;
/**
* Pagination information.
*
* @var array
*/
private
$pages
;
/**
* Set number of items.
*
* @param integer $numberOfItems
* @return self
*/
public
function
setNumberOfItems
(
$numberOfItems
)
{
$this
->
numberOfItems
=
$numberOfItems
;
$this
->
numberOfPages
=
null
;
return
$this
;
}
/**
* Return number of items.
*
* @return integer
*/
public
function
getNumberOfItems
()
{
return
$this
->
numberOfItems
;
}
/**
* Return number of items per page.
*
* @return integer
*/
public
function
getItemsPerPage
()
{
return
$this
->
itemsPerPage
;
}
/**
* Set number of items per page.
*
* @param integer $itemsPerPage
* @return self
*/
public
function
setItemsPerPage
(
$itemsPerPage
)
{
$this
->
itemsPerPage
=
$itemsPerPage
;
return
$this
;
}
/**
* Set current page.
*
* @param integer $currentPage
* @return self
*/
public
function
setCurrentPage
(
$currentPage
)
{
$this
->
currentPage
=
$this
->
normalizePage
(
$currentPage
);
return
$this
;
}
/**
* Return current page.
*
* @return integer
*/
public
function
getCurrentPage
()
{
return
$this
->
currentPage
;
}
/**
* Return offset of current page.
*
* @return integer
*/
public
function
getCurrentPageOffset
()
{
$page
=
$this
->
getCurrentPage
();
$size
=
$this
->
getItemsPerPage
();
return
(
$page
-
1
)
*
$size
;
}
/**
* Return paging information.
*
* @return array
*/
public
function
getPages
()
{
if
(
is_null
(
$this
->
pages
))
{
$this
->
pages
=
$this
->
createPages
();
}
return
$this
->
pages
;
}
/**
* {@inheritDoc}
*/
public
function
count
()
{
if
(
is_null
(
$this
->
numberOfPages
))
{
$total
=
$this
->
getNumberOfItems
();
$pagesize
=
$this
->
getItemsPerPage
();
$this
->
numberOfPages
=
(
int
)
ceil
(
$total
/
$pagesize
);
}
return
$this
->
numberOfPages
;
}
/**
* Return visible pages.
*
* @return array
*/
public
function
getVisiblePages
()
{
$current
=
$this
->
getCurrentPage
();
$total
=
$this
->
count
();
if
(
$total
<
3
)
{
return
array
();
}
$start
=
max
(
$current
-
5
,
1
);
$end
=
min
(
$start
+
9
,
$total
);
return
range
(
$start
,
$end
);
}
/**
* Normalize page number.
*
* @param integer $page
* @return integer
*/
private
function
normalizePage
(
$page
)
{
if
(
$page
<
1
)
{
$page
=
1
;
}
$count
=
count
(
$this
);
if
(
$count
>
0
and
$page
>
$count
)
{
$page
=
$count
;
}
return
$page
;
}
/**
* Create paging information.
*
* @return array
*/
private
function
createPages
()
{
$pages
=
array
();
$pages
[
'current'
]
=
$this
->
getCurrentPage
();
$pages
[
'first'
]
=
1
;
$pages
[
'last'
]
=
count
(
$this
);
$pages
[
'count'
]
=
count
(
$this
);
$pages
[
'size'
]
=
$this
->
getItemsPerPage
();
$pages
[
'visible'
]
=
$this
->
getVisiblePages
();
$pages
[
'prev'
]
=
(
$pages
[
'current'
]
===
$pages
[
'first'
])
?
null
:
$pages
[
'current'
]
-
1
;
$pages
[
'next'
]
=
(
$pages
[
'current'
]
===
$pages
[
'last'
])
?
null
:
$pages
[
'current'
]
+
1
;
return
$pages
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment