Test Failed
Push — master ( 465865...7c14ad )
by sabaku
04:21 queued 22s
created

Util   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 42%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 87
ccs 21
cts 50
cp 0.42
rs 10
wmc 14
lcom 0
cbo 0

7 Methods

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