mindtwo /
laravel-multilingual
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace mindtwo\LaravelMultilingual\Services; |
||||
| 4 | |||||
| 5 | use Exception; |
||||
| 6 | use Illuminate\Support\Str; |
||||
| 7 | use Illuminate\Support\Collection; |
||||
| 8 | use Illuminate\Config\Repository as Config; |
||||
| 9 | use mindtwo\LaravelMultilingual\Exceptions\LocaleNotAvailableException; |
||||
| 10 | use mindtwo\LaravelMultilingual\Exceptions\LocaleServiceNotAvailableException; |
||||
| 11 | |||||
| 12 | class Locale extends Collection |
||||
| 13 | { |
||||
| 14 | /** |
||||
| 15 | * Configuration. |
||||
| 16 | * |
||||
| 17 | * @var Config |
||||
| 18 | */ |
||||
| 19 | protected $config; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * Locale constructor. |
||||
| 23 | * |
||||
| 24 | * @param Config $config |
||||
| 25 | */ |
||||
| 26 | public function __construct(Config $config) |
||||
| 27 | { |
||||
| 28 | $this->config = $config; |
||||
| 29 | parent::__construct([ |
||||
| 30 | 'default' => $this->config->get('app.locale'), |
||||
| 31 | ]); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * Get the fallback locale. |
||||
| 36 | * |
||||
| 37 | * @return string |
||||
| 38 | */ |
||||
| 39 | protected function getFallbackAttribute(): string |
||||
| 40 | { |
||||
| 41 | return $this->config->get('app.fallback_locale'); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * Get the current language. |
||||
| 46 | * |
||||
| 47 | * @throws Exception |
||||
| 48 | * |
||||
| 49 | * @return string |
||||
| 50 | */ |
||||
| 51 | protected function getCurrentAttribute(): string |
||||
| 52 | { |
||||
| 53 | return $this->config->get('app.locale'); |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * Get a list of available site languages. |
||||
| 58 | * |
||||
| 59 | * @return Collection |
||||
| 60 | */ |
||||
| 61 | protected function getAvailableAttribute(): Collection |
||||
| 62 | { |
||||
| 63 | return $this->available(); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * Get a list of available site languages. |
||||
| 68 | * |
||||
| 69 | * @return Collection |
||||
| 70 | */ |
||||
| 71 | public function available(): Collection |
||||
| 72 | { |
||||
| 73 | if (! array_key_exists('available', $this->items)) { |
||||
| 74 | $locales = collect($this->config->get('laravel-multilingual.locales')); |
||||
| 75 | |||||
| 76 | $this->items['available'] = $locales->flatten()->unique(); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | return $this->items['available']; |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Get a list of available locales within a service. |
||||
| 84 | * |
||||
| 85 | * @param string $service |
||||
| 86 | * |
||||
| 87 | * @throws LocaleServiceNotAvailableException |
||||
| 88 | * |
||||
| 89 | * @return Collection |
||||
| 90 | */ |
||||
| 91 | public function availableInService(string $service): Collection |
||||
| 92 | { |
||||
| 93 | $service = collect($this->config->get('laravel-multilingual.locales'))->only($service)->flatten(); |
||||
| 94 | |||||
| 95 | if ($service->isEmpty()) { |
||||
| 96 | throw new LocaleServiceNotAvailableException(); |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | return $service->mapWithKeys(function ($locale) { |
||||
| 100 | return [$locale => $locale]; |
||||
| 101 | }); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Set current locale. |
||||
| 106 | * |
||||
| 107 | * @param string $locale |
||||
| 108 | * |
||||
| 109 | * @throws LocaleNotAvailableException |
||||
| 110 | * @throws LocaleServiceNotAvailableException |
||||
| 111 | * |
||||
| 112 | * @return Locale |
||||
| 113 | */ |
||||
| 114 | public function setCurrentLocaleAttribute(string $locale): self |
||||
| 115 | { |
||||
| 116 | if (! $this->isAvailable($locale)) { |
||||
| 117 | throw new LocaleNotAvailableException(); |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | if (app()->getLocale() !== $locale) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 121 | app()->setLocale($locale); |
||||
|
0 ignored issues
–
show
The method
setLocale() 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
Loading history...
|
|||||
| 122 | } |
||||
| 123 | |||||
| 124 | $this->items['current'] = $locale; |
||||
| 125 | |||||
| 126 | return $this; |
||||
| 127 | } |
||||
| 128 | |||||
| 129 | /** |
||||
| 130 | * Get a locale or throw an exception, if it is unavailable. |
||||
| 131 | * |
||||
| 132 | * @param string $locale |
||||
| 133 | * @param string|null $service |
||||
| 134 | * |
||||
| 135 | * @throws LocaleNotAvailableException |
||||
| 136 | * @throws LocaleServiceNotAvailableException |
||||
| 137 | * |
||||
| 138 | * @return string |
||||
| 139 | */ |
||||
| 140 | public function getOrFail(string $locale, string $service = null): string |
||||
| 141 | { |
||||
| 142 | if (! $this->isAvailable($locale, $service)) { |
||||
| 143 | throw new LocaleNotAvailableException(); |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | return $locale; |
||||
| 147 | } |
||||
| 148 | |||||
| 149 | /** |
||||
| 150 | * Determinate if a locale is available. |
||||
| 151 | * |
||||
| 152 | * @param string $locale |
||||
| 153 | * @param string|null $service |
||||
| 154 | * |
||||
| 155 | * @throws LocaleServiceNotAvailableException |
||||
| 156 | * |
||||
| 157 | * @return bool |
||||
| 158 | */ |
||||
| 159 | public function isAvailable(string $locale, string $service = null): bool |
||||
| 160 | { |
||||
| 161 | if (is_null($service)) { |
||||
| 162 | return $this->available->contains($locale); |
||||
|
0 ignored issues
–
show
The property
available does not exist on mindtwo\LaravelMultilingual\Services\Locale. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 163 | } |
||||
| 164 | |||||
| 165 | return $this->availableInService($service)->contains($locale); |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * Get the value of an attribute using its mutator. |
||||
| 170 | * |
||||
| 171 | * @param string $key |
||||
| 172 | * |
||||
| 173 | * @return mixed |
||||
| 174 | */ |
||||
| 175 | protected function mutateAttribute($key) |
||||
| 176 | { |
||||
| 177 | return $this->{'get'.Str::studly($key).'Attribute'}(); |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | /** |
||||
| 181 | * Get an element from the collection. |
||||
| 182 | * |
||||
| 183 | * @param string $key |
||||
| 184 | * |
||||
| 185 | * @throws Exception |
||||
| 186 | * |
||||
| 187 | * @return mixed |
||||
| 188 | */ |
||||
| 189 | public function __get($key) |
||||
| 190 | { |
||||
| 191 | if (method_exists($this, 'get'.Str::studly($key).'Attribute')) { |
||||
| 192 | if (array_key_exists($key, $this->items)) { |
||||
| 193 | return $this->items[$key]; |
||||
| 194 | } |
||||
| 195 | |||||
| 196 | return $this->items[$key] = $this->mutateAttribute($key); |
||||
| 197 | } |
||||
| 198 | |||||
| 199 | if ($this->has($key)) { |
||||
| 200 | return $this->items[$key]; |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | return parent::__get($key); |
||||
| 204 | } |
||||
| 205 | } |
||||
| 206 |