supernova-ws /
SuperNova
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by Gorlum 25.11.2017 18:49 |
||
| 4 | */ |
||
| 5 | |||
| 6 | namespace DBAL; |
||
| 7 | |||
| 8 | use mysqli_result; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class DbSqlPaging |
||
| 12 | * |
||
| 13 | * Another implementation for DbAbstractResultIterator |
||
| 14 | * |
||
| 15 | * Fetches data from string SQL-query and pages through it |
||
| 16 | * |
||
| 17 | * @package DBAL |
||
| 18 | */ |
||
| 19 | class DbSqlPaging extends DbAbstractResultIterator { |
||
| 20 | protected $db = null; |
||
| 21 | protected $sqlQuery; |
||
| 22 | protected $pageSize = PAGING_PAGE_SIZE_DEFAULT; |
||
| 23 | protected $currentPage = 1; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * DbSqlPaging constructor. |
||
| 27 | * |
||
| 28 | * @param string $sqlQuery |
||
| 29 | * @param int $pageSize |
||
| 30 | * @param int $currentPage |
||
| 31 | * @param \DBAL\db_mysql|null $db |
||
| 32 | */ |
||
| 33 | public function __construct($sqlQuery, $pageSize = PAGING_PAGE_SIZE_DEFAULT, $currentPage = 1, $db = null) { |
||
| 34 | $this->sqlQuery = $sqlQuery; |
||
| 35 | $this->pageSize = max(intval($pageSize), PAGING_PAGE_SIZE_MINIMUM); |
||
| 36 | $this->currentPage = max(intval($currentPage), 1); |
||
| 37 | |||
| 38 | $this->db = isset($db) ? $db : \SN::$gc->db; |
||
| 39 | |||
| 40 | $this->query(); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @inheritdoc |
||
| 45 | */ |
||
| 46 | public function valid() { |
||
| 47 | return parent::valid() && $this->counter < $this->getRecordsOnCurrentPage(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @inheritdoc |
||
| 52 | */ |
||
| 53 | public function count() { |
||
| 54 | return $this->mysqli_result instanceof mysqli_result ? $this->getRecordsOnCurrentPage() : 0; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @inheritdoc |
||
| 59 | */ |
||
| 60 | protected function seekToFirst() { |
||
| 61 | if ($this->mysqli_result instanceof mysqli_result) { |
||
| 62 | $this->mysqli_result->data_seek($this->getPageZeroRecordNum()); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get current page number |
||
| 68 | * |
||
| 69 | * @return int |
||
| 70 | */ |
||
| 71 | public function getCurrentPageNumber() { |
||
| 72 | return $this->currentPage; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get total number of records in query |
||
| 77 | * |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | public function getTotalRecords() { |
||
| 81 | return $this->mysqli_result instanceof mysqli_result ? $this->mysqli_result->num_rows : 0; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Calculates total page numbers |
||
| 86 | * |
||
| 87 | * @return float|int |
||
| 88 | */ |
||
| 89 | public function getTotalPages() { |
||
| 90 | return $this->mysqli_result instanceof mysqli_result ? ceil($this->getTotalRecords() / $this->pageSize) : 0; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Calculates record count on current page |
||
| 95 | * |
||
| 96 | * @return int |
||
| 97 | */ |
||
| 98 | public function getRecordsOnCurrentPage() { |
||
| 99 | return min($this->pageSize, $this->getTotalRecords() - $this->getPageZeroRecordNum()); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Calculates record index from which current page starts |
||
| 104 | * |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | protected function getPageZeroRecordNum() { |
||
| 108 | return ($this->currentPage - 1) * $this->pageSize; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Queries DB for data and makes all necessary preparations |
||
| 113 | */ |
||
| 114 | protected function query() { |
||
| 115 | if ($this->mysqli_result !== null) { |
||
| 116 | unset($this->mysqli_result); |
||
| 117 | } |
||
| 118 | // Performing query |
||
| 119 | $this->mysqli_result = $this->db->doquery($this->sqlQuery); |
||
|
0 ignored issues
–
show
|
|||
| 120 | |||
| 121 | // Checking if page number within allowed ranges |
||
| 122 | if ( |
||
| 123 | // Is it not a first page? |
||
| 124 | $this->currentPage != 1 |
||
| 125 | && |
||
| 126 | // Is current page beyond limits? |
||
| 127 | $this->currentPage > $this->getTotalPages() |
||
| 128 | ) { |
||
| 129 | // Correcting page number to first |
||
| 130 | $this->currentPage = max(1, $this->getTotalPages()); |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->rewind(); |
||
| 134 | } |
||
| 135 | |||
| 136 | } |
||
| 137 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..