Completed
Push — 4.0 ( ef8406...c59327 )
by David
12s
created

TranslatedValue   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A val() 0 7 2
1
<?php
2
namespace Mouf\Utils\I18n\Fine\Common;
3
4
use Mouf\Utils\I18n\Fine\TranslatorInterface;
5
use Mouf\Utils\Value\ValueUtils;
6
use Mouf\Utils\Value\ValueInterface;
7
8
/**
9
 * An object that represents a translated key.
10
 */
11
class TranslatedValue implements ValueInterface
12
{
13
    /**
14
     * The service that will perform the translation
15
     *
16
     * @var TranslatorInterface
17
     */
18
	private $translator;
19
20
	/**
21
     * Key to search translation
22
     * @var string|ValueInterface
23
     */
24
	private $key;
25
26
    /**
27
     * Parameters to customize the translation
28
     * @var array<string,string>|ValueInterface
29
     */
30
    private $parameters;
31
32
    /**
33
     *
34
     * @param TranslatorInterface $translator
35
     * @param string $key
36
     * @param array<string,string> $parameters
37
     * @Important
38
     */
39
	public function __construct(TranslatorInterface $translator, $key, array $parameters = []) {
40
        $this->translator = $translator;
41
        $this->key = $key;
42
        $this->parameters = $parameters;
43
    }
44
45
	/**
46
     * Returns the value represented by this object.
47
     *
48
     * @return string
49
     */
50
	public function val() {
51
        $translation = $this->translator->getTranslation(ValueUtils::val($this->key), ValueUtils::val($this->parameters));
52
        if ($translation === null) {
53
            $translation = '???'.ValueUtils::val($this->key).'???';
54
        }
55
        return $translation;
56
    }
57
}