TranslatableConfig::setDefaults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Laraplus\Data;
4
5
use Exception;
6
7
class TranslatableConfig
8
{
9
    protected static $config = [
10
        'locale'      => [
11
            'current_getter'  => null,
12
            'fallback_getter' => null,
13
        ],
14
        'cache'       => [
15
            'getter' => null,
16
            'setter' => null,
17
        ],
18
        'db_settings' => [
19
            'table_suffix' => '_i18n',
20
            'locale_field' => 'locale',
21
        ],
22
        'defaults'    => [
23
            'only_translated' => false,
24
            'with_fallback'   => true,
25
        ],
26
    ];
27
28
    public static function currentLocaleGetter(callable $current)
29
    {
30
        static::$config['locale']['current_getter'] = $current;
31
    }
32
33
    public static function fallbackLocaleGetter(callable $fallback)
34
    {
35
        static::$config['locale']['fallback_getter'] = $fallback;
36
    }
37
38
    public static function cacheGetter(callable $getter)
39
    {
40
        static::$config['cache']['getter'] = $getter;
41
    }
42
43
    public static function cacheSetter(callable $setter)
44
    {
45
        static::$config['cache']['setter'] = $setter;
46
    }
47
48
    public static function setDbSettings(array $settings)
49
    {
50
        static::$config['db_settings'] = array_merge(static::$config['db_settings'], $settings);
51
    }
52
53
    public static function setDefaults(array $defaults)
54
    {
55
        static::$config['defaults'] = array_merge(static::$config['defaults'], $defaults);
56
    }
57
58
    public static function currentLocale()
59
    {
60
        static::checkIfSet('locale', 'current_getter');
61
62
        return call_user_func(static::$config['locale']['current_getter']);
63
    }
64
65
    public static function fallbackLocale()
66
    {
67
        static::checkIfSet('locale', 'fallback_getter');
68
69
        return call_user_func(static::$config['locale']['fallback_getter']);
70
    }
71
72
    public static function onlyTranslated()
73
    {
74
        return static::$config['defaults']['only_translated'];
75
    }
76
77
    public static function withFallback()
78
    {
79
        return static::$config['defaults']['with_fallback'];
80
    }
81
82
    public static function dbSuffix()
83
    {
84
        return static::$config['db_settings']['table_suffix'];
85
    }
86
87
    public static function dbKey()
88
    {
89
        return static::$config['db_settings']['locale_field'];
90
    }
91
92
    public static function cacheSet($table, array $fields)
93
    {
94
        static::checkIfSet('cache', 'setter');
95
96
        return call_user_func_array(static::$config['cache']['setter'], [$table, $fields]);
97
    }
98
99
    public static function cacheGet($table)
100
    {
101
        static::checkIfSet('cache', 'getter');
102
103
        return call_user_func_array(static::$config['cache']['getter'], [$table]);
104
    }
105
106
    protected static function checkIfSet($key1, $key2)
107
    {
108
        if (empty(static::$config[$key1][$key2])) {
109
            throw new Exception("Translatable is not configured correctly. Config for [$key1.$key2] is missing.");
110
        }
111
    }
112
}
113