Passed
Branch master (d24529)
by BENOIT
02:55
created
src/BenTools/Pager/Page.php 1 patch
Braces   +33 added lines, -28 removed lines patch added patch discarded remove patch
@@ -72,8 +72,9 @@  discard block
 block discarded – undo
72 72
      * @return int
73 73
      */
74 74
     public function getOffset() {
75
-        if (!$this->offset)
76
-            $this->offset   =   ($this->getIndex() * $this->getPager()->getResultsPerPage());
75
+        if (!$this->offset) {
76
+                    $this->offset   =   ($this->getIndex() * $this->getPager()->getResultsPerPage());
77
+        }
77 78
         return $this->offset;
78 79
     }
79 80
 
@@ -82,8 +83,9 @@  discard block
 block discarded – undo
82 83
      * @return int
83 84
      */
84 85
     public function getResultCount() {
85
-        if (!$this->resultCount)
86
-            $this->resultCount   =   $this->isLastPage() ? $this->getPager()->getTotalResultCount() - $this->getOffset() : $this->getPager()->getResultsPerPage();
86
+        if (!$this->resultCount) {
87
+                    $this->resultCount   =   $this->isLastPage() ? $this->getPager()->getTotalResultCount() - $this->getOffset() : $this->getPager()->getResultsPerPage();
88
+        }
87 89
         return $this->resultCount;
88 90
     }
89 91
 
@@ -92,8 +94,9 @@  discard block
 block discarded – undo
92 94
      * @return bool
93 95
      */
94 96
     public function isFirstPage() {
95
-        if (!$this->isFirstPage)
96
-            $this->isFirstPage    =    ($this->getIteration() == 1);
97
+        if (!$this->isFirstPage) {
98
+                    $this->isFirstPage    =    ($this->getIteration() == 1);
99
+        }
97 100
         return $this->isFirstPage;
98 101
     }
99 102
 
@@ -102,8 +105,9 @@  discard block
 block discarded – undo
102 105
      * @return bool
103 106
      */
104 107
     public function isLastPage() {
105
-        if (!$this->isLastPage)
106
-            $this->isLastPage    =    ($this->getIteration() == $this->pager->getNbPages());
108
+        if (!$this->isLastPage) {
109
+                    $this->isLastPage    =    ($this->getIteration() == $this->pager->getNbPages());
110
+        }
107 111
         return $this->isLastPage;
108 112
     }
109 113
 
@@ -112,8 +116,9 @@  discard block
 block discarded – undo
112 116
      * @return bool
113 117
      */
114 118
     public function isCurrentPage() {
115
-        if (!$this->isCurrentPage)
116
-            $this->isCurrentPage    =    ($this->getIteration() == ($this->pager->getCurrentPageIteration()));
119
+        if (!$this->isCurrentPage) {
120
+                    $this->isCurrentPage    =    ($this->getIteration() == ($this->pager->getCurrentPageIteration()));
121
+        }
117 122
         return $this->isCurrentPage;
118 123
     }
119 124
 
@@ -122,8 +127,9 @@  discard block
 block discarded – undo
122 127
      * @return bool
123 128
      */
124 129
     public function isPreviousPage() {
125
-        if (!$this->isPreviousPage)
126
-            $this->isPreviousPage    =    ($this->getIteration() == ($this->pager->getCurrentPageIteration() - 1));
130
+        if (!$this->isPreviousPage) {
131
+                    $this->isPreviousPage    =    ($this->getIteration() == ($this->pager->getCurrentPageIteration() - 1));
132
+        }
127 133
         return $this->isPreviousPage;
128 134
     }
129 135
 
@@ -132,8 +138,9 @@  discard block
 block discarded – undo
132 138
      * @return bool
133 139
      */
134 140
     public function isNextPage() {
135
-        if (!$this->isNextPage)
136
-            $this->isNextPage    =    ($this->getIteration() == ($this->pager->getCurrentPageIteration() + 1));
141
+        if (!$this->isNextPage) {
142
+                    $this->isNextPage    =    ($this->getIteration() == ($this->pager->getCurrentPageIteration() + 1));
143
+        }
137 144
         return $this->isNextPage;
138 145
     }
139 146
 
@@ -168,18 +175,17 @@  discard block
 block discarded – undo
168 175
     public function getUrl() {
169 176
 
170 177
         if ($this->pager->getRewriteString()) {
171
-            if (preg_match('#' . sprintf($this->pager->getRewriteString(), '([0-9]+)') . '#', (string) $this->pager->getUrl()))
172
-                $this->setUrl(new Url(preg_replace('#' . sprintf($this->pager->getRewriteString(), '([0-9]+)') . '#', sprintf($this->pager->getRewriteString(), $this->getIteration()), (string) $this->pager->getUrl())));
173
-            else {
178
+            if (preg_match('#' . sprintf($this->pager->getRewriteString(), '([0-9]+)') . '#', (string) $this->pager->getUrl())) {
179
+                            $this->setUrl(new Url(preg_replace('#' . sprintf($this->pager->getRewriteString(), '([0-9]+)') . '#', sprintf($this->pager->getRewriteString(), $this->getIteration()), (string) $this->pager->getUrl())));
180
+            } else {
174 181
                 $url        =   clone $this->getPager()->getUrl();
175 182
                 $this->setUrl($url->appendToPath(sprintf($this->getPager()->getRewriteString(), $this->getIteration())));
176 183
             }
177 184
 
185
+        } else {
186
+                    $this->setUrl(Url::NewInstance($this->pager->getUrl())->setParam($this->pager->getQueryParam(), $this->getIteration()));
178 187
         }
179 188
 
180
-        else
181
-            $this->setUrl(Url::NewInstance($this->pager->getUrl())->setParam($this->pager->getQueryParam(), $this->getIteration()));
182
-
183 189
         return $this->url;
184 190
     }
185 191
 
@@ -216,14 +222,13 @@  discard block
 block discarded – undo
216 222
      * @return mixed|null
217 223
      */
218 224
     public function offsetGet($offset) {
219
-        if (is_callable([$this, 'get' . $offset]))
220
-            return call_user_func([$this, 'get' . $offset]);
221
-
222
-        elseif (is_callable([$this, $offset]))
223
-            return call_user_func([$this, $offset]);
224
-
225
-        else
226
-            return null;
225
+        if (is_callable([$this, 'get' . $offset])) {
226
+                    return call_user_func([$this, 'get' . $offset]);
227
+        } elseif (is_callable([$this, $offset])) {
228
+                    return call_user_func([$this, $offset]);
229
+        } else {
230
+                    return null;
231
+        }
227 232
     }
228 233
 
229 234
     /**
Please login to merge, or discard this patch.
src/BenTools/Pager/Pager.php 1 patch
Braces   +55 added lines, -50 removed lines patch added patch discarded remove patch
@@ -112,17 +112,15 @@  discard block
 block discarded – undo
112 112
 
113 113
     public function __construct($url = null, $resultsPerPage = null, $totalResultCount = null, $queryParam = self::DEFAULT_QUERY_PARAM, $rewriteString = null, $currentPageIteration = null, $delta = self::DEFAULT_DELTA) {
114 114
 
115
-        if ($url instanceof Url)
116
-            $this->setUrl($url);
117
-
118
-        elseif ($url)
119
-            $this->setUrl(new Url($url));
120
-
121
-        elseif (isset($_SERVER['REQUEST_URI']))
122
-            $this->setUrl(new Url($_SERVER['REQUEST_URI']));
123
-
124
-        else
125
-            $this->setUrl(new Url('/'));
115
+        if ($url instanceof Url) {
116
+                    $this->setUrl($url);
117
+        } elseif ($url) {
118
+                    $this->setUrl(new Url($url));
119
+        } elseif (isset($_SERVER['REQUEST_URI'])) {
120
+                    $this->setUrl(new Url($_SERVER['REQUEST_URI']));
121
+        } else {
122
+                    $this->setUrl(new Url('/'));
123
+        }
126 124
 
127 125
         $this   ->  setResultsPerPage($resultsPerPage)
128 126
                 ->  setTotalResultCount($totalResultCount)
@@ -184,10 +182,11 @@  discard block
 block discarded – undo
184 182
      */
185 183
     public function getCurrentPageIteration() {
186 184
         if (!$this->currentPageIteration) {
187
-            if (!is_null($this->rewriteString) && preg_match('#' . sprintf($this->rewriteString, '([0-9]+)') . '#', (string) $this->url, $matches))
188
-                $this->currentPageIteration = (int) ((isset($matches[1]) && $matches[1] > 0) ? $matches[1] : 1);
189
-            else
190
-                $this->currentPageIteration = ($this->url->getParam($this->queryParam)) ? (int) $this->url->getParam($this->queryParam) : 1;
185
+            if (!is_null($this->rewriteString) && preg_match('#' . sprintf($this->rewriteString, '([0-9]+)') . '#', (string) $this->url, $matches)) {
186
+                            $this->currentPageIteration = (int) ((isset($matches[1]) && $matches[1] > 0) ? $matches[1] : 1);
187
+            } else {
188
+                            $this->currentPageIteration = ($this->url->getParam($this->queryParam)) ? (int) $this->url->getParam($this->queryParam) : 1;
189
+            }
191 190
         }
192 191
         return (int) $this->currentPageIteration;
193 192
 
@@ -234,10 +233,11 @@  discard block
 block discarded – undo
234 233
      * @return Page Instance
235 234
      */
236 235
     public function getPreviousPage() {
237
-        if ($this->getCurrentPageIteration() - 1 >= 1)
238
-            return $this->getPageInstance($this->getCurrentPageIteration() - 1);
239
-        else
240
-            return $this->getFirstPage();
236
+        if ($this->getCurrentPageIteration() - 1 >= 1) {
237
+                    return $this->getPageInstance($this->getCurrentPageIteration() - 1);
238
+        } else {
239
+                    return $this->getFirstPage();
240
+        }
241 241
     }
242 242
 
243 243
     /**
@@ -246,10 +246,11 @@  discard block
 block discarded – undo
246 246
      * @return Page Instance
247 247
      */
248 248
     public function getNextPage() {
249
-        if ($this->getCurrentPageIteration() + 1 <= $this->getNbPages())
250
-            return $this->getPageInstance($this->getCurrentPageIteration() + 1);
251
-        else
252
-            return $this->getLastPage();
249
+        if ($this->getCurrentPageIteration() + 1 <= $this->getNbPages()) {
250
+                    return $this->getPageInstance($this->getCurrentPageIteration() + 1);
251
+        } else {
252
+                    return $this->getLastPage();
253
+        }
253 254
     }
254 255
 
255 256
     /**
@@ -269,10 +270,12 @@  discard block
 block discarded – undo
269 270
      */
270 271
     public function run() {
271 272
 
272
-        if (!$this->resultsPerPage)
273
-            throw new PagerException(get_called_class() . "::setResultsPerPage has to be called first.");
274
-        if (!is_integer($this->totalResultCount))
275
-            throw new PagerException(get_called_class() . "::setTotalResultCount has to be called first.");
273
+        if (!$this->resultsPerPage) {
274
+                    throw new PagerException(get_called_class() . "::setResultsPerPage has to be called first.");
275
+        }
276
+        if (!is_integer($this->totalResultCount)) {
277
+                    throw new PagerException(get_called_class() . "::setTotalResultCount has to be called first.");
278
+        }
276 279
 
277 280
         $this->pages        =   [];
278 281
 
@@ -293,27 +296,31 @@  discard block
 block discarded – undo
293 296
 
294 297
                 ####    SUPERIOR DELTA        ####
295 298
                 # If the next page doesn't overtstep the last one
296
-                if ($this->getCurrentPageIteration() + $i <= $this->getNbPages())
297
-                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() + $i);
299
+                if ($this->getCurrentPageIteration() + $i <= $this->getNbPages()) {
300
+                                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() + $i);
301
+                }
298 302
 
299 303
                 # Otherwise, we put the remaining delta before, providing it doesn't overstep the 1st one
300
-                elseif ($this->getCurrentPageIteration() - $i >= 1)
301
-                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() - $i);
304
+                elseif ($this->getCurrentPageIteration() - $i >= 1) {
305
+                                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() - $i);
306
+                }
302 307
 
303 308
                 ####    INFERIOR DELTA        ####
304 309
                 # If the previous page doesn't overstep the 1st one
305
-                if ($this->getCurrentPageIteration() - $i >= 1)
306
-                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() - $i);
310
+                if ($this->getCurrentPageIteration() - $i >= 1) {
311
+                                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() - $i);
312
+                }
307 313
 
308 314
                 # Otherwise, we put the remaining delta before, providing it doesn't overstep the last one
309
-                elseif ($this->getCurrentPageIteration() + $i <= $this->getNbPages())
310
-                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() + $i);
315
+                elseif ($this->getCurrentPageIteration() + $i <= $this->getNbPages()) {
316
+                                    $this->pages[] = $this->getPageInstance($this->getCurrentPageIteration() + $i);
317
+                }
311 318
 
312 319
             }
313
-        }
314
-        else {
315
-            for ($i = 1; $i <= $this->getNbPages(); $i++)
316
-                $this->pages[]  =   $this->getPageInstance($i);
320
+        } else {
321
+            for ($i = 1; $i <= $this->getNbPages(); $i++) {
322
+                            $this->pages[]  =   $this->getPageInstance($i);
323
+            }
317 324
         }
318 325
         $this->pages = array_unique((array) $this->pages);
319 326
         sort($this->pages);
@@ -487,17 +494,15 @@  discard block
 block discarded – undo
487 494
      */
488 495
     public function offsetGet($offset) {
489 496
 
490
-        if (is_numeric($offset))
491
-            return array_key_exists($offset, $this->getPages()) ? $this->getPages()[$offset] : null;
492
-
493
-        elseif (is_callable([$this, 'get' . $offset]))
494
-            return call_user_func([$this, 'get' . $offset]);
495
-
496
-        elseif (is_callable([$this, 'is' . $offset]))
497
-            return call_user_func([$this, 'is' . $offset]);
498
-
499
-        else
500
-            return null;
497
+        if (is_numeric($offset)) {
498
+                    return array_key_exists($offset, $this->getPages()) ? $this->getPages()[$offset] : null;
499
+        } elseif (is_callable([$this, 'get' . $offset])) {
500
+                    return call_user_func([$this, 'get' . $offset]);
501
+        } elseif (is_callable([$this, 'is' . $offset])) {
502
+                    return call_user_func([$this, 'is' . $offset]);
503
+        } else {
504
+                    return null;
505
+        }
501 506
     }
502 507
 
503 508
     /**
Please login to merge, or discard this patch.