PageIndex
in package
implements
Stringable
Uses
BackwardCompatibility
Constructs a page list.
E.g.: 1 ... 6 7 [8] 9 10 ... 15.
Table of Contents
Interfaces
- Stringable
Properties
- $base_url : string
- $max_value : int
- $num_per_page : int
- $short_format : bool
- $show_prevnext : bool
- $start : int
- $backcompat : array<string|int, mixed>
- $base_link : string
- $current_page : string
- $current_page_num : int
- $expand_pages : string
- $extra_after : string
- $extra_before : string
- $last_page_num : int
- $last_page_value : int
- $next_page : string
- $page : string
- $page_contiguous : int
- $previous_page : string
- $start_invalid : bool
Methods
- __construct() : mixed
- Constructor.
- __toString() : mixed
- Finalizes and returns the page index links.
- exportStatic() : void
- Provides a way to export a class's public static properties and methods to global namespace.
- load() : object
- Static wrapper for constructor.
- expandPages() : string
- Show the ... for the hidden pages.
- firstPage() : string
- Show the first page in the list.
- fixStart() : int
- Fixes $start if it is out of bounds or not a multiple of $this->num_per_page.
- lastPage() : string
- Show the last page in the list.
- nextPage() : string
- Show the "next page" link.
- pageRange() : string
- Shows a range of pages.
- prevPage() : string
- Show the "prev page" link.
Properties
$base_url
public
string
$base_url
The base URL for all the page index links.
$max_value
public
int
$max_value
The total number of items in the overall paginated list.
$num_per_page
public
int
$num_per_page
How many items to show per page.
$short_format
public
bool
$short_format
= false
Whether to use "url.offset" format instead of "url;start=offset" format.
$show_prevnext
public
bool
$show_prevnext
= true
Set this to false to hide the previous page and next page arrow links.
$start
public
int
$start
The value we are starting at.
Should be the number of a specific item in the overall paginated list.
$backcompat
private
static array<string|int, mixed>
$backcompat
= ['func_names' => ['load' => 'constructPageIndex']]
BackwardCompatibility settings for this class.
$base_link
private
string
$base_link
Template string used to create links to specific pages.
This is built by inserting $this->base_url into $this->page.
$current_page
private
string
$current_page
= '<span class="current_page">%1$d</span> '
Template string for the current page.
$current_page_num
private
int
$current_page_num
The number of the page that the user is currently viewing.
$expand_pages
private
string
$expand_pages
= '<span class="expand_pages" onclick="expandPages(this, {LINK}, {FIRST_PAGE}, {LAST_PAGE}, {PER_PAGE});"> ... </span>'
Template string for the "..." bit in long page indexes.
$extra_after
private
string
$extra_after
= ''
Template string for the bit to show after the page links end.
$extra_before
private
string
$extra_before
= '<span class="pages">{txt_pages}</span>'
Template string for the bit to show before the page links begin.
$last_page_num
private
int
$last_page_num
The number of the last page.
$last_page_value
private
int
$last_page_value
Item number of the first item on the last page.
$next_page
private
string
$next_page
= '<span class="main_icons next_page"></span>'
Template string for next page arrow link.
$page
private
string
$page
= '<a class="nav_page" href="{URL}">%2$s</a> '
Template string for links to numbered pages.
$page_contiguous
private
int
$page_contiguous
How many contiguous pages to show in the middle when using the compact page index.
For example: 3 to display: 1 ... 7 [8] 9 ... 15 5 to display: 1 ... 6 7 [8] 9 10 ... 15
$previous_page
private
string
$previous_page
= '<span class="main_icons previous_page"></span>'
Template string for previous page arrow link.
$start_invalid
private
bool
$start_invalid
Tracks whether the requested $start value was out of bounds.
Methods
__construct()
Constructor.
public
__construct(string $base_url, int &$start, int $max_value, int $num_per_page[, bool $short_format = false ][, bool $show_prevnext = true ]) : mixed
-
short_format causes it to use "url.page" instead of "url;start=page".
-
very importantly, cleans up the start value passed, and forces it to be a multiple of num_per_page.
-
checks that start is not more than max_value.
-
base_url should be the URL without any start parameter on it.
-
uses the compactTopicPagesEnable and compactTopicPagesContiguous settings to decide how to display the menu.
Parameters
- $base_url : string
-
The basic URL to be used for each link.
- $start : int
-
The start position, by reference. If this is not a multiple of the number of items per page, it is sanitized to be so and the value will persist upon the function's return.
- $max_value : int
-
The total number of items you are paginating for.
- $num_per_page : int
-
The number of items to be displayed on a given page. $start will be forced to be a multiple of this value.
- $short_format : bool = false
-
Whether to use "url.offset" instead of "url;start=offset". Default: false.
- $show_prevnext : bool = true
-
Whether the Previous and Next links should be shown. Default: true.
__toString()
Finalizes and returns the page index links.
public
__toString() : mixed
exportStatic()
Provides a way to export a class's public static properties and methods to global namespace.
public
static exportStatic() : void
To do so:
- Use this trait in the class.
- At the END of the class's file, call its exportStatic() method.
Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.
Regarding the $backcompat items:
A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names']. Likewise, a class's static methods are not exported as global functions unless explicitly included in $backcompat['func_names'].
$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.
$backcompat['func_names'] is a simple array where the keys are the names of one or more of a class's static methods, and the values are the names that should be used for global functions that will encapsulate those methods. Methods that are not named in this array will not be exported.
Adding non-static properties or methods to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.
load()
Static wrapper for constructor.
public
static load(string $base_url, int &$start, int $max_value, int $num_per_page[, bool $short_format = false ][, bool $show_prevnext = true ]) : object
Parameters
- $base_url : string
- $start : int
- $max_value : int
- $num_per_page : int
- $short_format : bool = false
- $show_prevnext : bool = true
Return values
object —An instance of this class.
expandPages()
Show the ... for the hidden pages.
protected
expandPages([bool $after = false ]) : string
Used when compact pages are turned on.
If $after is false, does the one near the start: (prev page 1 >...< 6 7 [8] 9 10 ... 15 next page)
If $after is true, does the one near the end: (prev page 1 ... 6 7 [8] 9 10 >...< 15 next page)
Parameters
- $after : bool = false
-
If true, do the one near the end.
Return values
stringfirstPage()
Show the first page in the list.
protected
firstPage() : string
(prev page >1< ... 6 7 [8] 9 10 ... 15)
Return values
stringfixStart()
Fixes $start if it is out of bounds or not a multiple of $this->num_per_page.
protected
fixStart(int $start) : int
Parameters
- $start : int
-
The start value.
Return values
int —The fixed start value.
lastPage()
Show the last page in the list.
protected
lastPage() : string
(prev page 1 ... 6 7 [8] 9 10 ... >15< next page)
Return values
stringnextPage()
Show the "next page" link.
protected
nextPage() : string
(prev page 1 ... 6 7 [8] 9 10 ... 15 >next page<)
Return values
stringpageRange()
Shows a range of pages.
protected
pageRange(int $min, int $max) : string
Parameters
- $min : int
-
The lowest page number to show.
- $max : int
-
The highest page number to show.
Return values
stringprevPage()
Show the "prev page" link.
protected
prevPage() : string
(>prev page< 1 ... 6 7 [8] 9 10 ... 15 next page)