Total Complexity | 7 |
Total Lines | 64 |
Duplicated Lines | 0 % |
Coverage | 58.81% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
18 | final class RulesDumper |
||
19 | { |
||
20 | private ?FormatterInterface $formatter; |
||
21 | |||
22 | 1 | public function __construct(?FormatterInterface $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) { |
|
|
|||
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 |
||
82 | } |
||
83 | } |
||
84 |