Completed
Push — master ( c8a547...8676e4 )
by sabaku
03:53
created

Util   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0

7 Methods

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