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 文字列 |
|
|
|
|
18
|
|
|
* @return string バイト長 |
19
|
|
|
*/ |
20
|
|
|
public function bytelen($data) |
21
|
|
|
{ |
22
|
|
|
return strlen(bin2hex($data)) / 2; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 要素が存在するかどうか |
27
|
|
|
* @param array 検索対象配列 |
|
|
|
|
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文字) |
|
|
|
|
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 キャメルケース文字列 |
|
|
|
|
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 スネークケース文字列 |
|
|
|
|
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 スネークケース文字列 |
|
|
|
|
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オブジェクト |
|
|
|
|
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)) { |
|
|
|
|
119
|
|
|
$result[$k] = Utility::xml2array($v); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} elseif (is_array($xml)) { |
122
|
|
|
while (list($k, $v) = each($xml)) { |
|
|
|
|
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 ファイルタイプ |
|
|
|
|
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
|
|
|
|