Validator::rule()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Validator;
13
14
use Bluz\Validator\Exception\ComponentException;
15
use Bluz\Validator\Rule\RuleInterface;
16
17
/**
18
 * Validator
19
 *
20
 * @package  Bluz\Validator
21
 * @author   Anton Shevchuk
22
 * @link     https://github.com/Respect/Validation
23
 *
24
 * @method static ValidatorChain alpha($additionalCharacters = '')
25
 * @method static ValidatorChain alphaNumeric($additionalCharacters = '')
26
 * @method static ValidatorChain array($callback)
27
 * @method static ValidatorChain between($min, $max)
28
 * @method static ValidatorChain betweenInclusive($min, $max)
29
 * @method static ValidatorChain callback($callable, $description = null)
30
 * @method static ValidatorChain condition($condition)
31
 * @method static ValidatorChain contains($containsValue)
32
 * @method static ValidatorChain containsStrict($containsValue)
33
 * @method static ValidatorChain countryCode()
34
 * @method static ValidatorChain creditCard()
35
 * @method static ValidatorChain date($format)
36
 * @method static ValidatorChain domain($checkDns = false)
37
 * @method static ValidatorChain email($checkDns = false)
38
 * @method static ValidatorChain equals($compareTo)
39
 * @method static ValidatorChain equalsStrict($compareTo)
40
 * @method static ValidatorChain float()
41
 * @method static ValidatorChain in($haystack)
42
 * @method static ValidatorChain inStrict($haystack)
43
 * @method static ValidatorChain integer()
44
 * @method static ValidatorChain ip($options = null)
45
 * @method static ValidatorChain json()
46
 * @method static ValidatorChain latin($additionalCharacters = '')
47
 * @method static ValidatorChain latinNumeric($additionalCharacters = '')
48
 * @method static ValidatorChain length($min = null, $max = null)
49
 * @method static ValidatorChain less($maxValue)
50
 * @method static ValidatorChain lessOrEqual($maxValue)
51
 * @method static ValidatorChain more($minValue)
52
 * @method static ValidatorChain moreOrEqual($minValue)
53
 * @method static ValidatorChain notEmpty()
54
 * @method static ValidatorChain noWhitespace()
55
 * @method static ValidatorChain numeric()
56
 * @method static ValidatorChain required()
57
 * @method static ValidatorChain regexp($expression, $description = null)
58
 * @method static ValidatorChain slug()
59
 * @method static ValidatorChain string()
60
 */
61
class Validator
62
{
63
    /**
64
     * @var array[] list of rules namespaces
65
     */
66
    protected static $rulesNamespaces = [
67
        '\\Bluz\\Validator\\Rule\\'
68
    ];
69
70
    /**
71
     * Create new instance if ValidatorChain
72
     *
73
     * @return ValidatorChain
74
     */
75 23
    public static function create(): ValidatorChain
76
    {
77 23
        return new ValidatorChain();
78
    }
79
80
    /**
81
     * Magic static call for create instance of Validator
82
     *
83
     * @param string $ruleName
84
     * @param array  $arguments
85
     *
86
     * @return ValidatorChain
87
     */
88 11
    public static function __callStatic($ruleName, $arguments): ValidatorChain
89
    {
90 11
        $validatorChain = self::create();
91 11
        return $validatorChain->$ruleName(...$arguments);
92
    }
93
94
    /**
95
     * Create new rule by name
96
     *
97
     * @param string $ruleName
98
     * @param array $arguments
99
     *
100
     * @return RuleInterface
101
     * @throws Exception\ComponentException
102
     */
103 22
    public static function rule(string $ruleName, array $arguments): RuleInterface
104
    {
105 22
        $ruleName = ucfirst($ruleName) . 'Rule';
106
107 22
        foreach (static::$rulesNamespaces as $ruleNamespace) {
108 22
            $ruleClass = $ruleNamespace . $ruleName;
0 ignored issues
show
Bug introduced by
Are you sure $ruleNamespace of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            $ruleClass = /** @scrutinizer ignore-type */ $ruleNamespace . $ruleName;
Loading history...
109 22
            if (class_exists($ruleClass)) {
110 21
                return new $ruleClass(...$arguments);
111
            }
112
        }
113
114 1
        throw new ComponentException("Class for validator `$ruleName` not found");
115
    }
116
117
    /**
118
     * Add rules path
119
     *
120
     * @param  string $path
121
     *
122
     * @return void
123
     */
124 1
    public static function addRuleNamespace(string $path): void
125
    {
126 1
        static::$rulesNamespaces[] = rtrim($path, '\\') . '\\';
127 1
    }
128
}
129