1 | <?php |
||
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) |
|
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") |
|
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')) |
|
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) |
|
294 | |||
295 | /** |
||
296 | * 両端にある全角スペース、半角スペースを取り除く |
||
297 | * |
||
298 | * @param $value |
||
299 | * @return string |
||
300 | */ |
||
301 | 6 | public static function trimAll($value) |
|
314 | } |
||
315 |