Passed
Push — master ( 619936...64747c )
by Koldo
02:06
created

RuleParser::getRuleMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Validator;
6
7
class RuleParser
8
{
9
    const RULES = [
10
        'singleParam' => [
11
            'alnum', // (mixed $value);
12
            'base64', // (string $value);
13
            'boolean', // (mixed $value);
14
            'classExists', // (mixed $value);
15
            'defined', // (mixed $constant);
16
            'digit', // (mixed $value);
17
            'directory', // (string $value);
18
            'e164', // (string $value);
19
            'email', // (mixed $value); DOCUMENTED
20
            'extensionLoaded', // (mixed $value);
21
            'false', // (mixed $value);
22
            'file', // (string $value);
23
            'float', // (mixed $value);
24
            'integer', // (mixed $value);
25
            'integerish', // (mixed $value);
26
            'interfaceExists', // (mixed $value);
27
            'isArray', // (mixed $value);
28
            'isArrayAccessible', // (mixed $value);
29
            'isCallable', // (mixed $value);
30
            'isJsonString', // (mixed $value);
31
            'isObject', // (mixed $value);
32
            'isResource', // (mixed $value);
33
            'isTraversable', // (mixed $value);
34
            'noContent', // (mixed $value);
35
            'notBlank', // (mixed $value);
36
            'notEmpty', // (mixed $value); DOCUMENTED
37
            'notNull', // (mixed $value);
38
            'null', // (mixed $value);
39
            'numeric', // (mixed $value);
40
            'objectOrClass', // (mixed $value);
41
            'readable', // (string $value);
42
            'scalar', // (mixed $value);
43
            'string', // (mixed $value);
44
            'true', // (mixed $value);
45
            'url', // (mixed $value);
46
            'uuid', // (string $value);
47
            'writeable', // (string $value);
48
            'choice', // (mixed $value, array $choices); DOCUMENTED
49
            'choicesNotEmpty', // (array $values, array $choices);
50
            'contains', // (mixed $string, string $needle);
51
            'count', // (array|\Countable $countable, int $count);
52
            'date', // (string $value, string $format); DOCUMENTED
53
            'endsWith', // (mixed $string, string $needle);
54
            'eq', // (mixed $value, mixed $value2);
55
            'greaterOrEqualThan', // (mixed $value, mixed $limit);
56
            'greaterThan', // (mixed $value, mixed $limit);
57
            'implementsInterface', // (mixed $class, string $interfaceName);
58
            'inArray', // (mixed $value, array $choices);
59
            'ip', // (string $value, int $flag = null);
60
            'ipv4', // (string $value, int $flag = null);
61
            'ipv6', // (string $value, int $flag = null);
62
            'isInstanceOf', // (mixed $value, string $className);
63
            'keyExists', // (mixed $value, string|int $key);
64
            'keyIsset', // (mixed $value, string|int $key);
65
            'keyNotExists', // (mixed $value, string|int $key);
66
            'length', // (mixed $value, int $length);
67
            'lessOrEqualThan', // (mixed $value, mixed $limit);
68
            'lessThan', // (mixed $value, mixed $limit);
69
            'max', // (mixed $value, mixed $maxValue);
70
            'maxLength', // (mixed $value, int $maxLength); DOCUMENTED
71
            'methodExists', // (string $value, mixed $object);
72
            'min', // (mixed $value, mixed $minValue);
73
            'minLength', // (mixed $value, int $minLength); DOCUMENTED
74
            'notEmptyKey', // (mixed $value, string|int $key);
75
            'notEq', // (mixed $value1, mixed $value2);
76
            'notInArray', // (mixed $value, array $choices); DOCUMENTED
77
            'notIsInstanceOf', // (mixed $value, string $className);
78
            'notSame', // (mixed $value1, mixed $value2);
79
            'phpVersion', // (string $operator, mixed $version);
80
            'propertiesExist', // (mixed $value, array $properties);
81
            'propertyExists', // (mixed $value, string $property);
82
            'regex', // (mixed $value, string $pattern);
83
            'same', // (mixed $value, mixed $value2);
84
            'satisfy', // (mixed $value, callable $callback);
85
            'startsWith', // (mixed $string, string $needle);
86
            'subclassOf', // (mixed $value, string $className);
87
        ],
88
        'multipleParams' => [
89
            'between', // (mixed $value, mixed $lowerLimit, mixed $upperLimit);
90
            'betweenExclusive', // (mixed $value, mixed $lowerLimit, mixed $upperLimit);
91
            'betweenLength', // (mixed $value, int $minLength, int $maxLength); DOCUMENTED
92
            'extensionVersion', // (string $extension, string $operator, mixed $version);
93
            'range', // (mixed $value, mixed $minValue, mixed $maxValue);
94
            'version', // (string $version1, string $operator, string $version2);
95
        ]
96
    ];
97
98 7
    public static function getRuleMethod(string $assert): string
99
    {
100 7
        return key(array_filter(
0 ignored issues
show
Bug Best Practice introduced by
The expression return key(array_filter(...on(...) { /* ... */ })) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
101 7
            self::RULES,
102
            function (array $availableRules) use ($assert): bool {
103 7
                return in_array($assert, $availableRules);
104 7
            }
105
        ));
106
    }
107
108 5
    public static function getRule(string $rule, ?string $plainConstrains)
109
    {
110 5
        $method = self::getRuleMethod($rule);
111
112 5
        return call_user_func_array([self::class, $method], [$plainConstrains]);
113
    }
114
115 5
    private static function singleParam(?string $validation)
116
    {
117 5
        if (preg_match_all("@\((.+)\)@", $validation, $matches)) {
118 2
            $validation = explode(',', $matches[1][0]);
119
        }
120
121 5
        return $validation;
122
    }
123
124 1
    private static function multipleParams(?string $validation): array
125
    {
126
        return array_map(function ($rule) {
127 1
            return call_user_func_array([self::class, 'singleParam'], [$rule]);
128 1
        }, explode(',', $validation));
129
    }
130
}
131