Skip to content

Commit 444cbc9

Browse files
committed
added: email & string validators
1 parent 932578e commit 444cbc9

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

src/Validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use function is_object;
1212
use function sprintf;
1313

14-
class Validator
14+
final class Validator
1515
{
1616
/**
1717
* @var array<string,array>

src/Validators/EmailValidator.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenVoid\Validator\Validators;
6+
7+
use function filter_var;
8+
use function is_string;
9+
10+
class EmailValidator extends AbstractValidator
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private string $message = '{{ value }} is not a valid email address.';
16+
17+
public function validate($value) : bool
18+
{
19+
if ($value === null) {
20+
return true;
21+
}
22+
23+
if (! is_string($value)) {
24+
$this->error($this->message, ['value' => $value]);
25+
return false;
26+
}
27+
28+
if (! filter_var($value, FILTER_VALIDATE_EMAIL)) {
29+
$this->error($this->message, ['value' => $value]);
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
36+
public function message(string $message) : self
37+
{
38+
$this->message = $message;
39+
return $this;
40+
}
41+
}

src/Validators/StringValidator.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenVoid\Validator\Validators;
6+
7+
use function is_int;
8+
use function strlen;
9+
10+
class StringValidator extends AbstractValidator
11+
{
12+
private string $invalid_message = 'Invalid type given. String expected.';
13+
private string $min_message = '{{ value }} must be at least {{ limit }} characters long';
14+
private string $max_message = '{{ value }} cannot be longer than {{ limit }} characters';
15+
private ?int $min = null;
16+
private ?int $max = null;
17+
18+
public function validate($value) : bool
19+
{
20+
if (null === $value) {
21+
return true;
22+
}
23+
24+
if (! is_string($value)) {
25+
$this->error($this->invalid_message, ['value' => $value]);
26+
return false;
27+
}
28+
29+
if (is_int($this->min) && strlen($value) < $this->min) {
30+
$this->error($this->min_message, ['value' => $value, 'limit' => $this->min]);
31+
return false;
32+
}
33+
34+
if (is_int($this->max) && strlen($value) > $this->max) {
35+
$this->error($this->max_message, ['value' => $value, 'limit' => $this->max]);
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
42+
public function invalid_message(string $invalid_message) : self
43+
{
44+
$this->invalid_message = $invalid_message;
45+
return $this;
46+
}
47+
48+
public function min_message(string $min_message) : self
49+
{
50+
$this->min_message = $min_message;
51+
return $this;
52+
}
53+
54+
public function max_message(string $max_message) : self
55+
{
56+
$this->max_message = $max_message;
57+
return $this;
58+
}
59+
60+
public function min(int $min) : self
61+
{
62+
$this->min = $min;
63+
return $this;
64+
}
65+
66+
public function max(int $max) : self
67+
{
68+
$this->max = $max;
69+
return $this;
70+
}
71+
}

0 commit comments

Comments
 (0)