Completed
Pull Request — master (#438)
by Anton
06:33
created

Validator::addRuleNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator;
12
13
use Bluz\Validator\Exception\ComponentException;
14
use Bluz\Validator\Rule\RuleInterface;
15
16
/**
17
 * Validator
18
 *
19
 * @package  Bluz\Validator
20
 * @author   Anton Shevchuk
21
 * @link     https://github.com/Respect/Validation
22
 *
23
 * @method static ValidatorChain alpha($additionalCharacters = '')
24
 * @method static ValidatorChain alphaNumeric($additionalCharacters = '')
25
 * @method static ValidatorChain array($callback)
26
 * @method static ValidatorChain between($min, $max)
27
 * @method static ValidatorChain betweenInclusive($min, $max)
28
 * @method static ValidatorChain callback($callable, $description = null)
29
 * @method static ValidatorChain condition($condition)
30
 * @method static ValidatorChain contains($containsValue)
31
 * @method static ValidatorChain containsStrict($containsValue)
32
 * @method static ValidatorChain countryCode()
33
 * @method static ValidatorChain creditCard()
34
 * @method static ValidatorChain date($format)
35
 * @method static ValidatorChain domain($checkDns = false)
36
 * @method static ValidatorChain email($checkDns = false)
37
 * @method static ValidatorChain equals($compareTo)
38
 * @method static ValidatorChain equalsStrict($compareTo)
39
 * @method static ValidatorChain float()
40
 * @method static ValidatorChain in($haystack)
41
 * @method static ValidatorChain inStrict($haystack)
42
 * @method static ValidatorChain integer()
43
 * @method static ValidatorChain ip($options = null)
44
 * @method static ValidatorChain json()
45
 * @method static ValidatorChain latin($additionalCharacters = '')
46
 * @method static ValidatorChain latinNumeric($additionalCharacters = '')
47
 * @method static ValidatorChain length($min = null, $max = null)
48
 * @method static ValidatorChain less($maxValue)
49
 * @method static ValidatorChain lessOrEqual($maxValue)
50
 * @method static ValidatorChain more($minValue)
51
 * @method static ValidatorChain moreOrEqual($minValue)
52
 * @method static ValidatorChain notEmpty()
53
 * @method static ValidatorChain noWhitespace()
54
 * @method static ValidatorChain numeric()
55
 * @method static ValidatorChain required()
56
 * @method static ValidatorChain regexp($expression, $description = null)
57
 * @method static ValidatorChain slug()
58
 * @method static ValidatorChain string()
59
 */
60
class Validator
61
{
62
    /**
63
     * @var array[] list of rules namespaces
64
     */
65
    protected static $rulesNamespaces = [
66
        '\\Bluz\\Validator\\Rule\\'
67
    ];
68
69
    /**
70
     * Create new instance if ValidatorChain
71
     *
72
     * @return ValidatorChain
73
     */
74 22
    public static function create(): ValidatorChain
75
    {
76 22
        return new ValidatorChain();
77
    }
78
79
    /**
80
     * Magic static call for create instance of Validator
81
     *
82
     * @param string $ruleName
83
     * @param array  $arguments
84
     *
85
     * @return ValidatorChain
86
     * @throws Exception\ComponentException
87
     */
88 11
    public static function __callStatic($ruleName, $arguments)
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 21
    public static function rule($ruleName, $arguments) : RuleInterface
104
    {
105 21
        $ruleName = ucfirst($ruleName) . 'Rule';
106
107
108 21
        foreach (static::$rulesNamespaces as $ruleNamespace) {
109 21
            $ruleClass = $ruleNamespace . $ruleName;
110 21
            if (class_exists($ruleClass)) {
111 21
                return new $ruleClass(...$arguments);
112
            }
113
        }
114
115 1
        throw new ComponentException("Class for validator `$ruleName` not found");
116
    }
117
118
    /**
119
     * Add rules path
120
     *
121
     * @param  string $path
122
     *
123
     * @return void
124
     */
125 1
    public static function addRuleNamespace(string $path)
126
    {
127 1
        static::$rulesNamespaces[] = rtrim($path, '\\') . '\\';
128 1
    }
129
}
130