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