Skip to content

Commit 41c26a9

Browse files
committed
patterns
1 parent 1c51198 commit 41c26a9

File tree

3 files changed

+108
-16
lines changed

3 files changed

+108
-16
lines changed

index.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010

1111
$rules = [
12-
'username' => 'max_length[5]|min_length[3]',
12+
'username' => [
13+
'rules' => 'max_length[10]|min_length[5]'
14+
],
1315
'password' => [
14-
'rules' => 'max_length[5]|min_length[3]',
16+
'rules' => 'max_length[10]|min_length[5]',
1517
'error_messages' => [
1618
'max_length' => 'Password is too long',
1719
'min_length' => 'Password is too short'
@@ -21,10 +23,19 @@
2123

2224
$_POST = ['username' => '12345', 'password' => '12345'];
2325

24-
echo '<pre>';
25-
echo print_r($validation->validateRule($_POST, $rules));
2626

27-
echo '</pre>';
27+
28+
$checker = $validation->validateRule($_POST, $rules);
29+
30+
31+
if ($checker){
32+
//no errors
33+
echo "No errors";
34+
}else{
35+
print_r($validation->getErrors());
36+
}
37+
38+
2839

2940

3041

src/config.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Config
88

99

1010

11-
private $rule_pattern = [
11+
protected static $rule_pattern = [
1212
'/max_length/',
1313
'/min_length/',
1414
'/is_numeric/',
@@ -18,13 +18,36 @@ class Config
1818
];
1919

2020

21-
private $error_messages = [
21+
protected static $rule_pattern_check = [
22+
'/max_length/' => [
23+
'parameter' => true,
24+
'name' => 'max_length',
25+
'pattern' => '/max_length\[\d+\]/',
26+
'pattern_check' => '/max_length\[(\d+)\]/',
27+
'pattern_type' => 'numeric',
28+
'not_numeric_rule' => 'max_length can only be controlled numerically.example:max_length[5]'
29+
],
30+
'/min_length/' => [
31+
'parameter' => true,
32+
'name' => 'min_length',
33+
'pattern' => '/min_length\[\d+\]/',
34+
'pattern_check' => '/min_length\[(\d+)\]/',
35+
'pattern_type' => 'numeric',
36+
'not_numeric_rule' => 'min_length can only be controlled numerically. example:min_length[1]'
37+
],
38+
39+
];
40+
41+
42+
43+
protected static $error_messages = [
2244
'max_length' => 'The :field is too long',
2345
'min_length' => 'The :field is too short',
2446
'is_numeric' => 'The :field must be a number',
2547
'required' => 'The :field is required',
2648
'valid_email' => 'The :field must be a valid email',
2749
'valid_url' => 'The :field must be a valid url',
50+
'numeric' => 'The :field must be a number',
2851
];
2952

3053

src/validation.library.php

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,66 @@ class Validate extends Config
1818
private static $library_errors = [];
1919

2020

21-
private static function checkRules($data,$rule){
21+
private static function checkRules($data,$rule) : bool{
2222
//string match.. max_length match max_length[5]
23-
if(preg_match('/max_length/', $rule)){
24-
echo "var";
23+
foreach (self::$rule_pattern as $pattern){
24+
if (preg_match($pattern, $rule)){
2525

26+
$rulePattern = self::$rule_pattern_check[$pattern];
27+
28+
29+
30+
if (!preg_match($rulePattern['pattern'], $rule, $matches)){
31+
self::$library_errors[] = $rulePattern['not_numeric_rule'];
32+
return false;
33+
}
34+
35+
if ($rulePattern['parameter']){
36+
37+
preg_match($rulePattern['pattern_check'] ,$rule, $matches);
38+
39+
if (!is_numeric($matches[1])){
40+
self::$library_errors[] = $rulePattern['not_numeric_rule'];
41+
return false;
42+
}
43+
44+
45+
46+
if($rulePattern['pattern_type'] == "numeric"){
47+
//numeric check
48+
$numerical = $matches[1];
49+
50+
$checkStatus = true;
51+
52+
53+
54+
if ($rulePattern['name'] == 'max_length'){
55+
if (strlen($data['value']) > $numerical){
56+
$checkStatus = false;
57+
}
58+
}if ($rulePattern['name'] == 'min_length') {
59+
if (strlen($data['value']) < $numerical) {
60+
$checkStatus = false;
61+
}
62+
}
63+
64+
65+
66+
if($checkStatus === false){
67+
self::$errors['status'] = 0;
68+
if (isset(self::$errorMessages[$data['name']]['min_length'])){
69+
self::$errors[] = self::$errorMessages[$data['name']]['min_length'];
70+
}else{
71+
self::$errors[] = str_replace(':field', $data['name'], self::$error_messages[$rulePattern['name']]);
72+
}
73+
}
74+
}
75+
}
76+
}
2677
}
2778

2879

29-
return $rule." <br>";
80+
return true;
3081
}
3182

3283
private static function getRules($rules) : array {
@@ -84,16 +135,23 @@ public static function validateRule($data, $rules) : array {
84135
foreach(self::$rules as $key => $value){
85136
foreach($value as $k => $v) {
86137
//check if the rule has a parameter
87-
$rule = self::checkRules($data, $v);
138+
$rule = self::checkRules(['name' => $key, 'value' => $data[$key]], $v);
139+
if (!$rule){
140+
return self::$library_errors;
141+
}
142+
}
143+
}
88144

89-
echo $rule;
90145

91-
}
92146

93-
}
147+
return self::$errors;
148+
149+
}
150+
94151

95152

96-
return [ self::$rules ,self::$errorMessages];
153+
public function getErrors(){
154+
return self::$errors;
97155
}
98156

99157

0 commit comments

Comments
 (0)