Failed Conditions
Push — experimental/3.1 ( ea436b...26b8cd )
by chihiro
244:59 queued 239:01
created

StringUtil::timeAgo()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 13
nop 1
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
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 StringUtil
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
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
    public static function random($length = 16)
62
    {
63
        if (function_exists('openssl_random_pseudo_bytes')) {
64
            $bytes = openssl_random_pseudo_bytes($length * 2);
65
66
            if ($bytes === false) {
67
                throw new \RuntimeException('Unable to generate random string.');
68
            }
69
70
            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
    public static function quickRandom($length = 16)
107
    {
108
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
109
110
        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
111
    }
112
113
114
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$value" missing
Loading history...
115
     * 改行コードの変換
116
     *
117
     * @param $value
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
118
     * @param string $lf
119
     * @return string
120
     */
121
    public static function convertLineFeed($value, $lf = "\n")
122
    {
123
        if (empty($value)) {
124
            return '';
125
        }
126
        return strtr($value, array_fill_keys(array("\r\n", "\r", "\n"), $lf));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
127
    }
128
129
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$value" missing
Loading history...
introduced by
Doc comment for parameter "$encoding" missing
Loading history...
130
     * 文字コードの判定
131
     *
132
     * @param $value
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
133
     * @return string
134
     */
135
    public static function characterEncoding($value, $encoding = array('UTF-8', 'SJIS', 'EUC-JP', 'ASCII', 'JIS', 'sjis-win'))
136
    {
137
        foreach ($encoding as $encode) {
138
            if (mb_convert_encoding($value, $encode, $encode) == $value) {
139
                return $encode;
140
            }
141
        }
142
143
        return null;
144
145
    }
146
147
    /**
148
     * 指定した文字列以上ある場合、「...」を付加する
149
     * lengthに7を指定すると、「1234567890」は「1234567...」と「...」を付与して出力される
150
     *
151
     * @param string $value
152
     * @param int $length
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
153
     * @param string $end
154
     * @return string
155
     */
156
    public static function ellipsis($value, $length = 100, $end = '...')
157
    {
158
        if (mb_strlen($value) <= $length) {
159
            return $value;
160
        }
161
162
        return rtrim(mb_substr($value, 0, $length, 'UTF-8')) . $end;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
163
    }
164
165
166
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
167
     * 現在からの経過時間を書式化する.
168
     *
169
     * @param $date
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
170
     * @return string
171
     */
172
    public static function timeAgo($date)
173
    {
174
        if (empty($date)) {
175
            return '';
176
        }
177
178
        $now = new \DateTime();
179
        if (!($date instanceof \DateTime)) {
180
            $date = new \DateTime($date);
181
        }
182
        $diff = $date->diff($now, true);
183
        if ($diff->y > 0) {
184
            // return $date->format("Y/m/d H:i");
185
            return $date->format("Y/m/d");
186
        }
187
        if ($diff->m == 1 || $diff->days > 0) {
188
            if ($diff->days <= 31) {
189
                return $diff->days . '日前';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
190
            }
191
            // return $date->format("Y/m/d H:i");
192
            return $date->format("Y/m/d");
193
        }
194
        if ($diff->h > 0) {
195
            return $diff->h . "時間前";
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
196
        }
197
        if ($diff->i > 0) {
198
            return $diff->i . "分前";
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
199
        }
200
        return $diff->s . "秒前";
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
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 チェック対象の変数. 文字型以外も使用できるが、非推奨.
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
225
     * @param boolean $greedy '貧欲'にチェックを行う場合 true, デフォルト false
226
     * @return boolean $value が空白と判断された場合 true
227
     */
228
    public static function isBlank($value, $greedy = false)
229
    {
230
        $deprecated = '\Eccube\Util\StringUtil::isBlank() の第一引数は文字型、数値を使用してください';
231
        // テストカバレッジを上げるために return の前で trigger_error をスローしている
232
        if (is_object($value)) {
233
            if ($value instanceof ArrayCollection) {
234
                if ($value->isEmpty()) {
235
                    trigger_error($deprecated, E_USER_DEPRECATED);
236
237
                    return true;
238
                } else {
239
                    trigger_error($deprecated, E_USER_DEPRECATED);
240
241
                    return false;
242
                }
243
            }
244
            trigger_error($deprecated, E_USER_DEPRECATED);
245
            return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
246
        }
247
        if (is_array($value)) {
248
            if ($greedy) {
249
                if (empty($value)) {
250
                    trigger_error($deprecated, E_USER_DEPRECATED);
251
252
                    return true;
253
                }
254
                $array_result = true;
255
                foreach ($value as $in) {
256
                    $array_result = self::isBlank($in, $greedy);
257
                    if (!$array_result) {
258
                        trigger_error($deprecated, E_USER_DEPRECATED);
259
260
                        return false;
261
                    }
262
                }
263
                trigger_error($deprecated, E_USER_DEPRECATED);
264
265
                return $array_result;
266
            } else {
267
                trigger_error($deprecated, E_USER_DEPRECATED);
268
269
                return empty($value);
270
            }
271
        }
272
273
        if ($greedy) {
274
            $value = preg_replace('/ /', '', $value);
275
        }
276
277
        $value = trim($value);
278
        if (strlen($value) > 0) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
279
280
            return false;
281
        }
282
283
        return true;
284
    }
285
286
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$value" missing
Loading history...
introduced by
Doc comment for parameter "$greedy" missing
Loading history...
287
     * @param $value
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
288
     * @return bool
289
     */
290
    public static function isNotBlank($value, $greedy = false)
291
    {
292
        return !self::isBlank($value, $greedy);
293
    }
294
295
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$value" missing
Loading history...
296
     * 両端にある全角スペース、半角スペースを取り除く
297
     *
298
     * @param $value
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
299
     * @return string
300
     */
301
    public static function trimAll($value)
302
    {
303
        if ($value === '') {
304
            return '';
305
        }
306
        if ($value === 0) {
307
            return 0;
308
        }
309
        if ($value == null) {
310
            return null;
311
        }
312
        return preg_replace('/(^\s+)|(\s+$)/u', '', $value);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
313
    }
314
}
315