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