Completed
Pull Request — master (#21)
by Tom
02:14
created

ApollonValidation::alpha()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
6
class ApollonValidation extends Validation
7
{
8
    /**
9
     *  zip
10
     * 郵便番号チェック 1カラム
11
     *
12
     * @access public
13
     * @author hagiwara
14
     * @param string $check
15
     * @return boolean
16
     */
17
    public static function zip($check)
18
    {
19
        $regex = '/^[0-9]{3}-?[0-9]{4}$/';
20
        return self::_check($check, $regex);
21
    }
22
23
    /**
24
     * zip1
25
     * 郵便番号チェック 上3桁
26
     *
27
     * @access public
28
     * @author hagiwara
29
     * @param string $check
30
     * @return boolean
31
     */
32
    public static function zip1($check)
33
    {
34
        $regex = '/^[0-9]{3}$/';
35
        return self::_check($check, $regex);
36
    }
37
38
    /**
39
     * zip2
40
     * 郵便番号チェック 下4桁
41
     *
42
     * @access public
43
     * @author hagiwara
44
     * @param string $check
45
     * @return boolean
46
     */
47
    public static function zip2($check)
48
    {
49
        $regex = '/^[0-9]{4}$/';
50
        return self::_check($check, $regex);
51
    }
52
53
    /**
54
     * 半角英字チェック
55
     *
56
     * @access public
57
     * @author sakuragawa
58
     * @param string $check
59
     * @return boolean
60
     */
61
    public static function alpha($check)
62
    {
63
        $regex = '/^[a-zA-Z]+$/u';
64
        return self::_check($check, $regex);
65
    }
66
67
    /**
68
     * numeric
69
     * 数値チェック
70
     * integerなどの上限チェックを同時に行う
71
     *
72
     * @access public
73
     * @author hagiwara
74
     * @param string $check
75
     * @param integer $limit
76
     * @return boolean
77
     */
78
    public static function numeric($check, $limit = 2147483647)
79
    {
80
        //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす
81
        if (is_array($limit) && isset($limit['providers'])) {
82
            $limit = 2147483647;
83
        }
84
85
        //coreのチェックを先に行う
86
        if (!parent::numeric($check)) {
87
            return false;
88
        }
89
        return abs($check) <= $limit;
90
    }
91
92
    /**
93
     * naturalNumber
94
     * 数値チェック
95
     * integerなどの上限チェックを同時に行う
96
     *
97
     * @access public
98
     * @author hagiwara
99
     * @param string $check
100
     * @param boolean $allowZero
101
     * @param integer $limit
102
     * @return boolean
103
     */
104
    public static function naturalNumber($check, $allowZero = false, $limit = 2147483647)
105
    {
106
        //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす
107
        if (is_array($allowZero) && isset($allowZero['providers'])) {
108
            $allowZero = false;
109
        }
110
        if (is_array($limit) && isset($limit['providers'])) {
111
            $limit = 2147483647;
112
        }
113
114
        //coreのチェックを先に行う
115
        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...
116
            return false;
117
        }
118
        return abs($check) <= $limit;
119
    }
120
121
    /**
122
     * hiraganaOnly
123
     * 全角ひらがな以外が含まれていればエラーとするバリデーションチェック
124
     * 全角ダッシュ「ー」のみ必要と考えられるので追加
125
     * Japanese HIRAGANA Validation
126
     * @param string $check
127
     * @return boolean
128
     * https://github.com/ichikaway/cakeplus
129
     */
130
    public static function hiraganaOnly($check)
131
    {
132
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc))*$/';
133
        return self::_check($check, $regex);
134
    }
135
136
    /**
137
     * hiraganaSpaceOnly
138
     * 全角ひらがな以外にスペースもOKとするバリデーション
139
     *
140
     * @param string $check
141
     * @return boolean
142
     */
143
    public static function hiraganaSpaceOnly($check)
144
    {
145
        $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc)| )*$/';
146
        return self::_check($check, $regex);
147
    }
148
149
    /**
150
     * katakanaOnly
151
     * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック
152
     * Japanese KATAKANA Validation
153
     *
154
     * @param string $check
155
     * @return boolean
156
     * https://github.com/ichikaway/cakeplus
157
     */
158
    public static function katakanaOnly($check)
159
    {
160
        //\xe3\x82\x9b 濁点゛
161
        //\xe3\x82\x9c 半濁点゜
162
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c))*$/';
163
        return self::_check($check, $regex);
164
    }
165
166
    /**
167
     * katakanaSpaceOnly
168
     * 全角カタナカ以外にスペースもOKとするバリデーション
169
     *
170
     * @param string $check
171
     * @return boolean
172
     */
173
    public static function katakanaSpaceOnly($check)
174
    {
175
        $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c)| )*$/';
176
        return self::_check($check, $regex);
177
    }
178
179
    /**
180
     * zenkakuOnly
181
     * マルチバイト文字以外が含まれていればエラーとするバリデーションチェック
182
     * Japanese ZENKAKU Validation
183
     *
184
     * @param string $check
185
     * @return boolean
186
     * https://github.com/ichikaway/cakeplus
187
     */
188
    public static function zenkakuOnly($check)
189
    {
190
        $regex = '/(?:\xEF\xBD[\xA1-\xBF]|\xEF\xBE[\x80-\x9F])|[\x20-\x7E]/';
191
        return !self::_check($check, $regex);
192
    }
193
194
    /**
195
     * spaceOnly
196
     * 全角、半角スペースのみであればエラーとするバリデーションチェック
197
     * Japanese Space only validation
198
     *
199
     * @param string $check
200
     * @return boolean
201
     * https://github.com/ichikaway/cakeplus
202
     */
203
    public static function spaceOnly($check)
204
    {
205
        $regex = '/^(\s| )+$/';
206
        return !self::_check($check, $regex);
207
    }
208
209
    /**
210
     * hankakukatakanaOnly
211
     * 半角カタカナ以外が含まれていればエラーとするバリデーションチェック
212
     * Japanese HANKAKU KATAKANA Validation
213
     * http://ash.jp/code/unitbl1.htm
214
     * @param string $check
215
     * @return boolean
216
     */
217
    public static function hankakukatakanaOnly($check)
218
    {
219
        $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F])*$/';
220
        return self::_check($check, $regex);
221
    }
222
223
    /**
224
     * hankakukatakanaSpaceOnly
225
     * 半角カタカナ以外にも半角スペースもOKとするバリデーション
226
     * Japanese HANKAKU KATAKANA SPACE Validation
227
     * http://ash.jp/code/unitbl1.htm
228
     * @param string $check
229
     * @return boolean
230
     */
231
    public static function hankakukatakanaSpaceOnly($check)
232
    {
233
        $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F]|\x20)*$/';
234
        return self::_check($check, $regex);
235
    }
236
237
    /**
238
     * phone
239
     *
240
     * @access public
241
     * @author hayasaki
242
     * @param string $check
243
     * @return boolean
244
     */
245
    public static function phone($check)
246
    {
247
        $regex = '/^[0-9]{2,5}-?[0-9]{1,4}-?[0-9]{4}$/';
248
        return self::_check($check, $regex);
249
    }
250
251
    /**
252
     * phone1
253
     * 市外局番範囲は2~5桁
254
     *
255
     * @access public
256
     * @author hayasaki
257
     * @param string $check
258
     * @return boolean
259
     */
260
    public static function phone1($check)
261
    {
262
        $regex = '/^[0-9]{2,5}$/';
263
        return self::_check($check, $regex);
264
    }
265
266
    /**
267
     * phone2
268
     * 範囲は2~4桁
269
     *
270
     * @access public
271
     * @author hayasaki
272
     * @param string $check
273
     * @return boolean
274
     */
275
    public static function phone2($check)
276
    {
277
        $regex = '/^[0-9]{2,4}$/';
278
        return self::_check($check, $regex);
279
    }
280
281
    /**
282
     * phone3
283
     * 範囲は4桁固定
284
     *
285
     * @access public
286
     * @author hayasaki
287
     * @param string $check
288
     * @return boolean
289
     */
290
    public static function phone3($check)
291
    {
292
        $regex = '/^[0-9]{4}$/';
293
        return self::_check($check, $regex);
294
    }
295
296
    /**
297
     * emailNonRfc
298
     * メールアドレスチェック(RFC非準拠)
299
     *
300
     * @access public
301
     * @author fantasista21jp
302
     * @param string $check
303
     * @return boolean
304
     */
305
    public static function emailNonRfc($check)
306
    {
307
        $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@' . self::$_pattern['hostname'] . '$/ui';
308
        return self::_check($check, $regex);
309
    }
310
311
    /**
312
     * datetimeComparison
313
     * 日時比較チェック
314
     *
315
     * @access public
316
     * @author fantasista21jp
317
     * @param $check1
318
     * @param $operator
319
     * @param $check2
320
     * @param $context
321
     * @return boolean
322
     */
323
    public static function datetimeComparison($check1, $operator, $check2, $context)
324
    {
325
        $date1 = $check1;
326
        $date2 = $context['data'][$check2];
327
328
        if (empty($date1) || empty($date2)) {
329
            return true;
330
        }
331
332
        if (is_array($date1)) {
333
            $date1 = static::_getDateString($date1);
334
        }
335
        if (is_array($date2)) {
336
            $date2 = static::_getDateString($date2);
337
        }
338
339
        $timestamp1 = \Cake\Chronos\Chronos::parse($date1)->timestamp;
340
        $timestamp2 = \Cake\Chronos\Chronos::parse($date2)->timestamp;
341
342
        $operator = str_replace([' ', "\t", "\n", "\r", "\0", "\x0B"], '', strtolower($operator));
343
        switch ($operator) {
344
            case 'isgreater':
345
            case '>':
346
                if ($timestamp1 > $timestamp2) {
347
                    return true;
348
                }
349
                break;
350
            case 'isless':
351
            case '<':
352
                if ($timestamp1 < $timestamp2) {
353
                    return true;
354
                }
355
                break;
356
            case 'greaterorequal':
357
            case '>=':
358
                if ($timestamp1 >= $timestamp2) {
359
                    return true;
360
                }
361
                break;
362
            case 'lessorequal':
363
            case '<=':
364
                if ($timestamp1 <= $timestamp2) {
365
                    return true;
366
                }
367
                break;
368
            case 'equalto':
369
            case '==':
370
                if ($timestamp1 == $timestamp2) {
371
                    return true;
372
                }
373
                break;
374
            case 'notequal':
375
            case '!=':
376
                if ($timestamp1 != $timestamp2) {
377
                    return true;
378
                }
379
                break;
380
            default:
381
                static::$errors[] = 'You must define the $operator parameter for Validation::datetimeComparison()';
382
        }
383
384
        return false;
385
    }
386
}
387