sokil /
php-mongo
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of the PHPMongo package. |
||
| 5 | * |
||
| 6 | * (c) Dmytro Sokil <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Sokil\Mongo; |
||
| 13 | |||
| 14 | class Paginator implements \Iterator |
||
| 15 | { |
||
| 16 | private $currentPage = 1; |
||
| 17 | |||
| 18 | private $itemsOnPage = 30; |
||
| 19 | |||
| 20 | private $totalRowsCount; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * |
||
| 24 | * @var \Sokil\Mongo\Cursor |
||
| 25 | */ |
||
| 26 | private $cursor; |
||
| 27 | |||
| 28 | public function __construct(Cursor $cursor = null) |
||
| 29 | { |
||
| 30 | if ($cursor !== null) { |
||
| 31 | $this->setCursor($cursor); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | public function __destruct() |
||
| 36 | { |
||
| 37 | $this->cursor = null; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @param int $itemsOnPage |
||
| 43 | * @return Paginator |
||
| 44 | */ |
||
| 45 | public function setItemsOnPage($itemsOnPage) |
||
| 46 | { |
||
| 47 | $this->itemsOnPage = (int) $itemsOnPage; |
||
| 48 | |||
| 49 | // define offset |
||
| 50 | $this->applyLimits(); |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | * @param int $currentPage |
||
| 58 | * @return \Sokil\Mongo\Paginator |
||
| 59 | */ |
||
| 60 | public function setCurrentPage($currentPage) |
||
| 61 | { |
||
| 62 | $this->currentPage = (int) $currentPage; |
||
| 63 | |||
| 64 | // define offset |
||
| 65 | $this->applyLimits(); |
||
| 66 | |||
| 67 | return $this; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getCurrentPage() |
||
| 71 | { |
||
| 72 | // check if current page number greater than max allowed |
||
| 73 | $totalPageCount = $this->getTotalPagesCount(); |
||
| 74 | |||
| 75 | // no document found - page is 1 |
||
| 76 | if (0 === $totalPageCount) { |
||
| 77 | return 1; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->currentPage <= $totalPageCount) { |
||
| 81 | $currentPage = $this->currentPage; |
||
| 82 | } else { |
||
| 83 | $currentPage = $totalPageCount; |
||
| 84 | } |
||
| 85 | |||
| 86 | return $currentPage; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Define cursor for paginator |
||
| 91 | * |
||
| 92 | * @param \Sokil\Mongo\Cursor $cursor |
||
| 93 | * @return \Sokil\Mongo\Paginator |
||
| 94 | */ |
||
| 95 | public function setCursor(Cursor $cursor) |
||
| 96 | { |
||
| 97 | $this->cursor = clone $cursor; |
||
| 98 | |||
| 99 | $this->applyLimits(); |
||
| 100 | |||
| 101 | return $this; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getTotalRowsCount() |
||
| 105 | { |
||
| 106 | if (null !== $this->totalRowsCount) { |
||
| 107 | return $this->totalRowsCount; |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->totalRowsCount = $this->cursor->count(); |
||
| 111 | |||
| 112 | return $this->totalRowsCount; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | public function getTotalPagesCount() |
||
| 119 | { |
||
| 120 | return (int) ceil($this->getTotalRowsCount() / $this->itemsOnPage); |
||
| 121 | } |
||
| 122 | |||
| 123 | private function applyLimits() |
||
| 124 | { |
||
| 125 | if (!$this->cursor) { |
||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | $currentPage = $this->getCurrentPage(); |
||
| 130 | |||
| 131 | // get page of rows |
||
| 132 | $this->cursor |
||
| 133 | ->limit($this->itemsOnPage) |
||
| 134 | ->skip(($currentPage - 1) * $this->itemsOnPage); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function rewind() |
||
| 141 | { |
||
| 142 | $this->cursor->rewind(); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function valid() |
||
| 149 | { |
||
| 150 | return $this->cursor->valid(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return Document |
||
| 155 | */ |
||
| 156 | public function current() |
||
| 157 | { |
||
| 158 | return $this->cursor->current(); |
||
|
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function key() |
||
| 165 | { |
||
| 166 | return $this->cursor->key(); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function next() |
||
| 173 | { |
||
| 174 | $this->cursor->next(); |
||
| 175 | } |
||
| 176 | } |
||
| 177 |