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

TranslatableTrait::setLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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