Passed
Push — master ( f2e509...7ccfbc )
by Samuel
25:46 queued 10:19
created

transformCakephpValidationErrorsToOutputFormat()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace App\Module\Validation\Exception;
4
5
use RuntimeException;
6
7
class ValidationException extends RuntimeException
8
{
9
    /** @var array<string, array<int, string>> */
10
    public readonly array $validationErrors;
11
12
    /**
13
     * @param array<string, array<string, string>> $validationErrors
14
     * @param string $message
15
     */
16 8
    public function __construct(array $validationErrors, string $message = 'Validation error')
17
    {
18 8
        parent::__construct($message);
19
20 8
        $this->validationErrors = $this->transformCakephpValidationErrorsToOutputFormat($validationErrors);
0 ignored issues
show
Bug introduced by
The property validationErrors is declared read-only in App\Module\Validation\Ex...ion\ValidationException.
Loading history...
21
    }
22
23
    /**
24
     * Transform the validation error output from the library to array that is used by the frontend.
25
     * Changes are small, but the main purpose is to add an abstraction layer in case the validation
26
     * library changes its error output format in the future so that only this function has to be
27
     * changed instead of the frontend.
28
     *
29
     * In cakephp/validation 5 the error array is output in the following format:
30
     * $cakeValidationErrors = [
31
     *    'field_name' => [
32
     *        'validation_rule_name' => 'Validation error message for that field',
33
     *        'other_validation_rule_name' => 'Another validation error message for that field',
34
     *    ],
35
     *    'email' => [
36
     *        '_required' => 'This field is required',
37
     *    ],
38
     *    'first_name' => [
39
     *        'minLength' => 'Minimum length is 3',
40
     *    ],
41
     * ]
42
     *
43
     * This function transforms this into the format that is used by the frontend
44
     * (which is roughly the same except we don't need the infringed rule name as key):
45
     * $outputValidationErrors = [
46
     *    'field_name' => [
47
     *        0 => 'Validation error message for that field',
48
     *        1 => 'Another validation error message for that field',
49
     *    ],
50
     *    'email' => [
51
     *        0 => 'This field is required',
52
     *    ],
53
     *    'first_name' => [
54
     *        0 => 'Minimum length is 3',
55
     *    ],
56
     * ]
57
     *
58
     * @param array<string, array<string, string>> $validationErrors The cakephp validation errors
59
     *
60
     * @return array<string, array<int, string>> the transformed result in the format documented above
61
     */
62 8
    private function transformCakephpValidationErrorsToOutputFormat(array $validationErrors): array
63
    {
64 8
        $validationErrorsForOutput = [];
65 8
        foreach ($validationErrors as $fieldName => $fieldErrors) {
66
            // There may be cases with multiple error messages for a single field.
67 8
            foreach ($fieldErrors as $infringedRuleName => $infringedRuleMessage) {
68
                // Output is basically the same except without the rule name as a key.
69 8
                $validationErrorsForOutput[$fieldName][] = $infringedRuleMessage;
70
            }
71
        }
72
73 8
        return $validationErrorsForOutput;
74
    }
75
}
76