TranslateMethods   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
rs 10
ccs 5
cts 5
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 5 1
A translatePlural() 0 5 1
A checkTranslator() 0 10 2
1
<?php
2
3
/**
4
 * This file is part of slick/i18n package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\I18n;
11
12
use Slick\I18n\Exception\TranslatorNotSetException;
13
14
/**
15
 * Trait Translate Methods
16
 *
17
 * @package Slick\I18n
18
 */
19
trait TranslateMethods
20
{
21
22
    /**
23
     * @var TranslatorInterface
24
     */
25
    protected $translator;
26
27
    /**
28
     * Translate the provided message
29
     *
30 2
     * @param string $message
31
     *
32 2
     * @return string The translated message
33
     */
34
    public function translate($message)
35
    {
36
        $this->checkTranslator();
37
        return $this->translator->translate($message);
38
    }
39
40
    /**
41
     * Translates the provided message to singular or plural according to the number
42
     *
43
     * @param string  $singular
44
     * @param string  $plural
45
     * @param integer $number
46 2
     *
47
     * @return string
48
     */
49 2
    public function translatePlural($singular, $plural, $number)
50 2
    {
51
        $this->checkTranslator();
52
        return $this->translator->translatePlural($singular, $plural, $number);
53
    }
54
55
    /**
56
     * Checks if translate property is set
57
     */
58
    private function checkTranslator()
59
    {
60
        if (! $this->translator instanceof TranslatorInterface) {
61
            $name = __CLASS__;
62
            throw new TranslatorNotSetException(
63
                "The translator property was not set. Check that you have set the translator ".
64
                "property in {$name} class before using translate methods."
65
            );
66
        }
67
    }
68
}