|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smoren\Validator\Factories\Checks; |
|
4
|
|
|
|
|
5
|
|
|
use Smoren\Validator\Factories\CheckBuilder; |
|
6
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
|
7
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
|
8
|
|
|
use Smoren\Validator\Structs\CheckName; |
|
9
|
|
|
|
|
10
|
|
|
class StringCheckFactory |
|
11
|
|
|
{ |
|
12
|
24 |
|
public static function getStringCheck(): CheckInterface |
|
13
|
|
|
{ |
|
14
|
24 |
|
return CheckBuilder::create(CheckName::STRING) |
|
15
|
24 |
|
->withPredicate(fn ($value) => \is_string($value)) |
|
16
|
24 |
|
->build(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
2 |
|
public static function getNumericCheck(): CheckInterface |
|
20
|
|
|
{ |
|
21
|
2 |
|
return CheckBuilder::create(CheckName::NUMERIC) |
|
22
|
2 |
|
->withPredicate(fn($value) => \is_numeric($value)) |
|
23
|
2 |
|
->build(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public static function getEmptyCheck(): CheckInterface |
|
27
|
|
|
{ |
|
28
|
2 |
|
return CheckBuilder::create(CheckName::EMPTY) |
|
29
|
2 |
|
->withPredicate(fn($value) => $value === '') |
|
30
|
2 |
|
->build(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public static function getNotEmptyCheck(): CheckInterface |
|
34
|
|
|
{ |
|
35
|
2 |
|
return CheckBuilder::create(CheckName::NOT_EMPTY) |
|
36
|
2 |
|
->withPredicate(fn($value) => $value !== '') |
|
37
|
2 |
|
->build(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public static function getMatchCheck(string $regex): CheckInterface |
|
41
|
|
|
{ |
|
42
|
2 |
|
return CheckBuilder::create(CheckName::MATCH) |
|
43
|
2 |
|
->withPredicate(fn($value, string $regex) => \boolval(\preg_match($regex, $value))) |
|
44
|
2 |
|
->withParams(['regex' => $regex]) |
|
45
|
2 |
|
->build(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public static function getHasSubstringCheck(string $substr): CheckInterface |
|
49
|
|
|
{ |
|
50
|
2 |
|
return CheckBuilder::create(CheckName::HAS_SUBSTRING) |
|
51
|
2 |
|
->withPredicate(fn($value, string $substr) => \mb_strpos($value, $substr) !== false) |
|
52
|
2 |
|
->withParams(['substring' => $substr]) |
|
53
|
2 |
|
->build(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public static function getStartsWithCheck(string $substr): CheckInterface |
|
57
|
|
|
{ |
|
58
|
2 |
|
return CheckBuilder::create(CheckName::STARTS_WITH) |
|
59
|
2 |
|
->withPredicate(fn($value, string $substr) => \mb_strpos($value, $substr) === 0) |
|
60
|
2 |
|
->withParams(['substring' => $substr]) |
|
61
|
2 |
|
->build(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public static function getEndsWithCheck(string $substr): CheckInterface |
|
65
|
|
|
{ |
|
66
|
2 |
|
return CheckBuilder::create(CheckName::ENDS_WITH) |
|
67
|
2 |
|
->withPredicate(static function ($value, string $substr) { |
|
68
|
2 |
|
return \substr($value, \mb_strlen($value) - \mb_strlen($substr)) === $substr; |
|
69
|
2 |
|
}) |
|
70
|
2 |
|
->withParams(['substring' => $substr]) |
|
71
|
2 |
|
->build(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public static function getLengthIsCheck(IntegerRuleInterface $rule): CheckInterface |
|
75
|
|
|
{ |
|
76
|
2 |
|
return CheckBuilder::create(CheckName::LENGTH_IS) |
|
77
|
2 |
|
->withPredicate(static function ($value) use ($rule) { |
|
78
|
2 |
|
$rule->validate(\mb_strlen($value)); |
|
79
|
1 |
|
return true; |
|
80
|
2 |
|
}) |
|
81
|
2 |
|
->build(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|