Passed
Push — master ( acf216...a5b2aa )
by Smoren
02:21
created

StringCheckFactory::getNumericCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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