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

ErrorMessage::format()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 14
ccs 8
cts 11
cp 0.7272
crap 5.5069
rs 9.6111
c 1
b 0
f 0
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