Passed
Push — v6 ( 3d5ed9...fad304 )
by 光春
03:00
created

Urls::retrieve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
declare (strict_types=1);
21
22
namespace DtApp\ThinkLibrary\helper;
23
24
/**
25
 * 网址管理类
26
 * @mixin Urls
27
 * @package DtApp\ThinkLibrary\helper
28
 */
29
class Urls
30
{
31
    /**
32
     * 编码
33
     * @param string $url
34
     * @return string
35
     */
36
    public function lenCode(string $url): string
37
    {
38
        if (empty($url)) {
39
            return '';
40
        }
41
        return urlencode($url);
42
    }
43
44
    /**
45
     * 解码
46
     * @param string $url
47
     * @return string
48
     */
49
    public function deCode(string $url): string
50
    {
51
        if (empty($url)) {
52
            return '';
53
        }
54
        return urldecode($url);
55
    }
56
57
    /**
58
     * 格式化参数格式化成url参数
59
     * @param array $data
60
     * @return string
61
     */
62
    public function toParams(array $data): string
63
    {
64
        $buff = "";
65
        foreach ($data as $k => $v) {
66
            if ($k != "sign" && $v !== "" && !is_array($v)) {
67
                $buff .= $k . "=" . $v . "&";
68
            }
69
        }
70
        $buff = trim($buff, "&");
71
        return $buff;
72
    }
73
74
    /**
75
     * 判断是否为Url
76
     * @param string $url
77
     * @return bool
78
     */
79
    public function isUrl(string $url): bool
80
    {
81
        $pattern = "#(http|https)://(.*\.)?.*\..*#i";
82
        if (preg_match($pattern, $url)) {
83
            return true;
84
        } else {
85
            return false;
86
        }
87
    }
88
89
    /**
90
     * 删除协议
91
     * @param string $url
92
     * @return string
93
     */
94
    public function deleteProtocol(string $url): string
95
    {
96
        if (empty($this->isUrl($url))) {
97
            return $url;
98
        }
99
        if (strpos($url, 'https://') !== false) {
100
            return str_replace("https://", "//", $url);
101
        }
102
        if (strpos($url, 'http://') !== false) {
103
            return str_replace("http://", "//", $url);
104
        }
105
        return $url;
106
    }
107
108
    /**
109
     * 获取URL文件格式
110
     * @param string $url
111
     * @return mixed|string
112
     */
113
    public function retrieve(string $url)
114
    {
115
        $path = parse_url($url);
116
        $str = explode('.', $path['path']);
117
        return $str[1];
118
    }
119
}
120