Passed
Push — master ( 20cfdb...2eb03a )
by Samuel
02:32
created

ValidationException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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