Slimore
  • Namespace
  • Class

Namespaces

  • None
  • Slimore
    • Cache
      • Exception
    • Captcha
    • Database
    • Debug
    • Http
    • Image
    • Log
    • Middleware
    • Mvc
    • Pagination
    • Upload

Classes

  • Slimore\Cache\File
  • Slimore\Captcha\Builder
  • Slimore\Database\Manager
  • Slimore\Debug\Simpler
  • Slimore\Http\Client
  • Slimore\Image\Gd
  • Slimore\Log\Writer
  • Slimore\Middleware\Exceptions
  • Slimore\Mvc\Application
  • Slimore\Mvc\Controller
  • Slimore\Mvc\Model
  • Slimore\Mvc\View
  • Slimore\Pagination\Paginator
  • Slimore\Upload\Uploader

Exceptions

  • Slimore\Cache\Exception\File
  • Slimore\Captcha\Exception
  • Slimore\Database\Exception
  • Slimore\Http\Exception
  • Slimore\Mvc\Exception
  • Slimore\Pagination\Exception
  • Slimore\Upload\Exception

Functions

  • arrayToObject
  • console
  • controller
  • ctl
  • currentUrl
  • decrypt
  • detectDevice
  • encrypt
  • fileFormatSize
  • getDirectoryItems
  • getDpi
  • hexToRGB
  • html
  • imageDataUrl
  • iPad
  • iPhone
  • iPod
  • isAndroid
  • isApache
  • isBlackberry
  • isChrome
  • isCli
  • isFirefox
  • isFreeBSD
  • isIE
  • isIIS
  • isJson
  • isLinux
  • isMacOSX
  • isMobile
  • isNginx
  • isOpera
  • isSafari
  • isTablet
  • isUnix
  • isUnixLike
  • isWebOS
  • isWindows
  • js
  • jsonToArray
  • linkTag
  • password
  • phoneMaskCode
  • randomCharacters
  • replaceDirSeparator
  • rgbToHex
  • script
  • shortUrl
  • style
  1 <?php
  2 
  3 /**
  4  * Slimore - The fully (H)MVC framework based on the Slim PHP framework.
  5  *
  6  * @author      Pandao <slimore@ipandao.com>
  7  * @copyright   2015 Pandao
  8  * @link        http://github.com/slimore/slimore
  9  * @license     MIT License https://github.com/slimore/slimore#license
 10  * @version     0.1.0
 11  * @package     Slimore\Pagination
 12  */
 13 
 14 namespace Slimore\Pagination;
 15 
 16 use \Slimore\Debug as Debug;
 17 
 18 /**
 19  * Class Paginator
 20  *
 21  * @author Pandao
 22  * @updateTime 2015-06-05 23:55:05
 23  * @package Slimore\Pagination
 24  */
 25 
 26 class Paginator
 27 {
 28     public  $sql;
 29     public  $num;
 30     public  $page;
 31     public  $prev;
 32     public  $next;
 33     public  $last;
 34     public  $first;
 35     public  $total;
 36     public  $range;
 37     public  $ranges;
 38     public  $offset;
 39     public  $pageTotal;
 40     public  $baseUrl = '';
 41     public  $select  = '*';
 42     public  $where   = [];
 43     public  $orderBy = 'id';
 44     public  $sortBy  = 'ASC';
 45     public  $model;
 46     public  $query;
 47     private $params;
 48     private $data    = [];
 49 
 50     /**
 51      * Construction method
 52      *
 53      * @param \Slimore\Mvc\Model $model
 54      * @param $page
 55      * @param $total
 56      * @param int $num 10
 57      * @param int $range 6
 58      * @return void
 59      */
 60 
 61     public function __construct(\Slimore\Mvc\Model $model, $page, $total, $num = 10, $range = 6)
 62     {
 63         $this->num   = $num;
 64         $this->page  = $page;
 65         $this->total = $total;
 66         $this->range = $range;
 67         $this->model = $model;
 68         $this->first = 1;
 69 
 70         $this->make();
 71     }
 72 
 73     /**
 74      * Make paginator params
 75      *
 76      * @return void
 77      */
 78 
 79     public function make()
 80     {
 81         $this->pageTotal = ceil($this->total / $this->num);
 82         $this->page      = ($this->page > $this->pageTotal) ? $this->pageTotal : $this->page;
 83         $this->offset    = ($this->page == 1) ? 0 : (($this->page - 1) * $this->num);
 84         $this->prev      = ($this->page == 1) ? 1 : $this->page - 1;
 85         $this->next      = ($this->page == $this->pageTotal) ? $this->pageTotal : $this->page + 1;
 86         $this->last      = $this->pageTotal;
 87 
 88         $this->range();
 89     }
 90 
 91     /**
 92      * Set/Get paginator ranges
 93      *
 94      * @return array
 95      */
 96 
 97     public function range()
 98     {
 99         $start = $this->page - $this->range;
100         $start = ($start < 1) ? 1 : $start;
101         $end   = $this->page + $this->range;
102         $end   = ($end > $this->pageTotal) ? $this->pageTotal : $end;
103 
104         $this->ranges = [
105             'start'   => $start,
106             'end'     => $end
107         ];
108 
109         return $this->ranges;
110     }
111 
112     /**
113      * Reset model
114      *
115      * @param \Slimore\Mvc\Model $model
116      * @return \Slimore\Mvc\Model $model
117      */
118 
119     public function model(\Slimore\Mvc\Model $model)
120     {
121         $this->model = $model;
122 
123         return $model;
124     }
125 
126     /**
127      * Sql query
128      *
129      * @param bool $toJson
130      * @return array
131      */
132 
133     public function query()
134     {
135         $model       = $this->model;
136 
137         $this->query = $model->select($this->select)
138                             ->where($this->where)
139                             ->orderBy($this->orderBy, $this->sortBy)
140                             ->take($this->num)
141                             ->offset($this->offset);
142 
143         $this->sql   = $this->query->getQuery()->toSql();
144 
145         $this->data  = $this->query->get()->toArray();
146 
147         return $this->data;
148     }
149 
150     /**
151      * @return mixed
152      */
153 
154     public function getQuery()
155     {
156         return $this->query;
157     }
158 
159     /**
160      * @return string
161      */
162 
163     public function getSql()
164     {
165         return $this->sql;
166     }
167 
168     /**
169      * @param bool $sql false
170      * @return array
171      */
172 
173     public function params($sql = false)
174     {
175         $this->params = [
176             'sql'       => ($sql) ? $this->sql : '',
177             'num'       => $this->num,
178             'data'      => $this->data,
179             'page'      => $this->page,
180             'prev'      => $this->prev,
181             'next'      => $this->next,
182             'last'      => $this->last,
183             'first'     => $this->first,
184             'range'     => $this->range,
185             'total'     => $this->total,
186             'ranges'    => $this->ranges,
187             'offset'    => $this->offset,
188             'baseUrl'   => $this->baseUrl,
189             'pageTotal' => $this->pageTotal
190         ];
191 
192         return $this->params;
193     }
194 
195     /**
196      * @param bool $sql true
197      */
198 
199     public function debug($sql = true)
200     {
201         Debug::printr($this->params($sql));
202     }
203 
204     /**
205      * @param bool $sql  true
206      * @param bool $return  false
207      */
208 
209     public function json($sql = true, $return = false)
210     {
211         Debug::json($this->params($sql), $return);
212     }
213 }
Slimore API documentation generated by ApiGen