Passed
Push — master ( 21a6a4...4428d0 )
by xiaohui
03:17
created

EncodeUtils   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encodeWithURLSafeBase64() 0 8 3
A newEncodeWithUrlSafeBase64() 0 7 3
1
<?php
2
namespace AliMedia\Utils;
3
4
class EncodeUtils
5
{
6
7
    /*
8
     * URL安全的Base64编码适用于以URL方式传递Base64编码结果的场景。
9
     * 该编码方式的基本过程是先将内容以Base64格式编码为字符串,
10
     * 然后检查该结果字符串,将字符串中的加号+换成中划线-,并且将斜杠/换成下划线_,同时尾部去除等号padding。
11
     */
12
    public static function encodeWithURLSafeBase64($arg)
13
    {
14
        if ($arg === null || empty($arg)) {
15
            return null;
16
        }
17
        $result = base64_encode($arg);
18
        $result = str_replace(array('+', '/', '='), array('-', '_', ''), $result);
19
        return $result;
20
    }
21
22
    public static function newEncodeWithUrlSafeBase64($arg)
23
    {
24
        if ($arg === null || empty($arg)) {
25
            return null;
26
        }
27
        $result = preg_replace(array("/\r/", "/\n/"), "", rtrim(base64_encode($arg), '='));
28
        return $result;
29
    }
30
}
31