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

ErrorMessage::getParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 ErrorMessage
8
{
9
    private string $message = '';
10
    private array $parameters = [];
11
12 173
    public function __construct(string $message, array $parameters = [])
13
    {
14 173
        $this->message = $message;
15 173
        $this->parameters = $parameters;
16 173
    }
17
18
    public function getMessage(): string
19
    {
20
        return $this->message;
21
    }
22
23
    public function getParameters(): string
24
    {
25
        return $this->message;
26
    }
27
28 66
    public function getFormattedMessage(): string
29
    {
30 66
        return $this->format($this->message, $this->parameters);
31
    }
32
33 66
    public function __toString(): string
34
    {
35 66
        return $this->getFormattedMessage();
36
    }
37
38 66
    private function format(string $message, array $params = []): string
39
    {
40 66
        $replacements = [];
41 66
        foreach ($params as $key => $value) {
42 36
            if (is_array($value)) {
43
                $value = 'array';
44 36
            } elseif (is_object($value)) {
45
                $value = 'object';
46 36
            } elseif (is_resource($value)) {
47
                $value = 'resource';
48
            }
49 36
            $replacements['{' . $key . '}'] = $value;
50
        }
51 66
        return strtr($message, $replacements);
52
    }
53
}
54