Skip to content

Commit

Permalink
Started refactoring PaginatorHelper::numbers() into many smaller meth…
Browse files Browse the repository at this point in the history
…ods.
  • Loading branch information
Florian Krämer committed Jan 8, 2015
1 parent b20e8c8 commit b38b993
Showing 1 changed file with 156 additions and 72 deletions.
228 changes: 156 additions & 72 deletions src/View/Helper/PaginatorHelper.php
Expand Up @@ -635,96 +635,180 @@ public function numbers(array $options = [])
$templater->{$method}($options['templates']);
}

if ($options['modulus'] && $params['pageCount'] > $options['modulus']) {
$out = $this->_modulusNumbers($templater, $params, $options);
} else {
$out = $this->_numbers($templater, $params, $options);
}

if (isset($options['templates'])) {
$templater->pop();
}

return $out;
}

/**
* Calculates the start and end for the pagination numbers.
*
* @param array $params Params from the numbers() method.
* @param array $options Options from the numbers() method.
* @return array An array with the start and end numbers.
*/
protected function _getNumbersStartAndEnd($params, $options)
{
$half = (int)($options['modulus'] / 2);
$end = $params['page'] + $half;

if ($end > $params['pageCount']) {
$end = $params['pageCount'];
}
$start = $params['page'] - ($options['modulus'] - ($end - $params['page']));
if ($start <= 1) {
$start = 1;
$end = $params['page'] + ($options['modulus'] - $params['page']) + 1;
}
return compact('start', 'end');
}

/**
* Formats a number for the paginator number output.
*
* @param StringTemplate $templater
* @param array $options Options from the numbers() method.
* @return string
*/
protected function _formatNumber($templater, $options)
{
$vars = [
'text' => $options['text'],
'url' => $this->generateUrl(['page' => $options['page']], $options['model']),
];
return $templater->format('number', $vars);
}

/**
* Generates the numbers for the paginator numbers() method.
*
* @param StringTemplate $templater
* @param array $params Params from the numbers() method.
* @param array $options Options from the numbers() method.
* @return string Markup output.
*/
protected function _modulusNumbers($templater, $params, $options)
{
$out = '';
$ellipsis = $templater->format('ellipsis', []);

if ($options['modulus'] && $params['pageCount'] > $options['modulus']) {
$half = (int)($options['modulus'] / 2);
$end = $params['page'] + $half;
extract($this->_getNumbersStartAndEnd($params, $options));

if ($end > $params['pageCount']) {
$end = $params['pageCount'];
}
$start = $params['page'] - ($options['modulus'] - ($end - $params['page']));
if ($start <= 1) {
$start = 1;
$end = $params['page'] + ($options['modulus'] - $params['page']) + 1;
}
$out .= $this->_firstNumber($ellipsis, $params, $end, $options);

if ($options['first'] && $start > 1) {
$offset = ($start <= (int)$options['first']) ? $start - 1 : $options['first'];
$out .= $this->first($offset);
if ($offset < $start - 1) {
$out .= $ellipsis;
}
}
$out .= $options['before'];

$out .= $options['before'];
for ($i = $start; $i < $params['page']; $i++) {
$out .= $this->_formatNumber($templater, [
'text' => $i,
'page' => $i,
'model' => $options['model']
]);
}

for ($i = $start; $i < $params['page']; $i++) {
$vars = [
'text' => $i,
'url' => $this->generateUrl(['page' => $i], $options['model']),
];
$out .= $templater->format('number', $vars);
}
$out .= $templater->format('current', [
'text' => $params['page'],
'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
]);

$out .= $templater->format('current', [
'text' => $params['page'],
'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
$start = $params['page'] + 1;
for ($i = $start; $i < $end; $i++) {
$out .= $this->_formatNumber($templater, [
'text' => $i,
'page' => $i,
'model' => $options['model']
]);
}

$start = $params['page'] + 1;
for ($i = $start; $i < $end; $i++) {
$vars = [
'text' => $i,
'url' => $this->generateUrl(['page' => $i], $options['model']),
];
$out .= $templater->format('number', $vars);
if ($end != $params['page']) {
$out .= $this->_formatNumber($templater, [
'text' => $i,
'page' => $end,
'model' => $options['model']
]);
}

$out .= $options['after'];
$out .= $this->_lastNumber($ellipsis, $params, $end, $options);
return $out;
}

/**
* Generates the last number for the paginator numbers() method.
*
* @param StringTemplate $ellipsis
* @param array $params Params from the numbers() method.
* @param integer $start Start number.
* @param array $options Options from the numbers() method.
* @return string Markup output.
*/
protected function _firstNumber($ellipsis, $params, $start, $options) {
$out = '';
if ($options['first'] && $start > 1) {
$offset = ($start <= (int)$options['first']) ? $start - 1 : $options['first'];
$out .= $this->first($offset);
if ($offset < $start - 1) {
$out .= $ellipsis;
}
}
return $out;
}

if ($end != $params['page']) {
/**
* Generates the last number for the paginator numbers() method.
*
* @param StringTemplate $ellipsis
* @param array $params Params from the numbers() method.
* @param integer $end End number.
* @param array $options Options from the numbers() method.
* @return string Markup output.
*/
protected function _lastNumber($ellipsis, $params, $end, $options) {
$out = '';
if ($options['last'] && $end < $params['pageCount']) {
$offset = ($params['pageCount'] < $end + (int)$options['last']) ? $params['pageCount'] - $end : $options['last'];
if ($offset <= $options['last'] && $params['pageCount'] - $end > $offset) {
$out .= $ellipsis;
}
$out .= $this->last($offset);
}
return $out;
}

/**
* Generates the numbers for the paginator numbers() method.
*
* @param StringTemplate $templater
* @param array $params Params from the numbers() method.
* @param array $options Options from the numbers() method.
* @return string Markup output.
*/
protected function _numbers($templater, $params, $options) {
$out = '';
$out .= $options['before'];
for ($i = 1; $i <= $params['pageCount']; $i++) {
if ($i == $params['page']) {
$out .= $templater->format('current', [
'text' => $params['page'],
'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
]);
} else {
$vars = [
'text' => $i,
'url' => $this->generateUrl(['page' => $end], $options['model']),
'url' => $this->generateUrl(['page' => $i], $options['model']),
];
$out .= $templater->format('number', $vars);
}

$out .= $options['after'];

if ($options['last'] && $end < $params['pageCount']) {
$offset = ($params['pageCount'] < $end + (int)$options['last']) ? $params['pageCount'] - $end : $options['last'];
if ($offset <= $options['last'] && $params['pageCount'] - $end > $offset) {
$out .= $ellipsis;
}
$out .= $this->last($offset);
}

} else {
$out .= $options['before'];

for ($i = 1; $i <= $params['pageCount']; $i++) {
if ($i == $params['page']) {
$out .= $templater->format('current', [
'text' => $params['page'],
'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
]);
} else {
$vars = [
'text' => $i,
'url' => $this->generateUrl(['page' => $i], $options['model']),
];
$out .= $templater->format('number', $vars);
}
}

$out .= $options['after'];
}
if (isset($options['templates'])) {
$templater->pop();
}

$out .= $options['after'];
return $out;
}

Expand Down

0 comments on commit b38b993

Please sign in to comment.