Completed
Push — master ( 3f99c9...f99ebc )
by Quentin
8s
created

TranslatableTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 59
ccs 0
cts 23
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setLocale() 0 6 1
A setDefaultLocale() 0 6 1
B getTranslation() 0 18 6
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