Completed
Push — master ( 2aa627...ecf235 )
by
unknown
10s
created

ApollonValidation::alphaNumericJp()   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
     * alphaNumericJp
95
     * 半角英数チェック
96
     * CoreのalphaNumericは日本語を通過させてしまうため、上書き
97
     * @access public
98
     * @author ito
99
     * @param string $check
100
     * @return boolean
101
     */
102
    public static function alphaNumericJp($check)
103
    {
104
        $regex = '/^[a-zA-Z0-9]+$/u';
105
        return (bool) self::_check($check, $regex);
106
    }
107
108
    /**
109
     * alphaNumericSymbols
110
     * 半角英数記号チェック
111
     * 参考URL:http://defindit.com/ascii.html
112
     * @access public
113
     * @author ito
114
     * @param string $check
115
     * @return boolean
116
     */
117
    public static function alphaNumericSymbols($check)
118
    {
119
        // \x21-\x2f
120
        // ! " # $ % & ' ( ) * + , - . /
121
        // \x3a-\x40
122
        // : ; < = > ? @
123
        // \x5b-\x60
124
        // [ \ ] ^ _ `
125
        // \x7b-\x7e
126
        // { | } ~
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
        // 半角スペース、全角スペースは認めない
128
        $regex = '/^[a-zA-Z0-9\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]+$/u';
129
        return (bool) self::_check($check, $regex);
130
    }
131
132
    /**
133
     * naturalNumber
134
     * 数値チェック
135
     * integerなどの上限チェックを同時に行う
136
     *
137
     * @access public
138
     * @author hagiwara
139
     * @param string $check
140
     * @param boolean $allowZero
141
     * @param integer $limit
142
     * @return boolean
143
     */
144
    public static function naturalNumber($check, $allowZero = false, $limit = 2147483647)
145
    {
146
        //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす
147
        if (is_array($allowZero) && isset($allowZero['providers'])) {
148
            $allowZero = false;
149
        }
150
        if (is_array($limit) && isset($limit['providers'])) {
151
            $limit = 2147483647;
152
        }
153
154
        //coreのチェックを先に行う
155
        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...
156
            return false;
157
        }
158
        return abs($check) <= $limit;
159
    }
160
161
    /**
162
     * hiraganaOnly
163
     * 全角ひらがな以外が含まれていればエラーとするバリデーションチェック
164
     * 全角ダッシュ「ー」のみ必要と考えられるので追加
165
     * Japanese HIRAGANA Validation
166
     * @param string $check
167
     * @return boolean
168
     * https://github.com/ichikaway/cakeplus
169
     */
170
    public static function hiraganaOnly($check)
171
    {
172
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc))*$/';
173
        return self::_check($check, $regex);
174
    }
175
176
    /**
177
     * hiraganaSpaceOnly
178
     * 全角ひらがな以外に全角スペースもOKとするバリデーション
179
     *
180
     * @param string $check
181
     * @return boolean
182
     */
183
    public static function hiraganaSpaceOnly($check)
184
    {
185
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc)| )*$/';
186
        return self::_check($check, $regex);
187
    }
188
189
    /**
190
     * hiraganaAllSpaceOnly
191
     * 全角ひらがな以外に全半角スペースもOKとするバリデーション
192
     *
193
     * @param string $check
194
     * @return boolean
195
     */
196
    public static function hiraganaAllSpaceOnly($check)
197
    {
198
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc)|\x20| )*$/';
199
        return self::_check($check, $regex);
200
    }
201
202
    /**
203
     * katakanaOnly
204
     * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック
205
     * Japanese KATAKANA Validation
206
     *
207
     * @param string $check
208
     * @return boolean
209
     * https://github.com/ichikaway/cakeplus
210
     */
211
    public static function katakanaOnly($check)
212
    {
213
        //\xe3\x82\x9b 濁点゛
214
        //\xe3\x82\x9c 半濁点゜
215
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c))*$/';
216
        return self::_check($check, $regex);
217
    }
218
219
    /**
220
     * katakanaSpaceOnly
221
     * 全角カタナカ以外に全角スペースもOKとするバリデーション
222
     *
223
     * @param string $check
224
     * @return boolean
225
     */
226
    public static function katakanaSpaceOnly($check)
227
    {
228
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c)| )*$/';
229
        return self::_check($check, $regex);
230
    }
231
232
    /**
233
     * katakanaAllSpaceOnly
234
     * 全角カタナカ以外に全半角スペースもOKとするバリデーション
235
     *
236
     * @param string $check
237
     * @return boolean
238
     */
239
    public static function katakanaAllSpaceOnly($check)
240
    {
241
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c)|\x20| )*$/';
242
        return self::_check($check, $regex);
243
    }
244
245
    /**
246
     * zenkakuOnly
247
     * マルチバイト文字以外が含まれていればエラーとするバリデーションチェック
248
     * Japanese ZENKAKU Validation
249
     *
250
     * @param string $check
251
     * @return boolean
252
     * https://github.com/ichikaway/cakeplus
253
     */
254
    public static function zenkakuOnly($check)
255
    {
256
        $regex = '/(?:\xEF\xBD[\xA1-\xBF]|\xEF\xBE[\x80-\x9F])|[\x20-\x7E]/';
257
        return !self::_check($check, $regex);
258
    }
259
260
    /**
261
     * spaceOnly
262
     * 全角、半角スペースのみであればエラーとするバリデーションチェック
263
     * Japanese Space only validation
264
     *
265
     * @param string $check
266
     * @return boolean
267
     * https://github.com/ichikaway/cakeplus
268
     */
269
    public static function spaceOnly($check)
270
    {
271
        $regex = '/^(\s| )+$/';
272
        return !self::_check($check, $regex);
273
    }
274
275
    /**
276
     * hankakukatakanaOnly
277
     * 半角カタカナ以外が含まれていればエラーとするバリデーションチェック
278
     * Japanese HANKAKU KATAKANA Validation
279
     * http://ash.jp/code/unitbl1.htm
280
     * @param string $check
281
     * @return boolean
282
     */
283
    public static function hankakukatakanaOnly($check)
284
    {
285
        $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F])*$/';
286
        return self::_check($check, $regex);
287
    }
288
289
    /**
290
     * hankakukatakanaSpaceOnly
291
     * 半角カタカナ以外にも半角スペースもOKとするバリデーション
292
     * Japanese HANKAKU KATAKANA SPACE Validation
293
     * http://ash.jp/code/unitbl1.htm
294
     * @param string $check
295
     * @return boolean
296
     */
297
    public static function hankakukatakanaSpaceOnly($check)
298
    {
299
        $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F]|\x20)*$/';
300
        return self::_check($check, $regex);
301
    }
302
303
    /**
304
     * phone
305
     *
306
     * @access public
307
     * @author hayasaki
308
     * @param string $check
309
     * @return boolean
310
     */
311
    public static function phone($check)
312
    {
313
        $regex = '/^[0-9]{2,5}-?[0-9]{1,4}-?[0-9]{4}$/';
314
        return self::_check($check, $regex);
315
    }
316
317
    /**
318
     * phone1
319
     * 市外局番範囲は2~5桁
320
     *
321
     * @access public
322
     * @author hayasaki
323
     * @param string $check
324
     * @return boolean
325
     */
326
    public static function phone1($check)
327
    {
328
        $regex = '/^[0-9]{2,5}$/';
329
        return self::_check($check, $regex);
330
    }
331
332
    /**
333
     * phone2
334
     * 範囲は2~4桁
335
     *
336
     * @access public
337
     * @author hayasaki
338
     * @param string $check
339
     * @return boolean
340
     */
341
    public static function phone2($check)
342
    {
343
        $regex = '/^[0-9]{2,4}$/';
344
        return self::_check($check, $regex);
345
    }
346
347
    /**
348
     * phone3
349
     * 範囲は4桁固定
350
     *
351
     * @access public
352
     * @author hayasaki
353
     * @param string $check
354
     * @return boolean
355
     */
356
    public static function phone3($check)
357
    {
358
        $regex = '/^[0-9]{4}$/';
359
        return self::_check($check, $regex);
360
    }
361
362
    /**
363
     * emailNonRfc
364
     * メールアドレスチェック(RFC非準拠)
365
     *
366
     * @access public
367
     * @author fantasista21jp
368
     * @param string $check
369
     * @return boolean
370
     */
371
    public static function emailNonRfc($check)
372
    {
373
        $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@' . self::$_pattern['hostname'] . '$/ui';
374
        return self::_check($check, $regex);
375
    }
376
377
    /**
378
     * datetimeComparison
379
     * 日時比較チェック
380
     *
381
     * @access public
382
     * @author fantasista21jp
383
     * @param $check1
384
     * @param $operator
385
     * @param $check2
386
     * @param $context
387
     * @return boolean
388
     */
389
    public static function datetimeComparison($check1, $operator, $check2, $context)
390
    {
391
        $date1 = $check1;
392
        $date2 = $context['data'][$check2];
393
394
        if (empty($date1) || empty($date2)) {
395
            return true;
396
        }
397
398
        if (is_array($date1)) {
399
            $date1 = static::_getDateString($date1);
400
        }
401
        if (is_array($date2)) {
402
            $date2 = static::_getDateString($date2);
403
        }
404
405
        $parseDate1 = Chronos::parse($date1);
406
        $parseDate2 = Chronos::parse($date2);
407
408
        $operator = str_replace([' ', "\t", "\n", "\r", "\0", "\x0B"], '', strtolower($operator));
409
        switch ($operator) {
410
            case 'isgreater':
411
            case '>':
412
                return $parseDate1->gt($parseDate2);
413
414
            case 'isless':
415
            case '<':
416
                return $parseDate1->lt($parseDate2);
417
418
            case 'greaterorequal':
419
            case '>=':
420
                return $parseDate1->gte($parseDate2);
421
422
            case 'lessorequal':
423
            case '<=':
424
                return $parseDate1->lte($parseDate2);
425
426
            case 'equalto':
427
            case '==':
428
                return $parseDate1->eq($parseDate2);
429
430
            case 'notequal':
431
            case '!=':
432
                return $parseDate1->ne($parseDate2);
433
434
            default:
435
                static::$errors[] = 'You must define the $operator parameter for Validation::datetimeComparison()';
436
        }
437
438
        return false;
439
    }
440
}
441