Completed
Pull Request — master (#145)
by Neomerx
03:38
created

Translator::getTranslator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php namespace Neomerx\JsonApi\I18n;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Neomerx\JsonApi\Contracts\I18n\TranslatorInterface;
20
21
/**
22
 * @package Neomerx\JsonApi
23
 */
24
class Translator implements TranslatorInterface
25
{
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    private static $translator = null;
30
31
    /**
32
     * @return TranslatorInterface
33
     */
34 34
    public static function getTranslator()
35
    {
36 34
        if (self::$translator === null) {
37 1
            self::$translator = new static;
38 1
        }
39
40 34
        return self::$translator;
41
    }
42
43
    /**
44
     * @param TranslatorInterface $translator
45
     */
46 1
    public static function setTranslator(TranslatorInterface $translator)
47
    {
48 1
        self::$translator = $translator;
49 1
    }
50
51
    /**
52
     * @param string $format
53
     * @param array  $parameters
54
     *
55
     * @return string
56
     */
57 33
    public function translate($format, array $parameters = [])
58
    {
59 33
        $result = empty($parameters) === false ? vsprintf($format, $parameters) : $format;
60
61 33
        return $result;
62
    }
63
64
    /**
65
     * @param string $format
66
     * @param array  $parameters
67
     *
68
     * @return string
69
     */
70 32
    public static function t($format, array $parameters = [])
71
    {
72 32
        return static::getTranslator()->translate($format, $parameters);
73
    }
74
}
75