CValidate   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 86.54%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
c 4
b 0
f 0
lcom 0
cbo 0
dl 0
loc 108
rs 10
ccs 45
cts 52
cp 0.8654

1 Method

Rating   Name   Duplication   Size   Complexity  
C check() 0 86 12
1
<?php
2
namespace Anax\Validate;
3
4
/**
5
 * A helper to validate variables.
6
 *
7
 */
8
class CValidate
9
{
10
11
    /**
12
     * Properties
13
     *
14
     */
15
    const REGEXP_EMAIL = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
16
17
18
19
    /**
20
     * Check if a value matches rules or throw exception.
21
     *
22
     * @param mixed   $value to check
23
     * @param string  $rules to apply when checking value
24
     * @param boolean $throws set to true to throw exception when check fails
25
     *
26
     * @return bool true or false
27
     * @throws \Exception
28
     */
29 16
    public function check($value, $rules, $throws = false)
30
    {
31
        $tests = [
32
            'fail' => [
33 16
                'message' => 'Will always fail.',
34
                'test' => function () {
35 2
                    return false;
36 16
                },
37 16
            ],
38
            'pass' => [
39 16
                'message' => 'Will always pass.',
40
                'test' => function () {
41 1
                    return true;
42 16
                },
43 16
            ],
44
            'not_empty' => [
45 16
                'message' => 'Can not be empty.',
46
                'test' => function ($value) {
47
                    return !empty($value);
48 16
                },
49 16
            ],
50
            'not_equal' => [
51 16
                'message' => 'Not equal.',
52
                'test' => function ($value, $arg) {
53
                    return $value != $arg;
54 16
                },
55 16
            ],
56
            'numeric' => [
57 16
                'message' => 'Must be numeric.',
58
                'test' => function ($value) {
59
                    return is_numeric($value);
60
                }
61 16
            ],
62
            'int' => [
63 16
                'message' => 'Must be an integer.',
64
                'test' => function ($value) {
65 3
                    $int = (int) $value;
66 3
                    return "$int" == "$value";
67
                }
68 16
            ],
69
            'range' => [
70 16
                'message' => 'Out of range.',
71
                'test' => function ($value, $min, $max) {
72 6
                    return $value >= $min && $value <= $max;
73
                }
74 16
            ],
75
            'email_adress' => [
76 16
                'message' => 'Must be an email adress.',
77
                'test' => function ($value) {
78
                    return preg_match(self::REGEXP_EMAIL, $value) === 1;
79
                }
80 16
            ],
81 16
        ];
82
83 16
        foreach ($rules as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $rules of type string is not traversable.
Loading history...
84 16
            $rule = is_int($key) ? $val : $key;
85
86 16
            if (!isset($tests[$rule])) {
87 4
                throw new \Exception("Validation rule does not exist.");
88
            }
89
    
90 12
            $param = is_int($key) ? null : $val;
91 12
            $test  =  $tests[$rule];
92
93 12
            if (is_callable($test['test'])) {
94
95 12
                if (isset($param) && is_array($param)) {
96 6
                    $param = array_merge([$value], $param);
97 12
                } elseif (isset($param)) {
98
                    $param = [$value, $param];
99
                } else {
100 6
                    $param = [$value];
101
                }
102
103 12
                if (!call_user_func_array($test['test'], $param)) {
104 8
                    if ($throws) {
105 4
                        throw new \Exception($test['message']);
106
                    } else {
107 4
                        return false;
108
                    }
109
                }
110 4
            }
111 4
        }
112
113 4
        return true;
114
    }
115
}
116