@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | use Module; |
| 15 | 15 | |
| 16 | 16 | protected static $methods = [], |
| 17 | - $errors = []; |
|
| 17 | + $errors = []; |
|
| 18 | 18 | public static $data = []; |
| 19 | 19 | |
| 20 | 20 | public static function valid($rules, $data){ |
@@ -103,112 +103,112 @@ discard block |
||
| 103 | 103 | 'required' => [ |
| 104 | 104 | 'validate' => function($value) { |
| 105 | 105 | return (is_numeric($value) && $value==0) || !empty($value); |
| 106 | - }, |
|
| 107 | - 'message' => "This value cannot be empty.", |
|
| 106 | + }, |
|
| 107 | + 'message' => "This value cannot be empty.", |
|
| 108 | 108 | ], |
| 109 | 109 | |
| 110 | 110 | 'alphanumeric' => [ |
| 111 | 111 | 'validate' => function($value) { |
| 112 | - return (bool)preg_match('/^[0-9a-zA-Z]+$/',$value); |
|
| 112 | + return (bool)preg_match('/^[0-9a-zA-Z]+$/',$value); |
|
| 113 | 113 | }, |
| 114 | 114 | 'message' => "Value must be alphanumeric.", |
| 115 | 115 | ], |
| 116 | 116 | |
| 117 | 117 | 'numeric' => [ |
| 118 | 118 | 'validate' => function($value) { |
| 119 | - return (bool)preg_match('/^\d+$/',$value); |
|
| 119 | + return (bool)preg_match('/^\d+$/',$value); |
|
| 120 | 120 | }, |
| 121 | 121 | 'message' => "Value must be numeric.", |
| 122 | 122 | ], |
| 123 | 123 | |
| 124 | 124 | 'email' => [ |
| 125 | 125 | 'validate' => function($value) { |
| 126 | - return (bool)filter_var($value, FILTER_VALIDATE_EMAIL); |
|
| 126 | + return (bool)filter_var($value, FILTER_VALIDATE_EMAIL); |
|
| 127 | 127 | }, |
| 128 | 128 | 'message' => "This is not a valid email.", |
| 129 | 129 | ], |
| 130 | 130 | |
| 131 | 131 | 'url' => [ |
| 132 | 132 | 'validate' => function($value) { |
| 133 | - return (bool)filter_var($value, FILTER_VALIDATE_URL); |
|
| 133 | + return (bool)filter_var($value, FILTER_VALIDATE_URL); |
|
| 134 | 134 | }, |
| 135 | 135 | 'message' => "This is not a valid URL.", |
| 136 | 136 | ], |
| 137 | 137 | |
| 138 | 138 | 'max' => [ |
| 139 | 139 | 'validate' => function($value,$max) { |
| 140 | - return $value<=$max ? true : false; |
|
| 140 | + return $value<=$max ? true : false; |
|
| 141 | 141 | }, |
| 142 | 142 | 'message' => "Value must be less than {{arg_1}}.", |
| 143 | 143 | ], |
| 144 | 144 | |
| 145 | 145 | 'min' => [ |
| 146 | 146 | 'validate' => function($value,$min) { |
| 147 | - return $value >= $min; |
|
| 147 | + return $value >= $min; |
|
| 148 | 148 | }, |
| 149 | 149 | 'message' => "Value must be greater than {{arg_1}}.", |
| 150 | 150 | ], |
| 151 | 151 | |
| 152 | 152 | 'range' => [ |
| 153 | 153 | 'validate' => function($value,$min,$max) { |
| 154 | - return ( $value >= $min ) && ( $value <= $max ); |
|
| 154 | + return ( $value >= $min ) && ( $value <= $max ); |
|
| 155 | 155 | }, |
| 156 | 156 | 'message' => "This value must be in [{{arg_1}},{{arg_2}}] range.", |
| 157 | 157 | ], |
| 158 | 158 | |
| 159 | 159 | 'words' => [ |
| 160 | 160 | 'validate' => function($value,$max) { |
| 161 | - return str_word_count($value) <= $max; |
|
| 161 | + return str_word_count($value) <= $max; |
|
| 162 | 162 | }, |
| 163 | 163 | 'message' => "Too many words, max count is {{arg_1}}.", |
| 164 | 164 | ], |
| 165 | 165 | |
| 166 | 166 | 'length' => [ |
| 167 | 167 | 'validate' => function($value,$length) { |
| 168 | - return strlen($value) == $length; |
|
| 168 | + return strlen($value) == $length; |
|
| 169 | 169 | }, |
| 170 | 170 | 'message' => "This value must be {{arg_1}} characters.", |
| 171 | 171 | ], |
| 172 | 172 | |
| 173 | 173 | 'min_length' => [ |
| 174 | 174 | 'validate' => function($value,$min) { |
| 175 | - return strlen($value) >= $min; |
|
| 175 | + return strlen($value) >= $min; |
|
| 176 | 176 | }, |
| 177 | 177 | 'message' => "Too few characters, min count is {{arg_1}}.", |
| 178 | 178 | ], |
| 179 | 179 | |
| 180 | 180 | 'max_length' => [ |
| 181 | 181 | 'validate' => function($value,$max) { |
| 182 | - return strlen($value) <= $max; |
|
| 182 | + return strlen($value) <= $max; |
|
| 183 | 183 | }, |
| 184 | 184 | 'message' => "Too many characters, max count is {{arg_1}}.", |
| 185 | 185 | ], |
| 186 | 186 | |
| 187 | 187 | 'true' => [ |
| 188 | 188 | 'validate' => function($value) { |
| 189 | - return (bool)$value; |
|
| 189 | + return (bool)$value; |
|
| 190 | 190 | }, |
| 191 | 191 | 'message' => "This value must be true.", |
| 192 | 192 | ], |
| 193 | 193 | |
| 194 | 194 | 'false' => [ |
| 195 | 195 | 'validate' => function($value) { |
| 196 | - return !$value; |
|
| 196 | + return !$value; |
|
| 197 | 197 | }, |
| 198 | 198 | 'message' => "This value must be false.", |
| 199 | 199 | ], |
| 200 | 200 | |
| 201 | 201 | 'same_as' => [ |
| 202 | 202 | 'validate' => function($value,$fieldname) { |
| 203 | - $x = isset(Check::$data[$fieldname]) ? Check::$data[$fieldname] : ''; |
|
| 204 | - return $value == $x; |
|
| 203 | + $x = isset(Check::$data[$fieldname]) ? Check::$data[$fieldname] : ''; |
|
| 204 | + return $value == $x; |
|
| 205 | 205 | }, |
| 206 | 206 | 'message' => "Field must be equal to {{arg_1}}.", |
| 207 | 207 | ], |
| 208 | 208 | |
| 209 | 209 | 'in_array' => [ |
| 210 | 210 | 'validate' => function($value,$array_values) { |
| 211 | - return in_array($value, $array_values); |
|
| 211 | + return in_array($value, $array_values); |
|
| 212 | 212 | }, |
| 213 | 213 | 'message' => "This value is forbidden.", |
| 214 | 214 | ], |
@@ -17,46 +17,46 @@ discard block |
||
| 17 | 17 | $errors = []; |
| 18 | 18 | public static $data = []; |
| 19 | 19 | |
| 20 | - public static function valid($rules, $data){ |
|
| 20 | + public static function valid($rules, $data) { |
|
| 21 | 21 | static::$errors = []; |
| 22 | 22 | Event::triggerOnce('core.check.init'); |
| 23 | - self::$data = ($data = (array)$data); |
|
| 23 | + self::$data = ($data = (array) $data); |
|
| 24 | 24 | |
| 25 | - foreach ((array)$rules as $field_name => $rule) { |
|
| 25 | + foreach ((array) $rules as $field_name => $rule) { |
|
| 26 | 26 | |
| 27 | 27 | $current = isset($data[$field_name]) ? $data[$field_name] : null; |
| 28 | 28 | |
| 29 | - if (is_callable($rule)){ |
|
| 30 | - static::$errors[$field_name] = call_user_func($rule,$current); |
|
| 29 | + if (is_callable($rule)) { |
|
| 30 | + static::$errors[$field_name] = call_user_func($rule, $current); |
|
| 31 | 31 | continue; |
| 32 | 32 | } elseif (is_string($rule)) { |
| 33 | 33 | $current_rules = array_flip(preg_split('/\s*\|\s*/', $rule)); |
| 34 | 34 | } else { |
| 35 | - $current_rules = (array)$rule; |
|
| 35 | + $current_rules = (array) $rule; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | static::$errors[$field_name] = true; |
| 39 | 39 | |
| 40 | - foreach($current_rules as $method => $message) { |
|
| 40 | + foreach ($current_rules as $method => $message) { |
|
| 41 | 41 | |
| 42 | 42 | $meth_name = strtok($method, ':'); |
| 43 | 43 | $opts = strtok(':') ?: ''; |
| 44 | 44 | $opts = $opts ? json_decode("[$opts]") : []; |
| 45 | 45 | $meth_opts = $opts ? array_merge([$current], $opts) : [$current]; |
| 46 | 46 | |
| 47 | - if ( static::$errors[$field_name] !== true ) continue 2; |
|
| 47 | + if (static::$errors[$field_name] !== true) continue 2; |
|
| 48 | 48 | |
| 49 | 49 | if (empty(static::$methods[$meth_name])) { |
| 50 | 50 | static::$errors[$field_name] = true; |
| 51 | 51 | } else { |
| 52 | - if (call_user_func_array(static::$methods[$meth_name]->validate,$meth_opts)){ |
|
| 52 | + if (call_user_func_array(static::$methods[$meth_name]->validate, $meth_opts)) { |
|
| 53 | 53 | static::$errors[$field_name] = true; |
| 54 | 54 | } else { |
| 55 | 55 | $arg = []; |
| 56 | 56 | foreach ($meth_opts as $key => $value) { |
| 57 | 57 | $arg["arg_$key"] = $value; |
| 58 | 58 | } |
| 59 | - static::$errors[$field_name] = Text::render(static::$methods[$meth_name]->message,$arg); |
|
| 59 | + static::$errors[$field_name] = Text::render(static::$methods[$meth_name]->message, $arg); |
|
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | } |
@@ -65,28 +65,28 @@ discard block |
||
| 65 | 65 | self::$data = []; |
| 66 | 66 | |
| 67 | 67 | // Clean non-errors |
| 68 | - static::$errors = array_filter(static::$errors,function($v){ |
|
| 68 | + static::$errors = array_filter(static::$errors, function($v) { |
|
| 69 | 69 | return $v !== true; |
| 70 | 70 | }); |
| 71 | 71 | |
| 72 | 72 | return empty(static::$errors); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - public static function method($name, $definition = null){ |
|
| 75 | + public static function method($name, $definition = null) { |
|
| 76 | 76 | if (is_array($name)) { |
| 77 | - foreach ($name as $method_name => $method_definition){ |
|
| 77 | + foreach ($name as $method_name => $method_definition) { |
|
| 78 | 78 | if (is_callable($method_definition)) $method_definition = ['validate' => $method_definition]; |
| 79 | 79 | if (empty($method_definition['validate']) || !is_callable($method_definition['validate'])) continue; |
| 80 | 80 | $method_definition['key'] = "core.check.error.$method_name"; |
| 81 | - $method_definition['message'] = Filter::with($method_definition['key'],@$method_definition['message']?:'Field not valid.'); |
|
| 82 | - static::$methods[$method_name] = (object)$method_definition; |
|
| 81 | + $method_definition['message'] = Filter::with($method_definition['key'], @$method_definition['message'] ?: 'Field not valid.'); |
|
| 82 | + static::$methods[$method_name] = (object) $method_definition; |
|
| 83 | 83 | } |
| 84 | 84 | } else { |
| 85 | 85 | if (is_callable($definition)) $definition = ['validate' => $definition]; |
| 86 | 86 | if (empty($definition['validate']) || !is_callable($definition['validate'])) return; |
| 87 | 87 | $methods['key'] = "core.check.error.$name"; |
| 88 | - $methods['message'] = Filter::with($methods['key'],@$methods['message']?:'Field not valid.'); |
|
| 89 | - static::$methods[$name] = (object)$definition; |
|
| 88 | + $methods['message'] = Filter::with($methods['key'], @$methods['message'] ?: 'Field not valid.'); |
|
| 89 | + static::$methods[$name] = (object) $definition; |
|
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
@@ -96,89 +96,89 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | -Event::on('core.check.init',function(){ |
|
| 99 | +Event::on('core.check.init', function() { |
|
| 100 | 100 | |
| 101 | 101 | Check::method([ |
| 102 | 102 | |
| 103 | 103 | 'required' => [ |
| 104 | 104 | 'validate' => function($value) { |
| 105 | - return (is_numeric($value) && $value==0) || !empty($value); |
|
| 105 | + return (is_numeric($value) && $value == 0) || !empty($value); |
|
| 106 | 106 | }, |
| 107 | 107 | 'message' => "This value cannot be empty.", |
| 108 | 108 | ], |
| 109 | 109 | |
| 110 | 110 | 'alphanumeric' => [ |
| 111 | 111 | 'validate' => function($value) { |
| 112 | - return (bool)preg_match('/^[0-9a-zA-Z]+$/',$value); |
|
| 112 | + return (bool) preg_match('/^[0-9a-zA-Z]+$/', $value); |
|
| 113 | 113 | }, |
| 114 | 114 | 'message' => "Value must be alphanumeric.", |
| 115 | 115 | ], |
| 116 | 116 | |
| 117 | 117 | 'numeric' => [ |
| 118 | 118 | 'validate' => function($value) { |
| 119 | - return (bool)preg_match('/^\d+$/',$value); |
|
| 119 | + return (bool) preg_match('/^\d+$/', $value); |
|
| 120 | 120 | }, |
| 121 | 121 | 'message' => "Value must be numeric.", |
| 122 | 122 | ], |
| 123 | 123 | |
| 124 | 124 | 'email' => [ |
| 125 | 125 | 'validate' => function($value) { |
| 126 | - return (bool)filter_var($value, FILTER_VALIDATE_EMAIL); |
|
| 126 | + return (bool) filter_var($value, FILTER_VALIDATE_EMAIL); |
|
| 127 | 127 | }, |
| 128 | 128 | 'message' => "This is not a valid email.", |
| 129 | 129 | ], |
| 130 | 130 | |
| 131 | 131 | 'url' => [ |
| 132 | 132 | 'validate' => function($value) { |
| 133 | - return (bool)filter_var($value, FILTER_VALIDATE_URL); |
|
| 133 | + return (bool) filter_var($value, FILTER_VALIDATE_URL); |
|
| 134 | 134 | }, |
| 135 | 135 | 'message' => "This is not a valid URL.", |
| 136 | 136 | ], |
| 137 | 137 | |
| 138 | 138 | 'max' => [ |
| 139 | - 'validate' => function($value,$max) { |
|
| 140 | - return $value<=$max ? true : false; |
|
| 139 | + 'validate' => function($value, $max) { |
|
| 140 | + return $value <= $max ? true : false; |
|
| 141 | 141 | }, |
| 142 | 142 | 'message' => "Value must be less than {{arg_1}}.", |
| 143 | 143 | ], |
| 144 | 144 | |
| 145 | 145 | 'min' => [ |
| 146 | - 'validate' => function($value,$min) { |
|
| 146 | + 'validate' => function($value, $min) { |
|
| 147 | 147 | return $value >= $min; |
| 148 | 148 | }, |
| 149 | 149 | 'message' => "Value must be greater than {{arg_1}}.", |
| 150 | 150 | ], |
| 151 | 151 | |
| 152 | 152 | 'range' => [ |
| 153 | - 'validate' => function($value,$min,$max) { |
|
| 154 | - return ( $value >= $min ) && ( $value <= $max ); |
|
| 153 | + 'validate' => function($value, $min, $max) { |
|
| 154 | + return ($value >= $min) && ($value <= $max); |
|
| 155 | 155 | }, |
| 156 | 156 | 'message' => "This value must be in [{{arg_1}},{{arg_2}}] range.", |
| 157 | 157 | ], |
| 158 | 158 | |
| 159 | 159 | 'words' => [ |
| 160 | - 'validate' => function($value,$max) { |
|
| 160 | + 'validate' => function($value, $max) { |
|
| 161 | 161 | return str_word_count($value) <= $max; |
| 162 | 162 | }, |
| 163 | 163 | 'message' => "Too many words, max count is {{arg_1}}.", |
| 164 | 164 | ], |
| 165 | 165 | |
| 166 | 166 | 'length' => [ |
| 167 | - 'validate' => function($value,$length) { |
|
| 167 | + 'validate' => function($value, $length) { |
|
| 168 | 168 | return strlen($value) == $length; |
| 169 | 169 | }, |
| 170 | 170 | 'message' => "This value must be {{arg_1}} characters.", |
| 171 | 171 | ], |
| 172 | 172 | |
| 173 | 173 | 'min_length' => [ |
| 174 | - 'validate' => function($value,$min) { |
|
| 174 | + 'validate' => function($value, $min) { |
|
| 175 | 175 | return strlen($value) >= $min; |
| 176 | 176 | }, |
| 177 | 177 | 'message' => "Too few characters, min count is {{arg_1}}.", |
| 178 | 178 | ], |
| 179 | 179 | |
| 180 | 180 | 'max_length' => [ |
| 181 | - 'validate' => function($value,$max) { |
|
| 181 | + 'validate' => function($value, $max) { |
|
| 182 | 182 | return strlen($value) <= $max; |
| 183 | 183 | }, |
| 184 | 184 | 'message' => "Too many characters, max count is {{arg_1}}.", |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | |
| 187 | 187 | 'true' => [ |
| 188 | 188 | 'validate' => function($value) { |
| 189 | - return (bool)$value; |
|
| 189 | + return (bool) $value; |
|
| 190 | 190 | }, |
| 191 | 191 | 'message' => "This value must be true.", |
| 192 | 192 | ], |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | ], |
| 200 | 200 | |
| 201 | 201 | 'same_as' => [ |
| 202 | - 'validate' => function($value,$fieldname) { |
|
| 202 | + 'validate' => function($value, $fieldname) { |
|
| 203 | 203 | $x = isset(Check::$data[$fieldname]) ? Check::$data[$fieldname] : ''; |
| 204 | 204 | return $value == $x; |
| 205 | 205 | }, |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | ], |
| 208 | 208 | |
| 209 | 209 | 'in_array' => [ |
| 210 | - 'validate' => function($value,$array_values) { |
|
| 210 | + 'validate' => function($value, $array_values) { |
|
| 211 | 211 | return in_array($value, $array_values); |
| 212 | 212 | }, |
| 213 | 213 | 'message' => "This value is forbidden.", |