Passed
Pull Request — master (#99)
by Def
01:58
created

Result::addResultWithErrorMessageWrapper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
final class Result
8
{
9
    /**
10
     * @var ErrorMessage[]
11
     */
12
    private array $errors = [];
13
14 140
    public function isValid(): bool
15
    {
16 140
        return $this->errors === [];
17
    }
18
19 123
    public function addError(ErrorMessage $error): void
20
    {
21 123
        $this->errors[] = $error;
22 123
    }
23
24 18
    public function addResult(self $result): void
25
    {
26 18
        $this->errors = array_merge($this->errors, $result->errors);
27 18
    }
28
29 2
    public function addResultWithErrorMessageWrapper(self $result, ErrorMessage $errorMessage): void
30
    {
31 2
        foreach ($result->errors as $error) {
32 2
            $this->errors[] = new ErrorMessage(
33 2
                $errorMessage->getMessage(),
34 2
                array_merge(['error' => $error], $errorMessage->getParameters())
35
            );
36
        }
37 2
    }
38
39
    /**
40
     * @param ErrorMessageFormatterInterface|null $formatter
41
     *
42
     * @return string[]
43
     */
44 32
    public function getErrors(?ErrorMessageFormatterInterface $formatter = null): array
45
    {
46 32
        return array_map(
47 32
            static function ($error) use ($formatter) {
48 29
                return $error->getFormattedMessage($formatter);
49 32
            },
50 32
            $this->errors
51
        );
52
    }
53
}
54