Passed
Push — master ( bb5e0a...fe1dc2 )
by sabaku
05:09
created

Util::md5Hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Upyun;
3
4
class Util
5
{
6
    public static function trim($str)
7
    {
8
        if (is_array($str)) {
9
            return array_map(array('Util', 'trim'), $str);
10
        } else {
11
            return trim($str);
12
        }
13
    }
14
15 8
    public static function getHeaderParams($headers)
16
    {
17 8
        $params = [];
18 8
        foreach ($headers as $header => $value) {
19 8
            $header = strtolower($header);
20 8
            if (strpos($header, 'x-upyun-') !== false) {
21 4
                $params[$header] = $value[0];
22 4
            }
23 8
        }
24 8
        return $params;
25
    }
26
27 1
    public static function parseDir($body)
28
    {
29 1
        $files = array();
30 1
        if (!$body) {
31 1
            return array();
32
        }
33
34 1
        $lines = explode("\n", $body);
35 1
        foreach ($lines as $line) {
36 1
            $file = [];
37 1
            list($file['name'], $file['type'], $file['size'], $file['time']) = explode("\t", $line, 4);
38 1
            $files[] = $file;
39 1
        }
40
41 1
        return $files;
42
    }
43
44 2
    public static function base64Json($params)
45
    {
46 2
        return base64_encode(json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
47
    }
48
49
    public static function stringifyHeaders($headers)
50
    {
51
        $return = array();
52
        foreach ($headers as $key => $value) {
53
            $return[] = "$key: $value";
54
        }
55
        return $return;
56
    }
57
58
    public static function md5Hash($resource)
59
    {
60
        rewind($resource);
61
        $ctx = hash_init('md5');
62
        hash_update_stream($ctx, $resource);
63
        $md5 = hash_final($ctx);
64
        return $md5;
65
    }
66
67
    /**
68
     * GuzzleHttp\Psr\Uri use `parse_url`,not good for utf-8,
69
     * So should `encodeURI` first, before `new Psr7\Request`
70
     * @see http://stackoverflow.com/questions/4929584/encodeuri-in-php
71
     */
72 13
    public static function encodeURI($url)
73
    {
74
        $unescaped = array(
75 13
            '%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
76 13
            '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
77 13
        );
78
        $reserved = array(
79 13
            '%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
80 13
            '%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
81 13
        );
82
        $score = array(
83
            '%23'=>'#'
84 13
        );
85 13
        return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score));
86
    }
87
}
88