-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
138 lines (121 loc) · 4.13 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
class Validator
{
protected $data;
protected $rules;
protected $errors = [];
public function __construct($data, $rules)
{
$this->data = $data;
$this->rules = $rules;
}
public static function make($data, $rules)
{
return new self($data, $rules);
}
public function fails()
{
$this->validate();
return !empty($this->errors);
}
public function messages()
{
return $this->errors;
}
protected function validate()
{
foreach ($this->rules as $field => $rules) {
$rules = explode('|', $rules);
foreach ($rules as $rule) {
if (strpos($rule, ':')) {
list($rule, $param) = explode(':', $rule);
} else {
$param = null;
}
$method = "validate" . ucfirst($rule);
if (method_exists($this, $method)) {
$this->$method($field, $param);
}
}
}
}
protected function validateRequired($field)
{
if (empty($this->data[$field])) {
$this->errors[$field][] = "The $field field is required.";
}
}
protected function validateMax($field, $param)
{
if (strlen($this->data[$field]) > $param) {
$this->errors[$field][] = "The $field field may not be greater than $param characters.";
}
}
protected function validateRegex($field, $param)
{
if (!preg_match($param, $this->data[$field])) {
$this->errors[$field][] = "The $field field format is invalid.";
}
}
protected function validateMin($field, $param)
{
if (strlen($this->data[$field]) < $param) {
$this->errors[$field][] = "The $field field must be at least $param characters.";
}
}
protected function validateCaptcha($field)
{
$secretKey = 'YOUR_SECRET_KEY'; // replace with your actual secret key
$response = $this->data[$field];
$remoteIp = $_SERVER['REMOTE_ADDR'];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => $secretKey,
'response' => $response,
'remoteip' => $remoteIp
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$resultJson = json_decode($result);
if (!$resultJson->success) {
$this->errors[$field][] = "The $field field is invalid.";
}
}
protected function validateFileSize($field, $param)
{
if (isset($this->data[$field]) && $this->data[$field]['size'] > $param) {
$this->errors[$field][] = "The $field file size may not be greater than " . ($param / 1024) . " KB.";
}
}
protected function validateFileType($field, $param)
{
if (isset($this->data[$field])) {
$fileType = pathinfo($this->data[$field]['name'], PATHINFO_EXTENSION);
$allowedTypes = explode(',', $param);
if (!in_array($fileType, $allowedTypes)) {
$this->errors[$field][] = "The $field file type must be one of the following: " . implode(', ', $allowedTypes) . ".";
}
}
}
protected function validateDateFormat($field, $param)
{
$date = DateTime::createFromFormat($param, $this->data[$field]);
if (!$date || $date->format($param) !== $this->data[$field]) {
$this->errors[$field][] = "The $field field does not match the format $param.";
}
}
protected function validateDatetimeFormat($field, $param)
{
$date = DateTime::createFromFormat($param, $this->data[$field]);
if (!$date || $date->format($param) !== $this->data[$field]) {
$this->errors[$field][] = "The $field field does not match the datetime format $param.";
}
}
}