RequestValidationException::getFirstErrors()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 7
c 2
b 1
f 0
nc 4
nop 0
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\RequestModel;
6
7
use RuntimeException;
8
9
final class RequestValidationException extends RuntimeException
10
{
11
    private const MESSAGE = 'Request model validation error';
12
13
    /**
14
     * @param string[] $errors
15
     *
16
     * @psalm-param array<string,string[]> $errors
17
     */
18 5
    public function __construct(
19
        private array $errors
20
    ) {
21 5
        parent::__construct(self::MESSAGE);
22
    }
23
24
    /**
25
     * @psalm-return array<string,string[]>
26
     */
27 1
    public function getErrors(): array
28
    {
29 1
        return $this->errors;
30
    }
31
32
    /**
33
     * @psalm-return array<string,string>
34
     */
35 3
    public function getFirstErrors(): array
36
    {
37 3
        if (empty($this->errors)) {
38 1
            return [];
39
        }
40
41 2
        $result = [];
42 2
        foreach ($this->errors as $name => $errors) {
43 2
            if (!empty($errors)) {
44 2
                $result[$name] = reset($errors);
45
            }
46
        }
47
48 2
        return $result;
49
    }
50
51 2
    public function getFirstError(): ?string
52
    {
53 2
        $errors = $this->getFirstErrors();
54
55 2
        return $errors === [] ? null : reset($errors);
56
    }
57
}
58