<?php
/**
* @param itn
*
*/
function range_limit($current, $total, $limit = 10, $start_at = 1)
{
$middle = ceil($limit / 2);
$current = max($start_at, min($total, $current));
$start = $current - $middle;
$end = $middle + $current;
if ($start <= $start_at) {
$start = $start_at;
$end = $limit;
} elseif ($end >= $total) {
$end = $total;
$start = $total - $limit;
}
for ($i = $start; $i <= $end; $i++) yield $i;
}
$limit = 10;
$total_pages = 55;
foreach(range_limit(1, $total_pages, $limit) as $page) {
echo $page == 1 ? " [$page] " : " $page ";
}
echo "\n";
foreach(range_limit(2, $total_pages, $limit) as $page) {
echo $page == 2 ? " [$page] " : " $page ";
}
echo "\n";
foreach(range_limit(33, $total_pages, $limit) as $page) {
echo $page == 33 ? " [$page] " : " $page ";
}
echo "\n";
foreach(range_limit(40, $total_pages, $limit) as $page) {
echo $page == 40 ? " [$page] " : " $page ";
}
PD9waHAKCi8qKgogKiBAcGFyYW0gaXRuIAogKiAKICovCmZ1bmN0aW9uIHJhbmdlX2xpbWl0KCRjdXJyZW50LCAkdG90YWwsICRsaW1pdCA9IDEwLCAkc3RhcnRfYXQgPSAxKQp7CiAgICAkbWlkZGxlID0gY2VpbCgkbGltaXQgLyAyKTsKCiAgICAkY3VycmVudCA9IG1heCgkc3RhcnRfYXQsIG1pbigkdG90YWwsICRjdXJyZW50KSk7CgogICAgJHN0YXJ0ID0gJGN1cnJlbnQgLSAkbWlkZGxlOwoKICAgICRlbmQgPSAkbWlkZGxlICsgJGN1cnJlbnQ7CgogICAgaWYgKCRzdGFydCA8PSAkc3RhcnRfYXQpIHsKICAgICAgICAkc3RhcnQgPSAkc3RhcnRfYXQ7CiAgICAgICAgJGVuZCA9ICRsaW1pdDsKCiAgICB9IGVsc2VpZiAoJGVuZCA+PSAkdG90YWwpIHsKICAgICAgICAkZW5kID0gJHRvdGFsOwogICAgICAgICRzdGFydCA9ICR0b3RhbCAtICRsaW1pdDsKICAgIH0KICAgIAogICAgZm9yICgkaSA9ICRzdGFydDsgJGkgPD0gJGVuZDs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