Issues (68)

src/Helpers/FluentHelper.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace LeKoala\EmailTemplates\Helpers;
4
5
use SilverStripe\i18n\i18n;
6
use TractorCow\Fluent\Model\Locale;
0 ignored issues
show
The type TractorCow\Fluent\Model\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use TractorCow\Fluent\State\FluentState;
0 ignored issues
show
The type TractorCow\Fluent\State\FluentState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use TractorCow\Fluent\Extension\FluentExtension;
0 ignored issues
show
The type TractorCow\Fluent\Extension\FluentExtension was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Helps providing base functionnalities where including
12
 * subsite module is optional and yet provide a consistent api
13
 *
14
 * TODO: externalize this to a specific module
15
 */
16
class FluentHelper
17
{
18
    /**
19
     * @var boolean
20
     */
21
    protected static $previousState;
22
23
    /**
24
     * @var int
25
     */
26
    protected static $previousSubsite;
27
28
    /**
29
     * @var array
30
     */
31
    protected static $locale_cache = [];
32
33
    /**
34
     * Do we have the subsite module installed
35
     * TODO: check if it might be better to use module manifest instead?
36
     *
37
     * @return bool
38
     */
39
    public static function usesFluent()
40
    {
41
        return class_exists(FluentState::class);
42
    }
43
44
    /**
45
     * @param string $class
46
     * @return boolean
47
     */
48
    public static function isClassTranslated($class)
49
    {
50
        if (!class_exists(FluentExtension::class)) {
51
            return false;
52
        }
53
        $singl = singleton($class);
54
        return $singl->hasExtension(FluentExtension::class);
55
    }
56
57
    /**
58
     * Execute the callback in given subsite
59
     *
60
     * @param string|Locale $locale
61
     * @param callable $cb
62
     * @return mixed callback result
63
     */
64
    public static function withLocale($locale, $cb)
65
    {
66
        if (!self::usesFluent() || !$locale || !class_exists(FluentState::class)) {
67
            $cb();
68
            return;
69
        }
70
        $state = FluentState::singleton();
71
        return $state->withState(function ($state) use ($locale, $cb) {
72
            if (is_object($locale)) {
73
                $locale = $locale->Locale;
74
            }
75
            $state->setLocale($locale);
76
            return $cb();
77
        });
78
    }
79
80
    /**
81
     * Execute the callback for all locales
82
     *
83
     * @param callable $cb
84
     * @return array an array of callback results
85
     */
86
    public static function withLocales($cb)
87
    {
88
        if (!self::usesFluent() || !class_exists(Locale::class)) {
89
            $cb();
90
            return [];
91
        }
92
        $allLocales = Locale::get();
93
        $results = [];
94
        foreach ($allLocales as $locale) {
95
            $results[] = self::withLocale($locale, $cb);
96
        }
97
        return $results;
98
    }
99
100
    /**
101
     * Get a locale from the lang
102
     *
103
     * @param string $lang
104
     * @return string
105
     */
106
    public static function get_locale_from_lang($lang)
107
    {
108
        // Normalize if needed
109
        if (strlen($lang) > 2) {
110
            $lang = self::get_lang($lang);
111
        }
112
113
        // Use fluent data
114
        if (class_exists(Locale::class)) {
115
            if (empty(self::$locale_cache)) {
116
                $fluentLocales = Locale::getLocales();
117
                foreach ($fluentLocales as $locale) {
118
                    self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale;
119
                }
120
            }
121
            if (isset(self::$locale_cache[$lang])) {
122
                return self::$locale_cache[$lang];
123
            }
124
        }
125
        // Guess
126
        $localesData = i18n::getData();
127
        return $localesData->localeFromLang($lang);
128
    }
129
130
    /**
131
     * Get the right locale (using fluent data if exists)
132
     *
133
     * @return string
134
     */
135
    public static function get_locale()
136
    {
137
        if (class_exists(FluentState::class)) {
138
            return FluentState::singleton()->getLocale();
139
        }
140
        return i18n::get_locale();
141
    }
142
143
    /**
144
     * Make sure we get a proper two characters lang
145
     *
146
     * @param string|object $lang a string or a fluent locale object
147
     * @return string a two chars lang
148
     */
149
    public static function get_lang($lang = null)
150
    {
151
        if (!$lang) {
152
            $lang = self::get_locale();
153
        }
154
        if (is_object($lang)) {
155
            $lang = $lang->Locale;
156
        }
157
        return substr($lang, 0, 2);
158
    }
159
}
160