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

ResourceInfo::urlencodeCh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace AliMedia\Utils;
3
4
/**文件资源类*/
5
class ResourceInfo
6
{
7
    /* 以下属性是资源ID所需的属性*/
8
    protected $namespace;                              //空间名,必须
9
    protected $dir;                                    //路径
10
    protected $name;                                   //文件名信息
11
    /* 以下属性是资源ID所需的属性*/
12
    /**
13
     * ResourceOption的构造函数
14
     */
15
    public function __construct($namespace, $dir = null, $name = null)
16
    {
17
        $this->namespace = $namespace;
18
        $this->dir = $dir;
19
        $this->name = $name;
20
    }
21
    /**检测资源ID的是否合法。$dirState表示是否检测dir,$nameState表示是否检测filename<p>
22
     * 返回格式{$isValid, $message}*/
23
    public function checkResourceInfo($dirState = false, $nameState = false)
24
    {
25
        if (empty($this->namespace)) {
26
            return array(false, "namespace is empty.[{$this->toString()}]"); // 判断是否设置空间名
27
        } else {
28
            return $this->checkFileInfo($dirState, $nameState); // 判断dir和name的合法性
29
        }
30
    }
31
    /**检测文件信息是否合法。$dirState表示是否检测dir,$nameState表示是否检测filename<p>
32
     * 返回格式{$isValid, $message}*/
33
    public function checkFileInfo($dirState = false, $nameState = false)
34
    {
35
        if ($nameState && empty($this->name)) {
36
            return array(false, "file's name is empty.[{$this->toString()}]"); // 1:若需要进行文件名name检测,则判断文件名是否为空
37
        }
38
        if ($dirState) {
39
            if (empty($this->dir)) {
40
                $this->dir = '/'; // 2:判断路径是否为空,若为空,则默认为根目录'/'
41
            } elseif (strpos($this->dir, '/') !== 0) {
42
                $this->dir = '/' . $this->dir; // 3:判断路径是否以'/'开头,若不是,则添加'/'
43
            }
44
        }
45
        return array(true, null);
46
    }
47
    /**资源ID(resourceId)的生成*/
48
    public function buildResourceId()
49
    {
50
        $jsonData = array();
51
        if (!empty($this->namespace)) {
52
            array_push($jsonData, urldecode($this->namespace));
53
        }
54
        if (!empty($this->dir)) {
55
            array_push($jsonData, urldecode($this->dir));
56
        }
57
        if (!empty($this->name)) {
58
            array_push($jsonData, urldecode($this->name));
59
        }
60
        return EncodeUtils::encodeWithURLSafeBase64(json_encode($jsonData, true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return EncodeUtils::encodeWithURLSafeBase64(json_encode($jsonData, /** @scrutinizer ignore-type */ true));
Loading history...
61
    }
62
    /**对URL中文进行编码*/
63
    protected function urlencodeCh($str)
64
    {
65
        return preg_replace_callback('/[^\0-\127]+/', function ($match) {
66
            return urlencode($match[0]);
67
        }, $str);
68
    }
69
    public function toString()
70
    {
71
        return "namespace={$this->namespace}, dir={$this->dir}, name={$this->name}";
72
    }
73
    public function toArray()
74
    {
75
        return array("namespace" => $this->namespace, "dir" => $this->dir, "name" => $this->name);
76
    }
77
    /*###################下面是属性的get和set方法###############*/
78
    /**设置路径*/
79
    public function setNamespace($namespace)
80
    {
81
        $this->namespace = $namespace;
82
        return $this;
83
    }
84
    /**设置路径*/
85
    public function setDir($dir)
86
    {
87
        $this->dir = $dir;
88
        return $this;
89
    }
90
    /**设置文件名*/
91
    public function setName($filename)
92
    {
93
        $this->name = $filename;
94
        return $this;
95
    }
96
}
97