Completed
Push — master ( a97ce7...6f972b )
by Jan
03:17
created

ArrayNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolveRules() 0 17 4
A ensureType() 0 5 3
A normalize() 0 9 1
1
<?php
2
3
namespace Kontrolio\Rules;
4
5
use UnexpectedValueException;
6
7
final class ArrayNormalizer
8
{
9
    private $parser;
10
11
    public function __construct(Parser $parser)
12
    {
13
        $this->parser = $parser;
14
    }
15
16
    /**
17
     * Normalizes "raw" array of rules.
18
     *
19
     * This method turns the following array structure
20
     *
21
     * [
22
     *     'foo' => 'equal_to:foo|not_equal_to:bar',
23
     *     'bar' => new EqualTo('foo'),
24
     *     'qux' => static function ($value) {
25
     *         return $value === 'qux';
26
     *     }
27
     * ]
28
     *
29
     * to this one
30
     *
31
     * [
32
     *    'foo' => [
33
     *        new EqualTo('foo'),
34
     *        new NotEqualTo('bar')
35
     *    ],
36
     *    'bar' => [
37
     *        new EqualTo('foo'),
38
     *    ],
39
     *    'qux' => [
40
     *        static function ($value) {
41
     *            return $value === 'qux';
42
     *        }
43
     *    ]
44
     * ]
45
     *
46
     * @param array $raw
47
     *
48
     * @return RuleInterface[]|callable[]
49
     * @throws UnexpectedValueException
50
     */
51
    public function normalize(array $raw)
52
    {
53
        $attributes = array_keys($raw);
54
55
        $rules = array_map(function ($attribute) use ($raw) {
56
            return $this->resolveRules($raw[$attribute]);
57
        }, $attributes);
58
59
        return array_combine($attributes, $rules);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_combine($attributes, $rules) could also return false which is incompatible with the documented return type Kontrolio\Rules\RuleInte...]|array<mixed,callable>. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
60
    }
61
62
    /**
63
     * Resolves rules from different formats.
64
     *
65
     * @param RuleInterface[]|callable[]|RuleInterface|callable|string $rules
66
     *
67
     * @return RuleInterface[]
68
     * @throws UnexpectedValueException
69
     */
70
    private function resolveRules($rules)
71
    {
72
        if (is_array($rules)) {
73
            foreach ($rules as $rule) {
74
                static::ensureType($rule);
75
            }
76
77
            return $rules;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rules returns an array which contains values of type callable which are incompatible with the documented value type Kontrolio\Rules\RuleInterface.
Loading history...
78
        }
79
80
        if (is_string($rules)) {
81
            return $this->parser->parse($rules);
82
        }
83
84
        static::ensureType($rules);
85
86
        return [$rules];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($rules) returns an array which contains values of type callable which are incompatible with the documented value type Kontrolio\Rules\RuleInterface.
Loading history...
87
    }
88
89
    /**
90
     * Checks rule for the proper type.
91
     *
92
     * @param mixed $rule
93
     * @throws UnexpectedValueException
94
     */
95
    private static function ensureType($rule)
96
    {
97
        if (!$rule instanceof RuleInterface && !is_callable($rule)) {
98
            throw new UnexpectedValueException(
99
                sprintf('Rule must implement `%s` or be callable.', RuleInterface::class)
100
            );
101
        }
102
    }
103
}
104