Completed
Push — master ( 0eeac7...3dc368 )
by satoru
10s
created

ApollonValidation::hiraganaAllSpaceOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Apollon\Validation;
3
4
use Cake\Validation\Validation;
5
use Cake\Chronos\Chronos;
6
7
class ApollonValidation extends Validation
8
{
9
    /**
10
     *  zip
11
     * 郵便番号チェック 1カラム
12
     *
13
     * @access public
14
     * @author hagiwara
15
     * @param string $check
16
     * @return boolean
17
     */
18
    public static function zip($check)
19
    {
20
        $regex = '/^[0-9]{3}-?[0-9]{4}$/';
21
        return self::_check($check, $regex);
22
    }
23
24
    /**
25
     * zip1
26
     * 郵便番号チェック 上3桁
27
     *
28
     * @access public
29
     * @author hagiwara
30
     * @param string $check
31
     * @return boolean
32
     */
33
    public static function zip1($check)
34
    {
35
        $regex = '/^[0-9]{3}$/';
36
        return self::_check($check, $regex);
37
    }
38
39
    /**
40
     * zip2
41
     * 郵便番号チェック 下4桁
42
     *
43
     * @access public
44
     * @author hagiwara
45
     * @param string $check
46
     * @return boolean
47
     */
48
    public static function zip2($check)
49
    {
50
        $regex = '/^[0-9]{4}$/';
51
        return self::_check($check, $regex);
52
    }
53
54
    /**
55
     * 半角英字チェック
56
     *
57
     * @access public
58
     * @author sakuragawa
59
     * @param string $check
60
     * @return boolean
61
     */
62
    public static function alpha($check)
63
    {
64
        $regex = '/^[a-zA-Z]+$/u';
65
        return self::_check($check, $regex);
66
    }
67
68
    /**
69
     * numeric
70
     * 数値チェック
71
     * integerなどの上限チェックを同時に行う
72
     *
73
     * @access public
74
     * @author hagiwara
75
     * @param string $check
76
     * @param integer $limit
77
     * @return boolean
78
     */
79
    public static function numeric($check, $limit = 2147483647)
80
    {
81
        //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす
82
        if (is_array($limit) && isset($limit['providers'])) {
83
            $limit = 2147483647;
84
        }
85
86
        //coreのチェックを先に行う
87
        if (!parent::numeric($check)) {
88
            return false;
89
        }
90
        return abs($check) <= $limit;
91
    }
92
93
    /**
94
     * naturalNumber
95
     * 数値チェック
96
     * integerなどの上限チェックを同時に行う
97
     *
98
     * @access public
99
     * @author hagiwara
100
     * @param string $check
101
     * @param boolean $allowZero
102
     * @param integer $limit
103
     * @return boolean
104
     */
105
    public static function naturalNumber($check, $allowZero = false, $limit = 2147483647)
106
    {
107
        //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす
108
        if (is_array($allowZero) && isset($allowZero['providers'])) {
109
            $allowZero = false;
110
        }
111
        if (is_array($limit) && isset($limit['providers'])) {
112
            $limit = 2147483647;
113
        }
114
115
        //coreのチェックを先に行う
116
        if (!parent::naturalNumber($check, $allowZero)) {
0 ignored issues
show
Bug introduced by
It seems like $allowZero can also be of type array<string,null,{"providers":"null"}>; however, Cake\Validation\Validation::naturalNumber() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
            return false;
118
        }
119
        return abs($check) <= $limit;
120
    }
121
122
    /**
123
     * hiraganaOnly
124
     * 全角ひらがな以外が含まれていればエラーとするバリデーションチェック
125
     * 全角ダッシュ「ー」のみ必要と考えられるので追加
126
     * Japanese HIRAGANA Validation
127
     * @param string $check
128
     * @return boolean
129
     * https://github.com/ichikaway/cakeplus
130
     */
131
    public static function hiraganaOnly($check)
132
    {
133
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc))*$/';
134
        return self::_check($check, $regex);
135
    }
136
137
    /**
138
     * hiraganaSpaceOnly
139
     * 全角ひらがな以外に全角スペースもOKとするバリデーション
140
     *
141
     * @param string $check
142
     * @return boolean
143
     */
144
    public static function hiraganaSpaceOnly($check)
145
    {
146
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc)| )*$/';
147
        return self::_check($check, $regex);
148
    }
149
150
    /**
151
     * hiraganaAllSpaceOnly
152
     * 全角ひらがな以外に全半角スペースもOKとするバリデーション
153
     *
154
     * @param string $check
155
     * @return boolean
156
     */
157
    public static function hiraganaAllSpaceOnly($check)
158
    {
159
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc)|\x20| )*$/';
160
        return self::_check($check, $regex);
161
    }
162
163
    /**
164
     * katakanaOnly
165
     * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック
166
     * Japanese KATAKANA Validation
167
     *
168
     * @param string $check
169
     * @return boolean
170
     * https://github.com/ichikaway/cakeplus
171
     */
172
    public static function katakanaOnly($check)
173
    {
174
        //\xe3\x82\x9b 濁点゛
175
        //\xe3\x82\x9c 半濁点゜
176
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c))*$/';
177
        return self::_check($check, $regex);
178
    }
179
180
    /**
181
     * katakanaSpaceOnly
182
     * 全角カタナカ以外に全角スペースもOKとするバリデーション
183
     *
184
     * @param string $check
185
     * @return boolean
186
     */
187
    public static function katakanaSpaceOnly($check)
188
    {
189
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c)| )*$/';
190
        return self::_check($check, $regex);
191
    }
192
193
    /**
194
     * katakanaAllSpaceOnly
195
     * 全角カタナカ以外に全半角スペースもOKとするバリデーション
196
     *
197
     * @param string $check
198
     * @return boolean
199
     */
200
    public static function katakanaAllSpaceOnly($check)
201
    {
202
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c)|\x20| )*$/';
203
        return self::_check($check, $regex);
204
    }
205
206
    /**
207
     * zenkakuOnly
208
     * マルチバイト文字以外が含まれていればエラーとするバリデーションチェック
209
     * Japanese ZENKAKU Validation
210
     *
211
     * @param string $check
212
     * @return boolean
213
     * https://github.com/ichikaway/cakeplus
214
     */
215
    public static function zenkakuOnly($check)
216
    {
217
        $regex = '/(?:\xEF\xBD[\xA1-\xBF]|\xEF\xBE[\x80-\x9F])|[\x20-\x7E]/';
218
        return !self::_check($check, $regex);
219
    }
220
221
    /**
222
     * spaceOnly
223
     * 全角、半角スペースのみであればエラーとするバリデーションチェック
224
     * Japanese Space only validation
225
     *
226
     * @param string $check
227
     * @return boolean
228
     * https://github.com/ichikaway/cakeplus
229
     */
230
    public static function spaceOnly($check)
231
    {
232
        $regex = '/^(\s| )+$/';
233
        return !self::_check($check, $regex);
234
    }
235
236
    /**
237
     * hankakukatakanaOnly
238
     * 半角カタカナ以外が含まれていればエラーとするバリデーションチェック
239
     * Japanese HANKAKU KATAKANA Validation
240
     * http://ash.jp/code/unitbl1.htm
241
     * @param string $check
242
     * @return boolean
243
     */
244
    public static function hankakukatakanaOnly($check)
245
    {
246
        $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F])*$/';
247
        return self::_check($check, $regex);
248
    }
249
250
    /**
251
     * hankakukatakanaSpaceOnly
252
     * 半角カタカナ以外にも半角スペースもOKとするバリデーション
253
     * Japanese HANKAKU KATAKANA SPACE Validation
254
     * http://ash.jp/code/unitbl1.htm
255
     * @param string $check
256
     * @return boolean
257
     */
258
    public static function hankakukatakanaSpaceOnly($check)
259
    {
260
        $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F]|\x20)*$/';
261
        return self::_check($check, $regex);
262
    }
263
264
    /**
265
     * phone
266
     *
267
     * @access public
268
     * @author hayasaki
269
     * @param string $check
270
     * @return boolean
271
     */
272
    public static function phone($check)
273
    {
274
        $regex = '/^[0-9]{2,5}-?[0-9]{1,4}-?[0-9]{4}$/';
275
        return self::_check($check, $regex);
276
    }
277
278
    /**
279
     * phone1
280
     * 市外局番範囲は2~5桁
281
     *
282
     * @access public
283
     * @author hayasaki
284
     * @param string $check
285
     * @return boolean
286
     */
287
    public static function phone1($check)
288
    {
289
        $regex = '/^[0-9]{2,5}$/';
290
        return self::_check($check, $regex);
291
    }
292
293
    /**
294
     * phone2
295
     * 範囲は2~4桁
296
     *
297
     * @access public
298
     * @author hayasaki
299
     * @param string $check
300
     * @return boolean
301
     */
302
    public static function phone2($check)
303
    {
304
        $regex = '/^[0-9]{2,4}$/';
305
        return self::_check($check, $regex);
306
    }
307
308
    /**
309
     * phone3
310
     * 範囲は4桁固定
311
     *
312
     * @access public
313
     * @author hayasaki
314
     * @param string $check
315
     * @return boolean
316
     */
317
    public static function phone3($check)
318
    {
319
        $regex = '/^[0-9]{4}$/';
320
        return self::_check($check, $regex);
321
    }
322
323
    /**
324
     * emailNonRfc
325
     * メールアドレスチェック(RFC非準拠)
326
     *
327
     * @access public
328
     * @author fantasista21jp
329
     * @param string $check
330
     * @return boolean
331
     */
332
    public static function emailNonRfc($check)
333
    {
334
        $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@' . self::$_pattern['hostname'] . '$/ui';
335
        return self::_check($check, $regex);
336
    }
337
338
    /**
339
     * datetimeComparison
340
     * 日時比較チェック
341
     *
342
     * @access public
343
     * @author fantasista21jp
344
     * @param $check1
345
     * @param $operator
346
     * @param $check2
347
     * @param $context
348
     * @return boolean
349
     */
350
    public static function datetimeComparison($check1, $operator, $check2, $context)
351
    {
352
        $date1 = $check1;
353
        $date2 = $context['data'][$check2];
354
355
        if (empty($date1) || empty($date2)) {
356
            return true;
357
        }
358
359
        if (is_array($date1)) {
360
            $date1 = static::_getDateString($date1);
361
        }
362
        if (is_array($date2)) {
363
            $date2 = static::_getDateString($date2);
364
        }
365
366
        $parseDate1 = Chronos::parse($date1);
367
        $parseDate2 = Chronos::parse($date2);
368
369
        $operator = str_replace([' ', "\t", "\n", "\r", "\0", "\x0B"], '', strtolower($operator));
370
        switch ($operator) {
371
            case 'isgreater':
372
            case '>':
373
                return $parseDate1->gt($parseDate2);
374
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
375
376
            case 'isless':
377
            case '<':
378
                return $parseDate1->lt($parseDate2);
379
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
380
381
            case 'greaterorequal':
382
            case '>=':
383
                return $parseDate1->gte($parseDate2);
384
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
385
386
            case 'lessorequal':
387
            case '<=':
388
                return $parseDate1->lte($parseDate2);
389
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
390
391
            case 'equalto':
392
            case '==':
393
                return $parseDate1->eq($parseDate2);
394
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
395
396
            case 'notequal':
397
            case '!=':
398
                return $parseDate1->ne($parseDate2);
399
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
400
401
            default:
402
                static::$errors[] = 'You must define the $operator parameter for Validation::datetimeComparison()';
403
        }
404
405
        return false;
406
    }
407
}
408