ArrayNormalizer::ensureType()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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);
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