1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Eccube\Util; |
25
|
|
|
|
26
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
27
|
|
|
|
28
|
|
|
class Str |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The MIT License (MIT) |
33
|
|
|
* |
34
|
|
|
* Copyright (c) <Taylor Otwell> |
35
|
|
|
* |
36
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
37
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
38
|
|
|
* in the Software without restriction, including without limitation the rights |
39
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
40
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
41
|
|
|
* furnished to do so, subject to the following conditions: |
42
|
|
|
* |
43
|
|
|
* The above copyright notice and this permission notice shall be included in |
44
|
|
|
* all copies or substantial portions of the Software. |
45
|
|
|
* |
46
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
47
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
48
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
49
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
50
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
51
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
52
|
|
|
* THE SOFTWARE |
53
|
|
|
* |
54
|
|
|
* Generate a more truly "random" alpha-numeric string. |
55
|
|
|
* |
56
|
|
|
* @param int $length |
57
|
|
|
* @return string |
58
|
|
|
* |
59
|
|
|
* @throws \RuntimeException |
60
|
|
|
*/ |
61
|
413 |
|
public static function random($length = 16) |
62
|
|
|
{ |
63
|
413 |
|
if (function_exists('openssl_random_pseudo_bytes')) { |
64
|
413 |
|
$bytes = openssl_random_pseudo_bytes($length * 2); |
65
|
|
|
|
66
|
413 |
|
if ($bytes === false) { |
67
|
1 |
|
throw new \RuntimeException('Unable to generate random string.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
412 |
|
return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return static::quickRandom($length); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The MIT License (MIT) |
78
|
|
|
* |
79
|
|
|
* Copyright (c) <Taylor Otwell> |
80
|
|
|
* |
81
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
82
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
83
|
|
|
* in the Software without restriction, including without limitation the rights |
84
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
85
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
86
|
|
|
* furnished to do so, subject to the following conditions: |
87
|
|
|
* |
88
|
|
|
* The above copyright notice and this permission notice shall be included in |
89
|
|
|
* all copies or substantial portions of the Software. |
90
|
|
|
* |
91
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
92
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
93
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
94
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
95
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
96
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
97
|
|
|
* THE SOFTWARE |
98
|
|
|
* |
99
|
|
|
* Generate a "random" alpha-numeric string. |
100
|
|
|
* |
101
|
|
|
* Should not be considered sufficient for cryptography, etc. |
102
|
|
|
* |
103
|
|
|
* @param int $length |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
2 |
|
public static function quickRandom($length = 16) |
107
|
|
|
{ |
108
|
2 |
|
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
109
|
|
|
|
110
|
2 |
|
return substr(str_shuffle(str_repeat($pool, $length)), 0, $length); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
|
|
|
|
115
|
|
|
* 改行コードの変換 |
116
|
|
|
* |
117
|
|
|
* @param $value |
|
|
|
|
118
|
|
|
* @param string $lf |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
15 |
|
public static function convertLineFeed($value, $lf = "\n") |
122
|
|
|
{ |
123
|
15 |
|
if (empty($value)) { |
124
|
3 |
|
return ''; |
125
|
|
|
} |
126
|
12 |
|
return strtr($value, array_fill_keys(array("\r\n", "\r", "\n"), $lf)); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
|
|
|
|
130
|
|
|
* 文字コードの判定 |
131
|
|
|
* |
132
|
|
|
* @param $value |
|
|
|
|
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
9 |
|
public static function characterEncoding($value, $encoding = array('UTF-8', 'SJIS', 'EUC-JP', 'ASCII', 'JIS', 'sjis-win')) |
136
|
|
|
{ |
137
|
9 |
|
foreach ($encoding as $encode) { |
138
|
9 |
|
if (mb_convert_encoding($value, $encode, $encode) == $value) { |
139
|
9 |
|
return $encode; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return null; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* 指定した文字列以上ある場合、「...」を付加する |
149
|
|
|
* lengthに7を指定すると、「1234567890」は「1234567...」と「...」を付与して出力される |
150
|
|
|
* |
151
|
|
|
* @param string $value |
152
|
|
|
* @param int $length |
|
|
|
|
153
|
|
|
* @param string $end |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
2 |
|
public static function ellipsis($value, $length = 100, $end = '...') |
157
|
|
|
{ |
158
|
2 |
|
if (mb_strlen($value) <= $length) { |
159
|
1 |
|
return $value; |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return rtrim(mb_substr($value, 0, $length, 'UTF-8')) . $end; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
|
|
|
|
167
|
|
|
* 現在からの経過時間を書式化する. |
168
|
|
|
* |
169
|
|
|
* @param $date |
|
|
|
|
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
1 |
|
public static function timeAgo($date) |
173
|
|
|
{ |
174
|
1 |
|
if (empty($date)) { |
175
|
1 |
|
return ''; |
176
|
|
|
} |
177
|
|
|
|
178
|
1 |
|
$now = new \DateTime(); |
179
|
1 |
|
if (!($date instanceof \DateTime)) { |
180
|
1 |
|
$date = new \DateTime($date); |
181
|
|
|
} |
182
|
1 |
|
$diff = $date->diff($now, true); |
183
|
1 |
|
if ($diff->y > 0) { |
184
|
|
|
// return $date->format("Y/m/d H:i"); |
|
|
|
|
185
|
1 |
|
return $date->format("Y/m/d"); |
186
|
|
|
} |
187
|
1 |
|
if ($diff->m == 1 || $diff->days > 0) { |
188
|
1 |
|
if ($diff->days <= 31) { |
189
|
1 |
|
return $diff->days . '日前'; |
|
|
|
|
190
|
|
|
} |
191
|
|
|
// return $date->format("Y/m/d H:i"); |
|
|
|
|
192
|
1 |
|
return $date->format("Y/m/d"); |
193
|
|
|
} |
194
|
1 |
|
if ($diff->h > 0) { |
195
|
1 |
|
return $diff->h . "時間前"; |
|
|
|
|
196
|
|
|
} |
197
|
1 |
|
if ($diff->i > 0) { |
198
|
1 |
|
return $diff->i . "分前"; |
|
|
|
|
199
|
|
|
} |
200
|
1 |
|
return $diff->s . "秒前"; |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* 変数が空白かどうかをチェックする. |
205
|
|
|
* |
206
|
|
|
* 引数 $value が空白かどうかをチェックする. 空白の場合は true. |
207
|
|
|
* 以下の文字は空白と判断する. |
208
|
|
|
* - ' ' (ASCII 32 (0x20)), 通常の空白 |
209
|
|
|
* - "\t" (ASCII 9 (0x09)), タブ |
210
|
|
|
* - "\n" (ASCII 10 (0x0A)), リターン |
211
|
|
|
* - "\r" (ASCII 13 (0x0D)), 改行 |
212
|
|
|
* - "\0" (ASCII 0 (0x00)), NULバイト |
213
|
|
|
* - "\x0B" (ASCII 11 (0x0B)), 垂直タブ |
214
|
|
|
* |
215
|
|
|
* 引数 $value がオブジェクト型、配列の場合は非推奨とし、 E_USER_DEPRECATED をスローする. |
216
|
|
|
* EC-CUBE2系からの互換性、ビギナー層を配慮し、以下のような実装とする. |
217
|
|
|
* 引数 $value が配列の場合は, 空の配列の場合 true を返す. |
218
|
|
|
* 引数 $value が ArrayCollection::isEmpty() == true の場合 true を返す. |
219
|
|
|
* 引数 $value が上記以外のオブジェクト型の場合は false を返す. |
220
|
|
|
* |
221
|
|
|
* 引数 $greedy が true の場合は, 全角スペース, ネストした空の配列も |
222
|
|
|
* 空白と判断する. |
223
|
|
|
* |
224
|
|
|
* @param string $value チェック対象の変数. 文字型以外も使用できるが、非推奨. |
|
|
|
|
225
|
|
|
* @param boolean $greedy '貧欲'にチェックを行う場合 true, デフォルト false |
226
|
|
|
* @return boolean $value が空白と判断された場合 true |
227
|
|
|
*/ |
228
|
75 |
|
public static function isBlank($value, $greedy = false) |
229
|
|
|
{ |
230
|
75 |
|
$deprecated = '\Eccube\Util\Str::isBlank() の第一引数は文字型、数値を使用してください'; |
231
|
|
|
// テストカバレッジを上げるために return の前で trigger_error をスローしている |
232
|
75 |
|
if (is_object($value)) { |
233
|
3 |
|
if ($value instanceof ArrayCollection) { |
234
|
2 |
|
if ($value->isEmpty()) { |
235
|
1 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
236
|
|
|
|
237
|
|
|
return true; |
238
|
|
|
} else { |
239
|
1 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
240
|
|
|
|
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
} |
244
|
1 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
245
|
|
|
return false; |
|
|
|
|
246
|
|
|
} |
247
|
72 |
|
if (is_array($value)) { |
248
|
5 |
|
if ($greedy) { |
249
|
3 |
|
if (empty($value)) { |
250
|
1 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
251
|
|
|
|
252
|
|
|
return true; |
253
|
|
|
} |
254
|
2 |
|
$array_result = true; |
255
|
2 |
|
foreach ($value as $in) { |
256
|
2 |
|
$array_result = self::isBlank($in, $greedy); |
257
|
2 |
|
if (!$array_result) { |
258
|
1 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
259
|
|
|
|
260
|
1 |
|
return false; |
261
|
|
|
} |
262
|
|
|
} |
263
|
1 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
264
|
|
|
|
265
|
|
|
return $array_result; |
266
|
|
|
} else { |
267
|
2 |
|
trigger_error($deprecated, E_USER_DEPRECATED); |
268
|
|
|
|
269
|
|
|
return empty($value); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
69 |
|
if ($greedy) { |
274
|
3 |
|
$value = preg_replace('/ /', '', $value); |
275
|
|
|
} |
276
|
|
|
|
277
|
69 |
|
$value = trim($value); |
278
|
69 |
|
if (strlen($value) > 0) { |
|
|
|
|
279
|
|
|
|
280
|
68 |
|
return false; |
281
|
|
|
} |
282
|
|
|
|
283
|
5 |
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
|
|
|
|
287
|
|
|
* @param $value |
|
|
|
|
288
|
|
|
* @return bool |
289
|
|
|
*/ |
290
|
64 |
|
public static function isNotBlank($value, $greedy = false) |
291
|
|
|
{ |
292
|
64 |
|
return !self::isBlank($value, $greedy); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
|
|
|
|
296
|
|
|
* 両端にある全角スペース、半角スペースを取り除く |
297
|
|
|
* |
298
|
|
|
* @param $value |
|
|
|
|
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
6 |
|
public static function trimAll($value) |
302
|
|
|
{ |
303
|
6 |
|
if ($value === '') { |
304
|
1 |
|
return ''; |
305
|
|
|
} |
306
|
6 |
|
if ($value === 0) { |
307
|
1 |
|
return 0; |
308
|
|
|
} |
309
|
6 |
|
if ($value == null) { |
310
|
1 |
|
return null; |
311
|
|
|
} |
312
|
6 |
|
return preg_replace('/(^\s+)|(\s+$)/u', '', $value); |
|
|
|
|
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|