1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "andrey-helldar/laravel-lang-publisher" project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @author Andrey Helldar <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @copyright 2021 Andrey Helldar |
12
|
|
|
* |
13
|
|
|
* @license MIT |
14
|
|
|
* |
15
|
|
|
* @see https://github.com/andrey-helldar/laravel-lang-publisher |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Helldar\LaravelLangPublisher\Concerns; |
21
|
|
|
|
22
|
|
|
use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; |
23
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
24
|
|
|
use Helldar\Support\Facades\Helpers\Str; |
25
|
|
|
|
26
|
|
|
trait Ask |
27
|
|
|
{ |
28
|
|
|
protected function getLocales(): array |
29
|
|
|
{ |
30
|
|
|
if ($locales = $this->argument('locales')) { |
|
|
|
|
31
|
|
|
return $this->resolveSelectedLocales($locales); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->askLocales($this->getMethod()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function askLocales(string $method): array |
38
|
|
|
{ |
39
|
|
|
$locales = $this->confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales($method); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return $this->resolveSelectedLocales($locales); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getAllLocales(): array |
45
|
|
|
{ |
46
|
|
|
return Locales::available(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function selectLocales(string $method) |
50
|
|
|
{ |
51
|
|
|
return $this->choice("Select localizations to $method (specify the necessary localizations separated by commas):", $this->getAllLocales(), null, null, true); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function resolveSelectedLocales($locales): array |
55
|
|
|
{ |
56
|
|
|
if ($locales === '*') { |
57
|
|
|
return $this->getAllLocales(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$locales = Arr::wrap($locales); |
61
|
|
|
|
62
|
|
|
return $this->validatedLocales($locales); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getMethod(): string |
66
|
|
|
{ |
67
|
|
|
$name = class_basename(static::class); |
68
|
|
|
|
69
|
|
|
return Str::lower($name); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function validatedLocales(array $locales): array |
73
|
|
|
{ |
74
|
|
|
foreach ($locales as $locale) { |
75
|
|
|
Locales::validate($locale); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $locales; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|