ValidationRules::passes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Cube\SilverStripe\Validation;
4
5
use Exception;
6
use SilverStripe\Core\ClassInfo;
7
use Cube\SilverStripe\Validation\Interfaces\Rule;
8
9
/**
10
 * Class ValidationRules
11
 * @package Cube\SilverStripe\Validation
12
 */
13
class ValidationRules
14
{
15
    /**
16
     * @var
17
     */
18
    private $validationMethod;
19
20
    /**
21
     * @param string $rule
22
     * @return static
23
     * @throws Exception
24
     */
25
    public static function get(string $rule)
26
    {
27
        $rules = new static();
28
29
        $rules->setValidationMethod($rule);
30
31
        return $rules;
32
    }
33
34
    /**
35
     * @param string $rule
36
     * @throws Exception
37
     */
38
    private function setValidationMethod(string $rule)
39
    {
40
        $method = 'validate' . ucfirst($rule);
41
        $break = strpos($rule, '_');
42
43
        if ($break) {
44
            $method = 'validate'.ucfirst(substr($rule, 0, $break)) . ucfirst(substr($rule, $break + 1));
45
        }
46
47
        if (method_exists($this, $method)) {
48
            $this->validationMethod = $method;
49
        } elseif ($customRule = $this->getCustomValidationRule($rule)) {
50
            $this->validationMethod = $customRule;
51
        } else {
52
            // This rule does not exist
53
            throw new Exception("The validation rule '{$rule}' does not exist.");
54
        }
55
    }
56
57
    /**
58
     * @param string $rule
59
     * @return bool|mixed
60
     * @throws Exception
61
     */
62
    private function getCustomValidationRule(string $rule)
63
    {
64
        foreach (ClassInfo::implementorsOf(Rule::class) as $class) {
65
            $properties = get_class_vars($class);
66
67
            if (isset($properties['name']) && $rule === $properties['name']) {
68
                return new $class();
69
            }
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * @param $value
77
     * @param array $arguments
78
     * @return false
79
     */
80
    public function passes($value, array $arguments = [])
81
    {
82
        if (is_object($this->validationMethod)) {
83
            return $this->validationMethod->passes($value);
84
        }
85
86
        if (count($arguments) > 0) {
87
            return $this->{$this->validationMethod}($value, $arguments);
88
        }
89
        return $this->{$this->validationMethod}($value);
90
    }
91
92
    /**
93
     * @param $str
94
     * @return false|int
95
     */
96
    private function strLength($str = '')
97
    {
98
        if (!is_string($str)) {
99
            return false;
100
        }
101
102
        return strlen($str);
103
    }
104
105
    /**
106
     * @param $value
107
     * @return bool
108
     */
109
    protected function validateRequired($value)
110
    {
111
        return $value !== null
112
            && !empty($value)
113
            || (is_array($value) && count($value) > 0);
114
    }
115
116
    /**
117
     * @param $value
118
     * @param $arguments
119
     * @return bool
120
     */
121
    protected function validateMin($value, $arguments)
122
    {
123
        if (!is_numeric($value) || is_string($value)) {
124
            return false;
125
        } else {
126
            return $arguments[0] <= $value;
127
        }
128
    }
129
130
    /**
131
     * @param $value
132
     * @param $arguments
133
     * @return bool
134
     */
135
    protected function validateMinLength($value, $arguments)
136
    {
137
        $length = $this->strLength($value);
138
139
        return ($length !== false) && $length >= (int) $arguments[0];
140
    }
141
142
    /**
143
     * @param $value
144
     * @param $arguments
145
     * @return bool
146
     */
147
    protected function validateMax($value, $arguments)
148
    {
149
        if (!is_numeric($value) || is_string($value)) {
150
            return false;
151
        } else {
152
            return $arguments[0] >= $value;
153
        }
154
    }
155
156
    /**
157
     * @param $value
158
     * @param $arguments
159
     * @return bool
160
     */
161
    protected function validateMaxLength($value, $arguments)
162
    {
163
        $length = $this->strLength($value);
164
165
        return ($length !== false) && $length <= (int) $arguments[0];
166
    }
167
168
    /**
169
     * @param $value
170
     * @param $arguments
171
     * @return false|int
172
     */
173
    protected function validateRegex($value, $arguments)
174
    {
175
        return preg_match($arguments[0], $value);
176
    }
177
178
    /**
179
     * @param $value
180
     * @return bool
181
     */
182
    protected function validateEmail($value)
183
    {
184
        return filter_var($value, FILTER_VALIDATE_EMAIL);
185
    }
186
}
187