1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\Validator\Rules; |
4
|
|
|
|
5
|
|
|
use Smoren\Validator\Checks\Check; |
6
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
7
|
|
|
use Smoren\Validator\Interfaces\StringRuleInterface; |
8
|
|
|
use Smoren\Validator\Structs\CheckErrorName; |
9
|
|
|
use Smoren\Validator\Structs\CheckName; |
10
|
|
|
|
11
|
|
|
class StringRule extends Rule implements StringRuleInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param string $name |
15
|
|
|
*/ |
16
|
|
|
public function __construct(string $name) |
17
|
|
|
{ |
18
|
|
|
parent::__construct($name); |
19
|
|
|
$this->check(new Check( |
20
|
|
|
CheckName::STRING, |
21
|
|
|
CheckErrorName::NOT_STRING, |
22
|
|
|
fn ($value) => \is_string($value) |
23
|
|
|
), true); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function numeric(): StringRuleInterface |
27
|
|
|
{ |
28
|
|
|
return $this->check(new Check( |
29
|
|
|
CheckName::NUMERIC, |
30
|
|
|
CheckErrorName::NOT_NUMERIC, |
31
|
|
|
fn ($value) => is_numeric($value) |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function empty(): StringRuleInterface |
36
|
|
|
{ |
37
|
|
|
return $this->check(new Check( |
38
|
|
|
CheckName::EMPTY, |
39
|
|
|
CheckErrorName::NOT_EMPTY, |
40
|
|
|
fn ($value) => $value === '' |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function notEmpty(): StringRuleInterface |
45
|
|
|
{ |
46
|
|
|
return $this->check(new Check( |
47
|
|
|
CheckName::NOT_EMPTY, |
48
|
|
|
CheckErrorName::EMPTY, |
49
|
|
|
fn ($value) => $value !== '' |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function match(string $regex): StringRuleInterface |
54
|
|
|
{ |
55
|
|
|
return $this->check(new Check( |
56
|
|
|
CheckName::MATCH, |
57
|
|
|
CheckErrorName::NOT_MATCH, |
58
|
|
|
fn ($value) => \preg_match($regex, $value) |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function hasSubstring(string $substr): StringRuleInterface |
63
|
|
|
{ |
64
|
|
|
return $this->check(new Check( |
65
|
|
|
CheckName::HAS_SUBSTRING, |
66
|
|
|
CheckErrorName::HAS_NOT_SUBSTRING, |
67
|
|
|
fn ($value) => \mb_strpos($value, $substr) !== false |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function startsWith(string $substr): StringRuleInterface |
72
|
|
|
{ |
73
|
|
|
return $this->check(new Check( |
74
|
|
|
CheckName::STARTS_WITH, |
75
|
|
|
CheckErrorName::NOT_STARTS_WITH, |
76
|
|
|
fn ($value) => \mb_strpos($value, $substr) === 0 |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function endsWith(string $substr): StringRuleInterface |
81
|
|
|
{ |
82
|
|
|
return $this->check(new Check( |
83
|
|
|
CheckName::STARTS_WITH, |
84
|
|
|
CheckErrorName::NOT_STARTS_WITH, |
85
|
|
|
fn ($value) => \mb_strpos($value, $substr) === \mb_strlen($value) - \mb_strlen($substr) |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function lengthIs(IntegerRuleInterface $rule): StringRuleInterface |
90
|
|
|
{ |
91
|
|
|
return $this->check(new Check( |
92
|
|
|
CheckName::LENGTH_IS, |
93
|
|
|
CheckErrorName::BAD_LENGTH, |
94
|
|
|
static function ($value) use ($rule) { |
95
|
|
|
/** @var string $value */ |
96
|
|
|
$rule->validate(\mb_strlen($value)); |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|