Passed
Pull Request — master (#376)
by
unknown
02:38
created

RulesDumper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 21
c 1
b 0
f 1
dl 0
loc 69
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A asArray() 0 3 1
B fetchOptions() 0 33 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use InvalidArgumentException;
8
9
use function is_int;
10
use function is_string;
11
12
/**
13
 * RulesDumper allows to get an array of rule names and corresponding settings from a set of rules.
14
 * The array is usually passed to the client to use it in client-side validation.
15
 *
16
 * * @see SerializableRuleInterface
17
 * * @see RuleInterface
18
 */
19
final class RulesDumper
20
{
21
    /**
22
     * Return all attribute rules as array.
23
     *
24
     * For example:
25
     *
26
     * ```php
27
     * [
28
     *    'amount' => [
29
     *        [
30
     *            'number',
31
     *            'integer' => true,
32
     *            'max' => 100,
33
     *            'notANumberMessage' => 'Value must be an integer.',
34
     *            'tooBigMessage' => 'Value must be no greater than 100.'
35
     *        ],
36
     *        ['callback'],
37
     *    ],
38
     *    'name' => [
39
     *        [
40
     *            'hasLength',
41
     *            'max' => 20,
42
     *            'message' => 'Value must contain at most 20 characters.'
43
     *        ],
44
     *    ],
45
     * ]
46
     * ```
47
     *
48
     * @param iterable $rules
49
     */
50 10
    public function asArray(iterable $rules): array
51
    {
52 10
        return $this->fetchOptions($rules);
53
    }
54
55 10
    private function fetchOptions(iterable $rules): array
56
    {
57 10
        $result = [];
58
        /** @var mixed $attribute */
59
        /** @var mixed $rule */
60 10
        foreach ($rules as $attribute => $rule) {
61 10
            if (!is_int($attribute) && !is_string($attribute)) {
62 1
                $message = sprintf(
63
                    'An attribute can only have an integer or a string type. %s given.',
64 1
                    get_debug_type($attribute),
65
                );
66
67 1
                throw new InvalidArgumentException($message);
68
            }
69
70 9
            if (is_iterable($rule)) {
71 3
                $options = $this->fetchOptions($rule);
72 9
            } elseif ($rule instanceof SerializableRuleInterface) {
73 8
                $options = array_merge([$rule->getName()], $rule->getOptions());
74 2
            } elseif ($rule instanceof RuleInterface) {
75 1
                $options = [$rule->getName()];
76
            } else {
77 1
                throw new InvalidArgumentException(sprintf(
78
                    'Each rule must implement "%s". Type "%s" given.',
79
                    RuleInterface::class,
80 1
                    get_debug_type($rule),
81
                ));
82
            }
83
84 8
            $result[$attribute] = $options;
85
        }
86
87 8
        return $result;
88
    }
89
}
90