FineCascadingTranslator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) 2012-2015 Marc TEYSSIER
4
 *
5
 * See the file LICENSE.txt for copying permission.
6
 */
7
namespace Mouf\Utils\I18n\Fine\Common;
8
9
use Mouf\Utils\I18n\Fine\TranslatorInterface;
10
use Mouf\Utils\I18n\Fine\LanguageDetectionInterface;
11
12
/**
13
 * This element can cascading the language translation to search the first with a translation
14
 *
15
 * @author Marc TEYSSIER
16
 */
17
class FineCascadingTranslator implements TranslatorInterface
18
{
19
    /**
20
     * List of all translator in order of priority
21
     *
22
     * @var array<\Mouf\Utils\I18n\Fine\TranslatorInterface>
23
     */
24
    private $translators = array();
25
26
    /**
27
     *
28
     * @param array<\Mouf\Utils\I18n\Fine\TranslatorInterface> $translators
29
     */
30
    public function __construct(array $translators = array())
31
    {
32
        $this->translators = $translators;
33
    }
34
35
    /**
36
     * Check all translators to retrieve message in the order. When there is one translation, the function return the message (of the first found).
37
     * If the message doesn't exist return null
38
     *
39
     * @param $message string This is the key of translation search
40
     * @param $parameters array All parameters to customize message
41
     * @param  LanguageDetectionInterface $languageDetection Set it if you want to force the language to another value
42
     * @return string|null
43
     */
44
    public function getTranslation($message, array $parameters = array(), LanguageDetectionInterface $languageDetection = null)
45
    {
46
        foreach ($this->translators as $translator) {
47
            $tranlation = $translator->getTranslation($message, $parameters, $languageDetection);
48
            if ($tranlation !== null) {
49
                return $tranlation;
50
            }
51
        }
52
53
        return;
54
    }
55
}
56