|
2 | 2 |
|
3 | 3 | namespace SEOService2020\Morphy; |
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
| 6 | + |
| 7 | +use Illuminate\Support\Facades\Storage; |
| 8 | + |
5 | 9 |
|
6 | 10 | class Factory |
7 | 11 | { |
8 | 12 | public static function makeMorphy(array $config, ?array $commonOptions = null): Morphy |
9 | 13 | { |
10 | | - return new Morphy( |
11 | | - $config['language'], |
12 | | - ($config['options'] ?? null) === null ? |
13 | | - null : |
14 | | - array_replace_recursive($commonOptions ?? [], $config['options'] ?? []), |
15 | | - $config['dicts_path'] ?? null |
16 | | - ); |
| 14 | + if (!array_key_exists('language', $config)) { |
| 15 | + throw new InvalidArgumentException( |
| 16 | + "language must be specified for morphy {$config['name']}" |
| 17 | + ); |
| 18 | + } |
| 19 | + |
| 20 | + if (!array_key_exists('dicts_storage', $config)) { |
| 21 | + throw new InvalidArgumentException( |
| 22 | + "dicts_storage must be specified for morphy {$config['name']}" |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + $dictsStorage = $config['dicts_storage']; |
| 27 | + if (!in_array($dictsStorage, ['storage', 'filesystem'])) { |
| 28 | + throw new InvalidArgumentException( |
| 29 | + "Wrong dicts_storage specified for morphy {$config['name']}: $dictsStorage" |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + $options = $config['options'] ?? null; |
| 34 | + $options = $options === null ?: |
| 35 | + array_replace_recursive($commonOptions ?? [], $options); |
| 36 | + |
| 37 | + $dictsPath = $config['dicts_path'] ?? null; |
| 38 | + $dictsPath = ($dictsPath === null || $dictsStorage !== 'storage') ?: |
| 39 | + Storage::path($dictsPath); |
| 40 | + |
| 41 | + return new Morphy($config['language'], $options, $dictsPath); |
17 | 42 | } |
18 | 43 |
|
19 | 44 | public static function fromArray(array $configs, ?array $commonOptions = null): array |
|
0 commit comments