ValidatorNormalizer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
D normalize() 0 27 10
1
<?php
2
namespace Wandu\Validator;
3
4
use Wandu\Validator\Contracts\Rule;
5
use Wandu\Validator\Contracts\RuleNormalizable;
6
7
class ValidatorNormalizer implements RuleNormalizable
8
{
9 12
    public function normalize($rule): array
10
    {
11 12
        while (is_callable($rule) || (is_object($rule) && $rule instanceof Rule)) {
12 5
            if (is_callable($rule)) {
13 4
                $rule = call_user_func($rule);
14
            } else {
15 4
                $rule = $rule->rules();
16
            }
17
        }
18
        // string -> array
19 12
        if (!is_array($rule)) {
20 11
            $rule = [$rule];
21
        }
22 12
        $normalized = [[], []];
23 12
        foreach ($rule as $key => $value) {
24 12
            if (is_int($key) || $key === '' || $key === null) {
25 12
                $normalized[0] = array_merge($normalized[0] ?? [], (array) $value);
26
            } else {
27 9
                $target = TargetName::parse($key);
28 9
                $normalized[1][] = [
29 9
                    [$target->getName(), $target->getIterator(), $target->isOptional(), ],
30 12
                    $this->normalize($value),
31
                ];
32
            }
33
        }
34 12
        return $normalized;
35
    }
36
}
37