Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save arthurvasconcelos/16d99ec55d8d74166e98 to your computer and use it in GitHub Desktop.

Select an option

Save arthurvasconcelos/16d99ec55d8d74166e98 to your computer and use it in GitHub Desktop.
<?php
namespace App\Custom\Pagination\Presenters;
use Illuminate\Pagination\BootstrapThreePresenter;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\UrlWindow;
class MaterializePresenter extends BootstrapThreePresenter
{
/**
* @var int Number of links to display on each end of paginator
*/
const LINK_BUFFER_SIZE = 3;
/**
* Create a new presenter instance
*/
public function __construct(LengthAwarePaginator $paginator)
{
$this->paginator = $paginator;
$this->window = UrlWindow::make($paginator, self::LINK_BUFFER_SIZE);
}
/**
* Render paginator
*
* @return string
*/
public function render()
{
if ($this->hasPages()) {
return sprintf('<ul class="pagination">%s %s %s</ul>',
$this->getPrevious(), $this->getLinks(), $this->getNext());
} else {
return '';
}
}
/**
* Get HTML wrapper for page link
*
* @param string $url Link URL
* @param string $text Link text
* @param string $rel Link relationship (not used)
* @return string
*/
public function getPageLinkWrapper($url, $text, $rel = null)
{
if ($text == $this->paginator->currentPage()) {
return '<li class="active"><a href="' . $url . '">' . $text . '</a></li>';
} else {
return '<li class="waves-effect"><a href="' . $url . '">' . $text . '</a></li>';
}
}
/**
* Get HTML wrapper for disabled text
*
* @param string $text
* @return string
*/
public function getDisabledTextWrapper($text)
{
return '<li class="disabled"><a href="javascript:void(0)">' . $text . '</a></li>';
}
/**
* Get previous page pagination element
*
* @return string
*/
public function getPrevious()
{
if ($this->paginator->currentPage() <= 1) {
return $this->getDisabledTextWrapper('<i class="mdi-navigation-chevron-left"></i>');
} else {
$url = $this->paginator->url($this->paginator->currentPage() - 1);
return $this->getPageLinkWrapper($url, '<i class="mdi-navigation-chevron-left"></i>');
}
}
/**
* Get next page pagination element
*
* @return string
*/
public function getNext()
{
if ($this->paginator->currentPage() >= $this->paginator->lastPage()) {
return $this->getDisabledTextWrapper('<i class="mdi-navigation-chevron-right"></i>');
} else {
$url = $this->paginator->url($this->paginator->currentPage() + 1);
return $this->getPageLinkWrapper($url, '<i class="mdi-navigation-chevron-right"></i>');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment