Skip to content

Commit 1c51198

Browse files
committed
config.class and validation library
1 parent 27cf308 commit 1c51198

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

index.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
44

55

66
require_once 'vendor/autoload.php';
7+
8+
$validation = new LuckyStar\Validation\Validate;
9+
10+
11+
$rules = [
12+
'username' => 'max_length[5]|min_length[3]',
13+
'password' => [
14+
'rules' => 'max_length[5]|min_length[3]',
15+
'error_messages' => [
16+
'max_length' => 'Password is too long',
17+
'min_length' => 'Password is too short'
18+
]
19+
]
20+
];
21+
22+
$_POST = ['username' => '12345', 'password' => '12345'];
23+
24+
echo '<pre>';
25+
echo print_r($validation->validateRule($_POST, $rules));
26+
27+
echo '</pre>';
28+
29+
30+

src/config.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace LuckyStar\Validation;
4+
5+
6+
class Config
7+
{
8+
9+
10+
11+
private $rule_pattern = [
12+
'/max_length/',
13+
'/min_length/',
14+
'/is_numeric/',
15+
'/required/',
16+
'/valid_email/',
17+
'/valid_url/',
18+
];
19+
20+
21+
private $error_messages = [
22+
'max_length' => 'The :field is too long',
23+
'min_length' => 'The :field is too short',
24+
'is_numeric' => 'The :field must be a number',
25+
'required' => 'The :field is required',
26+
'valid_email' => 'The :field must be a valid email',
27+
'valid_url' => 'The :field must be a valid url',
28+
];
29+
30+
31+
32+
}

src/validation.library.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,99 @@
33
namespace LuckyStar\Validation;
44

55

6-
class Validate
6+
use LuckyStar\Validation\Config;
7+
8+
class Validate extends Config
79
{
810

11+
private static $errors = []; //for returning errors
12+
13+
14+
private static $rules = [];
15+
private static $fields = [];
16+
private static $errorMessages = [];
17+
18+
private static $library_errors = [];
19+
20+
21+
private static function checkRules($data,$rule){
22+
//string match.. max_length match max_length[5]
23+
if(preg_match('/max_length/', $rule)){
24+
echo "var";
25+
26+
}
27+
28+
29+
return $rule." <br>";
30+
}
31+
32+
private static function getRules($rules) : array {
33+
34+
foreach ($rules as $v => $r){
35+
if (is_array($r)){
36+
//post name => rules
37+
38+
if(!isset($r['rules'])){
39+
self::$library_errors[] = $v.' Rules key is missing';
40+
return [];
41+
}
42+
43+
44+
self::$rules[$v] = explode('|', $r['rules']);
45+
46+
if (isset($r['error_messages'])){
47+
self::$errorMessages[$v] = $r['error_messages'];
48+
}
49+
50+
51+
}else{
52+
self::$rules[$v] = explode('|', $r);
53+
54+
55+
if(sizeof(self::$rules[$v]) <= 1 && empty(self::$rules[$v][0])){
56+
self::$library_errors[] = $v.' Rules key is missing';
57+
return [];
58+
}
59+
}
60+
}
61+
62+
return self::$rules;
63+
64+
65+
}
66+
67+
public static function validateRule($data, $rules) : array {
68+
69+
$errors = [];
70+
$rulesArray = self::getRules($rules);
71+
72+
73+
//check if rules array is empty
74+
if (empty($rulesArray)){
75+
return self::$library_errors;
76+
}
77+
78+
79+
80+
81+
82+
//check rules..
83+
84+
foreach(self::$rules as $key => $value){
85+
foreach($value as $k => $v) {
86+
//check if the rule has a parameter
87+
$rule = self::checkRules($data, $v);
88+
89+
echo $rule;
90+
91+
}
92+
93+
}
94+
95+
96+
return [ self::$rules ,self::$errorMessages];
97+
}
98+
999

10100
}
11101

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
return array(
99
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
10+
'LuckyStar\\Validation\\Config' => $baseDir . '/src/config.php',
1011
'LuckyStar\\Validation\\Validate' => $baseDir . '/src/validation.library.php',
1112
);

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ComposerStaticInit74b93a93619eb4431518cfcc1aee0a3c
88
{
99
public static $classMap = array (
1010
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
11+
'LuckyStar\\Validation\\Config' => __DIR__ . '/../..' . '/src/config.php',
1112
'LuckyStar\\Validation\\Validate' => __DIR__ . '/../..' . '/src/validation.library.php',
1213
);
1314

0 commit comments

Comments
 (0)