TranslatableTrait::getTranslation()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 6
nop 2
crap 42
1
<?php
2
3
namespace Majora\Framework\Model;
4
5
/**
6
 * Trait for translatable objects
7
 */
8
trait TranslatableTrait
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $locale;
14
15
    /**
16
     * @var string
17
     */
18
    protected $defaultLocale;
19
20
    /**
21
     * @see TranslatableInterface::setLocale()
22
     */
23
    public function setLocale($locale)
24
    {
25
        $this->locale = $locale;
26
27
        return $this;
28
    }
29
30
    /**
31
     * @see TranslatableInterface::setDefaultLocale()
32
     */
33
    public function setDefaultLocale($defaultLocale)
34
    {
35
        $this->defaultLocale = $defaultLocale;
36
37
        return $this;
38
    }
39
40
    /**
41
     * return translation matching current locale from given translation data
42
     *
43
     * @param array $translationData
44
     * @param string $default
45
     *
46
     * @return string
47
     */
48
    protected function getTranslation(array $translationData, $default = '')
49
    {
50
        // undefined locale
51
        if (empty($translationData)) {
52
            return $default;
53
        }
54
55
        // current locale matched
56
        if ($this->locale && isset($translationData[$this->locale])) {
57
            return $translationData[$this->locale];
58
        }
59
60
        // default locale matched
61
        return $this->defaultLocale && isset($translationData[$this->defaultLocale]) ?
62
            $translationData[$this->defaultLocale] :
63
            $default
64
        ;
65
    }
66
}
67