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

ErrorMessage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 45
ccs 20
cts 23
cp 0.8696
rs 10
c 1
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 14 5
A __construct() 0 4 1
A __toString() 0 3 1
A getFormattedMessage() 0 3 1
A getParameters() 0 3 1
A getMessage() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
final class ErrorMessage
8
{
9
    private string $message = '';
10
    private array $parameters = [];
11
12 172
    public function __construct(string $message, array $parameters = [])
13
    {
14 172
        $this->message = $message;
15 172
        $this->parameters = $parameters;
16 172
    }
17
18 1
    public function getMessage(): string
19
    {
20 1
        return $this->message;
21
    }
22
23 1
    public function getParameters(): array
24
    {
25 1
        return $this->parameters;
26
    }
27
28 67
    public function getFormattedMessage(): string
29
    {
30 67
        return $this->format($this->message, $this->parameters);
31
    }
32
33 66
    public function __toString(): string
34
    {
35 66
        return $this->getFormattedMessage();
36
    }
37
38 67
    private function format(string $message, array $params = []): string
39
    {
40 67
        $replacements = [];
41 67
        foreach ($params as $key => $value) {
42 37
            if (is_array($value)) {
43
                $value = 'array';
44 37
            } elseif (is_object($value)) {
45
                $value = 'object';
46 37
            } elseif (is_resource($value)) {
47
                $value = 'resource';
48
            }
49 37
            $replacements['{' . $key . '}'] = $value;
50
        }
51 67
        return strtr($message, $replacements);
52
    }
53
}
54