Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
PAgination moved to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jul 15, 2017
1 parent cbc0802 commit 68234cf
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 51 deletions.
124 changes: 124 additions & 0 deletions src/Twig/Functions/Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);

namespace Selami\View\Twig\Functions;

use Selami\View\FunctionInterface;

class Pagination implements FunctionInterface
{
const PAGE_GROUP_LIMIT = 3;
private $total;
private $current;
private $linkTemplate;
private $parentTemplate;
private $itemTemplate;
private $linkItemTemplate;
private $ellipsesTemplate;

public function __construct(
int $total,
int $current,
string $linkTemplate,
string $parentTemplate,
string $itemTemplate,
string $linkItemTemplate,
string $ellipsesTemplate
) {

$this->total = $total;
$this->current = $current;
$this->linkTemplate = $linkTemplate;
$this->parentTemplate = $parentTemplate;
$this->itemTemplate = $itemTemplate;
$this->linkItemTemplate = $linkItemTemplate;
$this->ellipsesTemplate = $ellipsesTemplate;
}

public function run(): string
{
$items = '';
$renderedEllipses = 0;
$useEllipses = ($this->total > 10) ? 1 : 0;

for ($i=1; $i <= $this->total; $i++) {
$item = $this->getItem($i, $renderedEllipses, $useEllipses);
$items .= $item[0];
$renderedEllipses = $item[1];
}
return str_replace('(items)', $items, $this->parentTemplate);
}

private function getItem(int $i, $renderedEllipses, $useEllipses) : array
{
$values = [
'(item_class)' => '',
'(href)' => '',
'(link_class)' => '',
'(text)' => ''
];
$class = $this->getClasses($i);
$values['(link_class)'] = $class;
$values['(item_class)'] = $class;
$values['(text)'] = $i;
$values['(href)'] = str_replace('(page_num)', $i, $this->linkTemplate);

$itemData = $this->determineUseLink($i, $values, $useEllipses, $renderedEllipses);

return $itemData;
}

private function determineUseLink(int $i, array $values, int $useEllipses, int $renderedEllipses) : array
{
$item ='';
if ($useEllipses === 1) {
return $this->useEllipses($i, $values, $renderedEllipses);
}
$link = strtr($this->linkItemTemplate, $values);
$thisItemTemplate = str_replace(
array('(link)'),
array($link),
$this->itemTemplate
);
$item = strtr($thisItemTemplate, $values);
return [$item, $renderedEllipses];
}

private function useEllipses(int $i, array $values, int $renderedEllipses)
{
$pageGroupLimit = self::PAGE_GROUP_LIMIT;
$item = '';
if (($i <= $pageGroupLimit)
|| ($i >= ($this->current-1) && $i <= ($this->current+1))
|| ($i >= ($this->total-2))
) {
return $this->returnLink($values);
}
if ($renderedEllipses === 0) {
$item = $this->ellipsesTemplate;
}
$renderedEllipses = 1;
return [$item, $renderedEllipses];
}

private function returnLink($values)
{
$renderedEllipses = 0;
$link = strtr($this->linkItemTemplate, $values);
$thisItemTemplate = str_replace(
array('(link)'),
array($link),
$this->itemTemplate
);
$item = strtr($thisItemTemplate, $values);
return [$item, $renderedEllipses];
}

private function getClasses(int $i)
{
if ($i === $this->current) {
return 'active';
}
return '';
}
}
64 changes: 13 additions & 51 deletions src/Twig/TwigExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ protected function extendForWidget() : void
$filter = new \Twig_SimpleFunction(
'Widget_*_*',
function ($widgetNameStr, $widgetActionStr, $args = []) {


$function = new Functions\Widget($this->twig, $this->config, $widgetNameStr, $widgetActionStr, $args);
return $function->run();

},
array('is_safe' => array('html'))
);
Expand Down Expand Up @@ -89,7 +86,7 @@ protected function extendForVarDump() : void
'varDump',
function ($args) {
/**
* @noinspection ForgottenDebugOutputInspection
* @noinspection ForgottenDebugOutputInspection
*/
var_dump($args);
},
Expand All @@ -101,7 +98,7 @@ function ($args) {
protected function extendForPagination() : void
{
/**
* @noinspection MoreThanThreeArgumentsInspection
* @noinspection MoreThanThreeArgumentsInspection
*/
$filter = new \Twig_SimpleFunction(
'Pagination',
Expand All @@ -114,52 +111,17 @@ function (
string $linkItemTemplate = '<a href="(href)" class="(link_class)">(text)</a>',
string $ellipsesTemplate = '<li><a>...</a></li>'
) {
$items = '';
$pageGroupLimit = 3;
$useEllipses = ($total >10) ? 1 : 0;
$renderedEllipses = 0;
$values = [
'(item_class)' => '',
'(href)' => '',
'(link_class)' => '',
'(text)' => ''
];
for ($i=1; $i <= $total; $i++) {
$values['(link_class)'] = '';
$values['(item_class)'] = '';
if ($i === $current) {
$values['(link_class)'] = 'active';
$values['(item_class)'] = 'active';
}
$values['(text)'] = $i;
$values['(href)'] = str_replace('(page_num)', $i, $linkTemplate);
$link = strtr($linkItemTemplate, $values);
$useLink = 1;
if ($useEllipses === 1) {
$useLink = 0;
if (($i <= $pageGroupLimit)
|| ($i >= ($current-1) && $i <= ($current+1))
|| ($i >= ($total-2))
) {
$useLink = 1;
$renderedEllipses = 0;
} else {
if ($renderedEllipses === 0) {
$items .= $ellipsesTemplate;
}
$renderedEllipses = 1;
}
}
if ($useLink === 1) {
$thisItemTemplate = str_replace(
array('(link)'),
array($link),
$itemTemplate
);
$items .= strtr($thisItemTemplate, $values);
}
}
return str_replace('(items)', $items, $parentTemplate);

$function = new Functions\Pagination(
$total,
$current,
$linkTemplate,
$parentTemplate,
$itemTemplate,
$linkItemTemplate,
$ellipsesTemplate
);
return $function->run();
},
array('is_safe' => array('html'))
);
Expand Down

0 comments on commit 68234cf

Please sign in to comment.