Passed
Pull Request — master (#99)
by Def
02:10
created

Result   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 47
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 3 1
A addError() 0 3 1
A getErrors() 0 7 1
A addResult() 0 3 1
A addResultWithWrapper() 0 10 3
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