* @package MCMS\Components */ class SimplePageList implements \MCMS\Component\iComponent { private $links = array(); private $current = 1; private $glue = ' • '; public function __construct() { } /** * * @param int * $start * @param int * $end * @param string $baseURL a string which contains ##. the first ## will be replaced by the page number * @return mixed */ public function setWithRange($start, $end, $baseURL) { $this->links = array(); for($i = $start; $i <= $end; $i++) { $this->links[$i] = str_replace('##', $i, $baseURL); } } /** * set values by an array, key is page number, value is url * * @param * $pageInfos */ public function setWithArray(array $pageInfos) { $this->links = $pageInfos; } public function setCurrentPage($pageNum) { $this->current = $pageNum; } public function getCurrentPage() { return $this->current; } public function generate() { $links = array(); $i = 1; if(count($this->links) < 10) { foreach($this->links as $l) { $links[] = '' . ($i - 1 == $this->current ? '' . $i . '' : $i) . ''; $i++; } return implode($this->glue, $links); } reset($this->links); $l = current($this->links); $links[] = '' . ($i - 1 == $this->current ? '' . $i . '' : $i) . ''; if($this->current < 4) { for($j = 1; $j <= $this->current + 2; $j++) { $i++; $l = next($this->links); $links[] = '' . ($i - 1 == $this->current ? '' . $i . '' : $i) . ''; } $links[] = '...'; } elseif($this->current > count($this->links) - 5) { $links[] = '...'; for($i; $i < $this->current - 2; $i++) next($this->links); $i++; for($i; $i < count($this->links); $i++) { $l = next($this->links); $links[] = '' . ($i - 1 == $this->current ? '' . $i . '' : $i) . ''; } } else { $links[] = '...'; for($i; $i < $this->current - 2; $i++) next($this->links); for($j = 1; $j <= 5; $j++) { $i++; $l = next($this->links); $links[] = '' . ($i - 1 == $this->current ? '' . $i . '' : $i) . ''; } $links[] = '...'; } end($this->links); $i = count($this->links); $l = current($this->links); $links[] = '' . ($i - 1 == $this->current ? '' . $i . '' : $i) . ''; return implode($this->glue, $links); } }