ArrayAttributeTranslator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 16
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A translate() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\AttributeTranslator;
6
7
use Yiisoft\Validator\AttributeTranslatorInterface;
8
9
/**
10
 * An attribute translator that uses array of translations.
11
 */
12
final class ArrayAttributeTranslator implements AttributeTranslatorInterface
13
{
14
    /**
15
     * @param array $translations Translations array where each key is an attribute name and the corresponding value is
16
     * a translation.
17
     *
18
     * @psalm-param array<string,string> $translations
19
     */
20
    public function __construct(
21
        private array $translations,
22
    ) {
23
    }
24
25
    public function translate(string $attribute): string
26
    {
27
        return $this->translations[$attribute] ?? $attribute;
28
    }
29
}
30