Issues (60)

src/Localisation.php (3 issues)

1
<?php
2
3
/**
4
 * PWWEB\Localisation.
5
 *
6
 * Localisation Master Class.
7
 *
8
 * @author    Frank Pillukeit <[email protected]>
9
 * @copyright 2020 pw-websolutions.com
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
11
 */
12
13
namespace PWWEB\Localisation;
14
15
use Illuminate\Contracts\Support\Renderable;
16
use Illuminate\Database\Eloquent\Collection;
17
use Illuminate\Support\Str;
18
use PWWEB\Localisation\Contracts\Language as LanguageContract;
19
use PWWEB\Localisation\Models\Language;
20
use PWWEB\Localisation\Repositories\LanguageRepository;
21
22
class Localisation
23
{
24
    /**
25
     * The Laravel application instance.
26
     *
27
     * @var \Illuminate\Foundation\Application
28
     */
29
    protected $app;
30
31
    /**
32
     * Normalized Laravel Version.
33
     *
34
     * @var string
35
     */
36
    protected $version;
37
38
    /**
39
     * True when this is a Lumen application.
40
     *
41
     * @var bool
42
     */
43
    protected $lumen = false;
44
45
    /**
46
     * Repository of languages to be used throughout the controller.
47
     *
48
     * @var \PWWEB\Localisation\Repositories\LanguageRepository
49
     */
50
    private $languageRepository;
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param \PWWEB\Localisation\Repositories\LanguageRepository $languageRepo Repository of Languages
56
     * @param \Illuminate\Foundation\Application                  $app          laravel application for further use
57
     */
58
    public function __construct(LanguageRepository $languageRepo, $app = null)
59
    {
60
        if (null === $app) {
61
            $app = app();
62
            //Fallback when $app is not given
63
        }
64
65
        $this->app = $app;
66
        $this->version = $app->version();
0 ignored issues
show
The method version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        /** @scrutinizer ignore-call */ 
67
        $this->version = $app->version();
Loading history...
67
        $this->lumen = Str::contains($this->version, 'Lumen');
68
69
        $this->languageRepository = $languageRepo;
70
    }
71
72
    /**
73
     * Retrieves all active languages.
74
     *
75
     * @return \Illuminate\Database\Eloquent\Collection a collection of active languages
76
     */
77
    public function languages(): Collection
78
    {
79
        return $this->languageRepository->getAllActive();
80
    }
81
82
    /**
83
     * Determines the currently selected locale.
84
     *
85
     * @param string $locale (Optional) Locale to be used for language retrieval
86
     *
87
     * @return \PWWEB\Localisation\Contracts\Language A language object
88
     */
89
    public function currentLanguage(string $locale = ''): LanguageContract
90
    {
91
        $fallbackLocale = config('app.fallback_locale');
92
93
        if ('' === $locale) {
94
            $locale = app()->getLocale();
0 ignored issues
show
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
            $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
95
        } elseif ($locale === $fallbackLocale) {
96
            $locale = 'en-GB';
97
        } else {
98
            $locale = $fallbackLocale.'-'.strtoupper($fallbackLocale);
99
        }
100
101
        try {
102
            $current = $this->languageRepository->findByLocale($locale);
103
        } catch (\InvalidArgumentException $e) {
104
            $current = $this->currentLanguage($fallbackLocale);
105
        }
106
107
        return $current;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $current could return the type Illuminate\Database\Eloquent\Model which is incompatible with the type-hinted return PWWEB\Localisation\Contracts\Language. Consider adding an additional type-check to rule them out.
Loading history...
108
    }
109
110
    /**
111
     * Renders a language selector / switcher according to view file.
112
     *
113
     * @return \Illuminate\Contracts\Support\Renderable language selector / switcher markup
114
     */
115
    public function languageSelector(): Renderable
116
    {
117
        $languages = $this->languages();
118
        $current = $this->currentLanguage();
119
120
        return view('localisation::atoms.languageselector', compact('languages', 'current'));
121
    }
122
}
123