RuleParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 102
dl 0
loc 130
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

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