Number::className()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author AIZAWA Hina <[email protected]>
4
 * @copyright 2015 by AIZAWA Hina <[email protected]>
5
 * @license https://github.com/fetus-hina/docomo-dialogue/blob/master/LICENSE MIT
6
 * @since 1.0.1
7
 */
8
9
namespace jp3cki\docomoDialogue\validators;
10
11
use jp3cki\docomoDialogue\DomainError;
12
13
/**
14
 * 数値のバリデータ
15
 */
16
class Number
17
{
18
    /**
19
     * 数値型を検査する
20
     *
21
     * @param   int     $value              対象にする数値
22
     * @param   int     $min                許容する最小値
23
     * @param   int     $max                許容する最大値
24
     * @param   string  $errorMessage       異常時に発生する例外のメッセージ
25
     * @return  bool
26
     *
27
     * @throws  \jp3cki\docomoDialogue\DomainError
28
     */
29
    public static function validate($value, $min, $max, $errorMessage)
30
    {
31
        if (is_string($value) && !preg_match('/^\d+$/', $value)) {
32
            throw new DomainError($errorMessage);
33
        }
34
        $value = (int)$value;
35
        if ($min !== null && $value < $min) {
36
            throw new DomainError($errorMessage);
37
        }
38
        if ($max !== null && $value > $max) {
39
            throw new DomainError($errorMessage);
40
        }
41
        return true;
42
    }
43
44
    /**
45
     * クラス名(FQCN)を取得
46
     *
47
     * return string
48
     */
49
    public static function className()
50
    {
51
        return get_called_class();
52
    }
53
}
54