Passed
Pull Request — master (#99)
by Def
03:05 queued 46s
created

Result::addResultWithWrapper()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 122
    public function addError(ErrorMessage $error): void
20
    {
21 122
        $this->errors[] = $error;
22 122
    }
23
24 17
    public function addResult(self $result): void
25
    {
26 17
        $this->errors = array_merge($this->errors, $result->errors);
27 17
    }
28
29 2
    public function addResultWithWrapper(self $result, string $message = '', array $params = []): void
30
    {
31 2
        foreach ($result->errors as $error) {
32 2
            if (!empty($message)) {
33 2
                $error = new ErrorMessage(
34 2
                    $message,
35 2
                    array_merge(['error' => $error], $params)
36
                );
37
            }
38 2
            $this->errors[] = $error;
39
        }
40 2
    }
41
42
    /**
43
     * @param ErrorMessageFormatterInterface|null $formatter
44
     *
45
     * @return string[]
46
     */
47 31
    public function getErrors(?ErrorMessageFormatterInterface $formatter = null): array
48
    {
49 31
        return array_map(
50 31
            function ($error) use ($formatter) {
51 28
                return $error->getFormattedMessage($formatter);
52 31
            },
53 31
            $this->errors
54
        );
55
    }
56
}
57