Passed
Push — master ( b2cf4a...c48dc4 )
by Samuel
02:01
created

ValidationException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 67
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transformCakephpValidationErrorsToOutputFormat() 0 12 3
1
<?php
2
3
namespace App\Core\Domain\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 6
    public function __construct(array $validationErrors, string $message = 'Validation error')
17
    {
18 6
        parent::__construct($message);
19
20 6
        $this->validationErrors = $this->transformCakephpValidationErrorsToOutputFormat($validationErrors);
0 ignored issues
show
Bug introduced by
The property validationErrors is declared read-only in App\Core\Domain\Exception\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 6
    private function transformCakephpValidationErrorsToOutputFormat(array $validationErrors): array
63
    {
64 6
        $validationErrorsForOutput = [];
65 6
        foreach ($validationErrors as $fieldName => $fieldErrors) {
66
            // There may be cases with multiple error messages for a single field.
67 6
            foreach ($fieldErrors as $infringedRuleName => $infringedRuleMessage) {
68
                // Output is basically the same except without the rule name as a key.
69 6
                $validationErrorsForOutput[$fieldName][] = $infringedRuleMessage;
70
            }
71
        }
72
73 6
        return $validationErrorsForOutput;
74
    }
75
}
76