Pagenation::genNext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 12
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
/**
3
 * Pagenation
4
 *
5
 * ページネーションのためのファンクション群
6
 *
7
 * @package           risoluto
8
 * @author            Risoluto Developers
9
 * @license           http://opensource.org/licenses/bsd-license.php new BSD license
10
 * @copyright     (C) 2008-2015 Risoluto Developers / All Rights Reserved.
11
 */
12
13
//------------------------------------------------------//
14
// 名前空間の定義
15
//------------------------------------------------------//
16
namespace Risoluto;
17
18
//------------------------------------------------------//
19
// クラス定義
20
//------------------------------------------------------//
21
class Pagenation
22
{
23
    //------------------------------------------------------//
24
    // クラス変数定義
25
    //------------------------------------------------------//
26
    /**
27
     * $separatorText
28
     * @access private
29
     * @var    string    セパレータのテキスト
30
     */
31
    private $separatorText = '&nbsp;|&nbsp;';
32
    /**
33
     * $firstLinkText
34
     * @access private
35
     * @var    string    「最初へ」のテキスト
36
     */
37
    private $firstLinkText = '&lt;&lt;First';
38
    /**
39
     * $prevLinkText
40
     * @access private
41
     * @var    string    「前へ」のテキスト
42
     */
43
    private $prevLinkText = '&lt;Prev';
44
    /**
45
     * $nextLinkText
46
     * @access private
47
     * @var    string    「次へ」のテキスト
48
     */
49
    private $nextLinkText = 'Next&gt;';
50
    /**
51
     * $lastLinkText
52
     * @access private
53
     * @var    string    「最後へ」のテキスト
54
     */
55
    private $lastLinkText = 'Last&gt;&gt;';
56
57
    /**
58
     * $classLink
59
     * @access private
60
     * @var    string    リンクになっている文字列のスタイルが定義されたクラス名
61
     */
62
    private $classLink = 'pagenation_link';
63
    /**
64
     * $classNoLink
65
     * @access private
66
     * @var    string    リンクになっていない文字列のスタイルが定義されたクラス名
67
     */
68
    private $classNoLink = 'pagenation_nolink';
69
70
    //------------------------------------------------------//
71
    // クラスメソッド定義
72
    //------------------------------------------------------//
73
    /**
74
     * genLinkUrl($page)
75
     *
76
     * PagenationのリンクURLを生成する
77
     *
78
     * @access    private
79
     *
80
     * @param     integer $page ページ番号
81
     *
82
     * @return    string ベースとなるURL
83
     */
84
    private function genLinkUrl( $page )
85
    {
86
        // 生成したベースURLを返却
87
        return str_replace( array ( '/&', '//' ), array ( '/?', '/' ),
88
            preg_replace( '/(&|\?)page=\d{1,}/', '', $_SERVER[ 'REQUEST_URI' ] ) . '&page=' . $page );
89
    }
90
91
    /**
92
     * genPagenation($current_page, $total_page, $max_pager_number = 10)
93
     *
94
     * Pagenationリンクテキストを生成する
95
     *
96
     * @access    private
97
     *
98
     * @param     integer $current_page 現在のページ数
99
     * @param     integer $total_page 合計ページ数
100
     * @param     integer $max_pager_number Pagenationリンクとして表示する最大数
101
     *
102
     * @return    array Pagenationリンク情報
103
     */
104
    private function genPagenation( $current_page, $total_page, $max_pager_number = 10 )
105
    {
106
        // Pagenationリンク情報を格納する変数を初期化
107
        $pager_link = '';
108
109
        // 「最初へ」リンクを生成
110
        $pager_link .= $this->genFirst( $current_page );
111
        // 「前へ」リンクを生成
112
        $pager_link .= $this->genPrev( $current_page, $total_page );
113
        // 数値リンクを生成
114
        $pager_link .= $this->genNum( $current_page, $total_page, $max_pager_number );
115
        // 「次へ」リンクを生成
116
        $pager_link .= $this->genNext( $current_page, $total_page );
117
        // 「最後へ」リンクを生成
118
        $pager_link .= $this->genLast( $current_page, $total_page );
119
120
        // 生成したリンクを返却
121
        return $pager_link;
122
    }
123
124
    /**
125
     * genFirst($current_page)
126
     *
127
     * 「最初へ」リンクを生成する
128
     *
129
     * @access    private
130
     *
131
     * @param     integer $current_page 現在のページ数
132
     *
133
     * @return    array Pagenationリンク情報
134
     */
135 View Code Duplication
    private function genFirst( $current_page )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        if ($current_page <= 1) {
138
            // 現在のページが1以下だったら通常のテキストを返却する
139
            $retval = '<span class="' . $this->classNoLink . '">' . $this->$firstLinkText . '</span>';
0 ignored issues
show
Bug introduced by
The variable $firstLinkText does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
140
        } else {
141
            // そうでなければリンクテキストを返却する
142
            $retval = '<a href="' . $this->genLinkUrl( 1 ) . '" class="' . $this->classLink . '">' . $this->$firstLinkText . '</a>';
143
        }
144
145
        return $retval . $this->separatorText;
146
    }
147
148
    /**
149
     * genPrev($current_page, $total_page)
150
     *
151
     * 「前へ」リンクを生成する
152
     *
153
     * @access    private
154
     *
155
     * @param     integer $current_page 現在のページ数
156
     * @param     integer $total_page 合計ページ数
157
     *
158
     * @return    array Pagenationリンク情報
159
     */
160 View Code Duplication
    private function genPrev( $current_page, $total_page )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        if ($current_page <= 1 or $total_page <= 1) {
163
            // 現在のページが1以下または合計ページ数が1だったら通常のテキストを返却する
164
            $retval = '<span class="' . $this->classNoLink . '">' . $this->prevLinkText . '</span>';
165
        } else {
166
            // そうでなければリンクテキストを返却する
167
            $retval = '<a href="' . $this->genLinkUrl( $current_page - 1 ) . '" class="' . $this->classLink . '">' . $this->prevLinkText . '</a>';
168
        }
169
170
        return $retval . $this->separatorText;
171
    }
172
173
    /**
174
     * genNum($current_page, $total_page, $max_pager_number = 10)
175
     *
176
     * 数値リンクを生成する
177
     *
178
     * @access    private
179
     *
180
     * @param     integer $current_page 現在のページ数
181
     * @param     integer $total_page 合計ページ数
182
     * @param     integer $max_pager_number Pagenationリンクとして表示する最大数
183
     *
184
     * @return    array Pagenationリンク情報
185
     */
186
    private function genNum( $current_page, $total_page, $max_pager_number = 10 )
187
    {
188
        $retval[ ] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$retval was never initialized. Although not strictly required by PHP, it is generally a good practice to add $retval = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
189
190
        // 比較基準となる数値の計算を実施
191
        $threshold = ceil( $max_pager_number / $current_page );
192
        $fix_num = round( $max_pager_number / 2 );
193
194
        if ($total_page == $max_pager_number) {
195
            $begin_number = 1;
196
            $end_number = $max_pager_number;
197
        } else {
198
            if ($total_page < $max_pager_number) {
199
                $begin_number = 1;
200
                $end_number = $total_page;
201
            } else {
202
                // 最大ページが最大表示数より大きければスタート位置を調整する
203
                if ($current_page <= $threshold + ceil( $fix_num / 2 )) {
204
                    // 現在ページが1~(最大表示数/2)までの時
205
                    $begin_number = 1;
206
                    $end_number = $max_pager_number;
207
                } else {
208
                    if (( $total_page - $current_page ) < $fix_num) {
209
                        // 現在ページがn~最大ページの時
210
                        $begin_number = $total_page - $max_pager_number;
211
                        $end_number = $total_page;
212
                    } else {
213
                        // それ以外
214
                        $begin_number = $current_page - $fix_num;
215
                        $end_number = $current_page + $fix_num - 1;
216
                    }
217
                }
218
            }
219
        }
220
221
        // Pagenationの数値リンクを生成
222
        for ($i = $begin_number; $i <= $end_number; $i++) {
223
            if ($i == $current_page) {
224
                $retval[ ] = '<span class="' . $this->classNoLink . '">' . $i . '</span>';
225
            } else {
226
                // そうでなければリンクテキストを返却する
227
                $retval[ ] = '<a href="' . $this->genLinkUrl( $i ) . '" class="' . $this->classLink . '">' . $i . '</a>';
228
            }
229
        }
230
231
        return implode( $this->separatorText, $retval );
232
    }
233
234
    /**
235
     * genNext($current_page, $total_page)
236
     *
237
     * 「次へ」リンクを生成する
238
     *
239
     * @access    private
240
     *
241
     * @param     integer $current_page 現在のページ数
242
     * @param     integer $total_page 合計ページ数
243
     *
244
     * @return    array Pagenationリンク情報
245
     */
246 View Code Duplication
    private function genNext( $current_page, $total_page )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
247
    {
248
        if ($current_page >= $total_page) {
249
            // 現在のページが最大ページ以上だったら通常のテキストを返却する
250
            $retval = '<span class="' . $this->classNoLink . '">' . $this->nextLinkText . '</span>';
251
        } else {
252
            // そうでなければリンクテキストを返却する
253
            $retval = '<a href="' . $this->genLinkUrl( $current_page + 1 ) . '" class="' . $this->classLink . '">' . $this->nextLinkText . '</a>';
254
        }
255
256
        return $this->separatorText . $retval;
257
    }
258
259
    /**
260
     * genLast($current_page, $total_page)
261
     *
262
     * 「最後へ」リンクを生成する
263
     *
264
     * @access    private
265
     *
266
     * @param     integer $current_page 現在のページ数
267
     * @param     integer $total_page 合計ページ数
268
     *
269
     * @return    array Pagenationリンク情報
270
     */
271 View Code Duplication
    private function genLast( $current_page, $total_page )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
    {
273
        if ($current_page >= $total_page) {
274
            // 現在のページが最大ページ以上だったら通常のテキストを返却する
275
            $retval = '<span class="' . $this->classNoLink . '">' . $this->lastLinkText . '</span>';
276
        } else {
277
            // そうでなければリンクテキストを返却する
278
            $retval = '<a href="' . $this->genLinkUrl( $total_page ) . '" class="' . $this->classLink . '">' . $this->lastLinkText . '</a>';
279
        }
280
281
        return $this->separatorText . $retval;
282
    }
283
284
    /**
285
     * setSeparatorText($text)
286
     *
287
     * セパレータテキストをセットする
288
     *
289
     * @access    public
290
     *
291
     * @param     string $text 設定するテキスト
292
     *
293
     * @return    void
294
     */
295
    public function setSeparatorText( $text )
296
    {
297
        $this->separatorText = $text;
298
    }
299
300
    /**
301
     * setFirstLinkText($text)
302
     *
303
     * 「最初へ」のテキストをセットする
304
     *
305
     * @access    public
306
     *
307
     * @param     string $text 設定するテキスト
308
     *
309
     * @return    void
310
     */
311
    public function setFirstLinkText( $text )
312
    {
313
        $this->firstLinkText = $text;
314
    }
315
316
    /**
317
     * setPrevLinkText($text)
318
     *
319
     * 「前へ」のテキストをセットする
320
     *
321
     * @access    public
322
     *
323
     * @param     string $text 設定するテキスト
324
     *
325
     * @return    void
326
     */
327
    public function setPrevLinkText( $text )
328
    {
329
        $this->prevLinkText = $text;
330
    }
331
332
    /**
333
     * setNextLinkText($text)
334
     *
335
     * 「次へ」のテキストをセットする
336
     *
337
     * @access    public
338
     *
339
     * @param     string $text 設定するテキスト
340
     *
341
     * @return    void
342
     */
343
    public function setNextLinkText( $text )
344
    {
345
        $this->nextLinkText = $text;
346
    }
347
348
    /**
349
     * setLastLinkText($text)
350
     *
351
     * 「最後へ」のテキストをセットする
352
     *
353
     * @access    public
354
     *
355
     * @param     string $text 設定するテキスト
356
     *
357
     * @return    void
358
     */
359
    public function setLastLinkText( $text )
360
    {
361
        $this->lastLinkText = $text;
362
    }
363
364
    /**
365
     * setClassLink($text)
366
     *
367
     * リンクになっている文字列のスタイルが定義されたクラス名をセットする
368
     *
369
     * @access    public
370
     *
371
     * @param     string $text 設定するテキスト
372
     *
373
     * @return    void
374
     */
375
    public function setClassLink( $text )
376
    {
377
        $this->classLink = $text;
378
    }
379
380
    /**
381
     * setClassNoLink($text)
382
     *
383
     * リンクになっていない文字列のスタイルが定義されたクラス名をセットする
384
     *
385
     * @access    public
386
     *
387
     * @param     string $text 設定するテキスト
388
     *
389
     * @return    void
390
     */
391
    public function setClassNoLink( $text )
392
    {
393
        $this->classNoLink = $text;
394
    }
395
396
    /**
397
     * getPagenation($current_page, $total_count, $per_page, $max_pager_number = 10)
398
     *
399
     * Pagenationを生成する
400
     *
401
     * @access    public
402
     *
403
     * @param     integer $current_page 現在のページ数
404
     * @param     integer $total_count 合計件数
405
     * @param     integer $per_page 1ページあたりの件数
406
     * @param     integer $max_pager_number Pagenationリンクとして表示する最大数
407
     *
408
     * @return    array Pagenationに関する情報が格納された連想配列
409
     */
410
    public function getPagenation( $current_page, $total_count, $per_page, $max_pager_number = 10 )
411
    {
412
        // 合計ページ数を算出
413
        $total_page = ceil( $total_count / $per_page );
414
415
        // 現在ページが合計ページを超過していたら、現在ページを合計ページにする
416
        if ($current_page > $total_page) {
417
            $current_page = $total_page;
418
        }
419
420
        // 取得結果を返却
421
        return [
422
            'PagerLink' => str_replace(
423
                $this->separatorText . $this->separatorText,
424
                $this->separatorText,
425
                $this->genPagenation( $current_page, $total_page, $max_pager_number )
426
            ),
427
            'CurrentPage' => $current_page,
428
            'TotalPage' => $total_page,
429
        ];
430
    }
431
}