Passed
Pull Request — master (#175)
by Alexander
02:36
created

RulesDumper::asArray()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 17
ccs 8
cts 11
cp 0.7272
crap 5.5069
rs 9.6111
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use InvalidArgumentException;
8
use function get_class;
9
use function is_array;
10
use function is_object;
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 ParametrizedRuleInterface
17
 */
18
final class RulesDumper
19
{
20
    private ?FormatterInterface $formatter;
21
22 1
    public function __construct(?FormatterInterface $formatter)
23
    {
24 1
        $this->formatter = $formatter;
25
    }
26
27
    /**
28
     * Return all attribute rules as array.
29
     *
30
     * For example:
31
     *
32
     * ```php
33
     * [
34
     *    'amount' => [
35
     *        [
36
     *            'number',
37
     *            'integer' => true,
38
     *            'max' => 100,
39
     *            'notANumberMessage' => 'Value must be an integer.',
40
     *            'tooBigMessage' => 'Value must be no greater than 100.'
41
     *        ],
42
     *        ['callback'],
43
     *    ],
44
     *    'name' => [
45
     *        [
46
     *            'hasLength',
47
     *            'max' => 20,
48
     *            'message' => 'Value must contain at most 20 characters.'
49
     *        ],
50
     *    ],
51
     * ]
52
     * ```
53
     *
54
     * @param iterable $rules
55
     *
56
     * @return array
57
     */
58 1
    public function asArray(iterable $rules): array
59
    {
60 1
        $rulesOfArray = [];
61 1
        foreach ($rules as $attribute => $rulesSet) {
62 1
            if (is_array($rulesSet)) {
63 1
                $ruleSet = new RuleSet($rulesSet);
64
            }
65 1
            if (!$ruleSet instanceof RuleSet) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ruleSet does not seem to be defined for all execution paths leading up to this point.
Loading history...
66
                throw new InvalidArgumentException(sprintf(
67
                    'Value should be an instance of %s or an array of rules, %s given.',
68
                    RuleSet::class,
69
                    is_object($ruleSet) ? get_class($ruleSet) : gettype($ruleSet)
70
                ));
71
            }
72 1
            $rulesOfArray[$attribute] = $ruleSet->withFormatter($this->formatter)->asArray();
73
        }
74 1
        return $rulesOfArray;
75
    }
76
77
    public function withFormatter(?FormatterInterface $formatter): self
78
    {
79
        $new = clone $this;
80
        $new->formatter = $formatter;
81
        return $new;
82
    }
83
}
84