Passed
Push — main ( 86fec7...422411 )
by Breno
01:44
created

Translator.php$0 ➔ getDefault()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
cc 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Translation;
5
6
final class Translator
7
{
8
    private static TranslatorInterface $default;
9
10
    public static function setDefault(TranslatorInterface|callable $translator, bool $compose = true): void
11
    {
12
        if ($translator instanceof TranslatorInterface) {
13
            self::$default = $translator;
14
        } else {
15
            self::$default = new CallableTranslator($translator);
16
        }
17
18
        if ($compose) {
19
            self::$default = new CompositeTranslator(self::$default, self::createDefault());
20
        }
21
    }
22
23
    public static function getDefault(): TranslatorInterface
24
    {
25
        return self::$default ?? self::$default = self::createDefault();
26
    }
27
28
    private static function createDefault(): TranslatorInterface
29
    {
30
        return new NaiveTranslator;
31
    }
32
33
    public static function translate(string $message, ...$args): string
34
    {
35
        return self::getDefault()->translate($message, ...$args) ?? $message;
36
    }
37
38
    private function __construct()
39
    {
40
    }
41
}
42