Passed
Push — master ( 8c496b...239a6a )
by sabaku
09:33 queued 03:12
created

Util::getHeaderParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 12
cts 12
cp 1
crap 4
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 11
    public static function getHeaderParams($headers, $otherParams = array())
16
    {
17 11
        $params = [];
18 11
        $otherParams = array_map('strtolower', $otherParams);
19 11
        foreach ($headers as $header => $value) {
20 11
            $header = strtolower($header);
21 11
            if (strpos($header, 'x-upyun-') !== false) {
22 11
                $params[$header] = $value[0];
23 11
            } else if (in_array($header, $otherParams)) {
24 1
                $params[$header] = $value[0];
25 1
            }
26 11
        }
27 11
        return $params;
28
    }
29
30 1
    public static function parseDir($body)
31
    {
32 1
        $files = array();
33 1
        if (!$body) {
34
            return array();
35
        }
36
37 1
        $lines = explode("\n", $body);
38 1
        foreach ($lines as $line) {
39 1
            $file = [];
40 1
            list($file['name'], $file['type'], $file['size'], $file['time']) = explode("\t", $line, 4);
41 1
            $files[] = $file;
42 1
        }
43
44 1
        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 16
    public static function encodeURI($url)
76
    {
77
        $unescaped = array(
78 16
            '%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
79 16
            '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
80 16
        );
81
        $reserved = array(
82 16
            '%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
83 16
            '%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
84 16
        );
85
        $score = array(
86
            '%23'=>'#'
87 16
        );
88 16
        return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score));
89
    }
90
}
91