Url   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 6
Bugs 6 Features 2
Metric Value
wmc 13
c 6
b 6
f 2
lcom 0
cbo 0
dl 0
loc 91
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getBaseUrl() 0 31 5
B redirectTo() 0 17 7
1
<?php
2
/**
3
 * Url
4
 *
5
 * Url操作のためのファンクション群
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
class Url
19
{
20
    //------------------------------------------------------//
21
    // クラスメソッド定義
22
    //------------------------------------------------------//
23
    /**
24
     * __construct()
25
     *
26
     * コンストラクタ
27
     */
28
    private function __construct()
29
    {
30
    }
31
32
    /**
33
     * getBaseUrl(array $target = array('HTTP_HOST' => 'localhost', 'SERVER_PORT' => '80', 'PHP_SELF' => '/'))
34
     *
35
     * ベースURLを取得する
36
     *
37
     * @access    public
38
     *
39
     * @param array $target
40
     *
41
     * @internal  param array $_SERVER 相当の情報が格納された配列
42
     *
43
     * @return    string    自身のベースURL
44
     */
45
    public static function getBaseUrl(
46
        array $target = [ 'HTTP_HOST' => 'localhost', 'SERVER_PORT' => '80', 'PHP_SELF' => '/' ]
47
    ) {
48
        //---スキーマ(ポートの値でHTTP/HTTPSのどちらかを判定)
49
        switch ($target[ 'SERVER_PORT' ]) {
50
            // スタンダードなHTTPS
51
            case '443': // FALL THRU
52
            case '8443': // FALL THRU
53
                $schema = 'https://';
54
                break;
55
56
            // デフォルト
57
            default:
58
                $schema = 'http://';
59
                break;
60
        }
61
62
        //---ポート(80と443以外なら「〜:PORTNUMBER」とする)
63
        $port = '';
64
        if ($target[ 'SERVER_PORT' ] != '80' and $target[ 'SERVER_PORT' ] != '443') {
65
            $port = ':' . $target[ 'SERVER_PORT' ];
66
        }
67
68
        //---ホスト名
69
        $host = $target[ 'HTTP_HOST' ];
70
71
        //---実行ファイル名(デフォルトの「index.php」が付いている場合は消す)
72
        $self = str_replace( 'index.php', '', $target[ 'PHP_SELF' ] );
73
74
        return $schema . $host . $port . $self;
75
    }
76
77
    /**
78
     * redirectTo($target = '', array $param = [], $status = '302', array $servinfo = [])
79
     *
80
     * 指定された画面へリダイレクトする
81
     *
82
     * @access    public
83
     *
84
     * @param     string $target リダイレクト先の画面識別子
85
     * @param     array  $param リダイレクト時に付与するパラメタ(連想配列指定)
86
     * @param     string $status リダイレクト時に付与するHTTPステータスコード
87
     * @param     array  $servinfo GetBaseUrlに引き渡すサーバ情報が格納された配列($_SERVER相当)
88
     *
89
     * @return    void      なし
90
     */
91
    public static function redirectTo( $target = '', array $param = [ ], $status = '302', array $servinfo = [ ] )
92
    {
93
        // ベースURLを取得する
94
        $baseUrl = self::getBaseUrl( ( !empty( $servinfo ) and is_array( $servinfo ) ) ? $servinfo : $_SERVER ) . ( !empty( $target ) ? '?seq=' . $target : '' );
95
96
        // 他のパラメタが指定されていたら、それをGETパラメタの形に生成
97
        $otherParam = '';
98
        if (!empty( $param )) {
99
            ksort( $param );
100
            foreach ($param as $key => $val) {
101
                $otherParam .= '.' . $key . ( !empty( $val ) ? '=' . $val : '' );
102
            }
103
        }
104
105
        // ヘッダを出力する
106
        header( "Location: $baseUrl$otherParam", true, $status );
107
    }
108
}