CommonUtils::getTemporaryDirectory()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 4
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Util;
3
4
use WebStream\Exception\SystemException;
5
6
/**
7
 * CommonUtils
8
 * 共通のUtility
9
 * @author Ryuichi Tanaka
10
 * @since 2015/12/26
11
 * @version 0.7
12
 */
13
trait CommonUtils
14
{
15
    /**
16
     * データのバイト長を返却する
17
     * @param string 文字列
0 ignored issues
show
Documentation Bug introduced by
The doc comment 文字列 at position 0 could not be parsed: Unknown type name '文字列' at position 0 in 文字列.
Loading history...
18
     * @return string バイト長
19
     */
20
    public function bytelen($data)
21
    {
22
        return strlen(bin2hex($data)) / 2;
23
    }
24
25
    /**
26
     * 要素が存在するかどうか
27
     * @param array 検索対象配列
0 ignored issues
show
Documentation Bug introduced by
The doc comment 検索対象配列 at position 0 could not be parsed: Unknown type name '検索対象配列' at position 0 in 検索対象配列.
Loading history...
28
     * @param mixed 検索値
29
     * @return bool 存在すればtrue
30
     */
31
    public function inArray($target, $list)
32
    {
33
        $type = gettype($target);
34
        switch ($type) {
35
            case "string":
36
            case "integer":
37
                return array_key_exists($target, array_flip($list));
38
            default:
39
                // それ以外の場合、in_arrayを使用する
40
                return in_array($target, $list, true);
41
        }
42
    }
43
44
    /**
45
     * ランダムな文字列を生成して返却する
46
     * @param int 生成する文字数(省略時は10文字)
0 ignored issues
show
Documentation Bug introduced by
The doc comment 生成する文字数(省略時は10文字) at position 0 could not be parsed: Unknown type name '生成する文字数' at position 0 in 生成する文字数(省略時は10文字).
Loading history...
47
     * @return string ランダム文字列
48
     */
49
    public function getRandomstring($length = 10)
50
    {
51
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
52
        mt_srand();
53
        $random_str = "";
54
        for ($i = 0; $i < $length; $i++) {
55
            $random_str .= $chars{mt_rand(0, strlen($chars) - 1)};
56
        }
57
58
        return $random_str;
59
    }
60
61
    /**
62
     * 一時ディレクトリパスを返却する
63
     * @return string 一時ディレクトリパス
64
     */
65
    public function getTemporaryDirectory()
66
    {
67
        return PHP_OS === "WIN32" || PHP_OS === "WINNT" ? "C:\\Windows\\Temp" : "/tmp";
68
    }
69
70
    /**
71
     * キャメルケース文字列をスネークケース文字列に置換する
72
     * @param string キャメルケース文字列
0 ignored issues
show
Documentation Bug introduced by
The doc comment キャメルケース文字列 at position 0 could not be parsed: Unknown type name 'キャメルケース文字列' at position 0 in キャメルケース文字列.
Loading history...
73
     * @return string スネークケース文字列
74
     */
75
    public function camel2snake($str)
76
    {
77
        $str = preg_replace_callback('/([A-Z])/', function ($matches) {
78
            return '_' . lcfirst($matches[1]);
79
        }, $str);
80
81
        return preg_replace('/^_/', '', $str);
82
    }
83
84
    /**
85
     * スネークケース文字列をアッパーキャメルケースに置換する
86
     * @param string スネークケース文字列
0 ignored issues
show
Documentation Bug introduced by
The doc comment スネークケース文字列 at position 0 could not be parsed: Unknown type name 'スネークケース文字列' at position 0 in スネークケース文字列.
Loading history...
87
     * @return string アッパーキャメルケース文字列
88
     */
89
    public function snake2ucamel($str)
90
    {
91
        $str = ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($matches) {
92
            return ucfirst($matches[1]);
93
        }, $str));
94
95
        return $str;
96
    }
97
98
    /**
99
     * スネークケース文字列をローワーキャメルケースに置換する
100
     * @param string スネークケース文字列
0 ignored issues
show
Documentation Bug introduced by
The doc comment スネークケース文字列 at position 0 could not be parsed: Unknown type name 'スネークケース文字列' at position 0 in スネークケース文字列.
Loading history...
101
     * @return string ローワーキャメルケース文字列
102
     */
103
    public function snake2lcamel($str)
104
    {
105
        return lcfirst($this->snake2ucamel($str));
106
    }
107
108
    /**
109
     * XMLオブジェクトを配列に変換する
110
     * @param object XMLオブジェクト
0 ignored issues
show
Documentation Bug introduced by
The doc comment XMLオブジェクト at position 0 could not be parsed: Unknown type name 'XMLオブジェクト' at position 0 in XMLオブジェクト.
Loading history...
111
     * @return Hash 配列/ハッシュデータ
112
     */
113
    public function xml2array($xml)
114
    {
115
        $result = array();
116
        if (is_object($xml)) {
117
            $list = get_object_vars($xml);
118
            while (list($k, $v) = each($list)) {
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

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

118
            while (list($k, $v) = /** @scrutinizer ignore-deprecated */ each($list)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
                $result[$k] = Utility::xml2array($v);
0 ignored issues
show
Bug introduced by
The type WebStream\Util\Utility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
            }
121
        } elseif (is_array($xml)) {
122
            while (list($k, $v) = each($xml)) {
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

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

122
            while (list($k, $v) = /** @scrutinizer ignore-deprecated */ each($xml)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
123
                $result[$k] = Utility::xml2array($v);
124
            }
125
        } else {
126
            $result = $xml;
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * ファイルからmimeタイプを返却する
134
     * @param string ファイルタイプ
0 ignored issues
show
Documentation Bug introduced by
The doc comment ファイルタイプ at position 0 could not be parsed: Unknown type name 'ファイルタイプ' at position 0 in ファイルタイプ.
Loading history...
135
     * @return string mimeタイプ
136
     */
137
    public function getMimeType($type)
138
    {
139
        switch ($type) {
140
            case "txt":
141
                return "text/plain";
142
            case "jpeg":
143
            case "jpg":
144
                return "image/jpeg";
145
            case "gif":
146
                return "image/gif";
147
            case "png":
148
                return "image/png";
149
            case "tiff":
150
                return "image/tiff";
151
            case "bmp":
152
                return "image/bmp";
153
            case "xml":
154
            case "rss":
155
            case "rdf":
156
            case "atom":
157
                return "application/xml";
158
            case "html":
159
            case "htm":
160
                return "text/html";
161
            case "css":
162
                return "text/css";
163
            case "js":
164
            case "jsonp":
165
                return "text/javascript";
166
            case "json":
167
                return "application/json";
168
            case "pdf":
169
                return "application/pdf";
170
            default:
171
                return "application/octet-stream";
172
        }
173
    }
174
}
175