-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.php
More file actions
43 lines (34 loc) · 1.15 KB
/
validator.php
File metadata and controls
43 lines (34 loc) · 1.15 KB
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
<?php
use TinyTalkMessages\Validation\Interfaces\ValidatorInterface;
class Validator implements ValidatorInterface
{
private \TinyTalkMessages\Validation\Data\Errors $errors;
public function validate(array $data): bool
{
$this->errors = new TinyTalkMessages\Validation\Data\Errors();
$valid = true;
if (empty($data)) {
$this->errors->add(new \TinyTalkMessages\Validation\Data\ErrorField('email', 'Email is required.'));
$valid = false;
}
$email = $data['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->errors->add(new \TinyTalkMessages\Validation\Data\ErrorField('email', 'Invalid email.'));
$valid = false;
}
return $valid;
}
public function getErrors(): \TinyTalkMessages\Validation\Data\Errors
{
return $this->errors;
}
}
$validator = new Validator();
$validator->validate([]); // false
$validator->getErrors();
// Errors {
// errors =>
// { 0 => ErrorField (field => 'email', message => 'Email is required.')}
// }
// }
$validator->validate(['email' => 'example@example.com']); // true