Passed
Pull Request — main (#123)
by Andrey
27:30 queued 12:28
created

BaseProcessor::resolveKeys()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 1
nop 1
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\Processors;
21
22
use Helldar\Contracts\LangPublisher\Processor;
23
use Helldar\Contracts\LangPublisher\Provider;
24
use Helldar\LaravelLangPublisher\Concerns\Has;
25
use Helldar\LaravelLangPublisher\Concerns\Keyable;
26
use Helldar\LaravelLangPublisher\Concerns\Paths;
27
use Helldar\LaravelLangPublisher\Constants\Locales;
28
use Helldar\LaravelLangPublisher\Constants\Path;
29
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem;
30
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
31
use Illuminate\Support\Str;
32
33
abstract class BaseProcessor implements Processor
34
{
35
    use Has;
36
    use Keyable;
37
    use Paths;
38
39
    protected $force = false;
40
41
    protected $locales = [];
42
43
    protected $source_keys = [];
44
45
    protected $translated = [];
46
47
    public function __construct(array $locales, bool $force = false)
48
    {
49
        $this->locales = $locales;
50
        $this->force   = $force;
51
    }
52
53
    public function provider(Provider $provider): Processor
54
    {
55
        foreach ($provider->plugins() as $plugin) {
56
            if (! $plugin->has()) {
57
                continue;
58
            }
59
60
            foreach ($plugin->files() as $source => $target) {
61
                $this->collectSource($provider, $source, $target);
62
                $this->collectLocales($provider, $source, $target);
63
            }
64
        }
65
66
        return $this;
67
    }
68
69
    public function source(): array
70
    {
71
        return $this->source_keys;
72
    }
73
74
    public function translated(): array
75
    {
76
        return $this->translated;
77
    }
78
79
    protected function collectSource(Provider $provider, string $source, string $target): void
80
    {
81
        $path = $this->path($provider->basePath(), Path::SOURCE, $source);
82
83
        $filename = $this->preparePath($target, Locales::ENGLISH);
84
85
        $content = Filesystem::load($path);
86
87
        if ($this->hasJson($source)) {
88
            $content = $this->resolveKeys($content);
89
        }
90
91
        $this->set($this->source_keys, $filename, $this->getKeysOnly($content));
92
        $this->set($this->translated, $filename, $content);
93
    }
94
95
    protected function collectLocales(Provider $provider, string $source, string $target): void
96
    {
97
        foreach ($this->locales as $locale) {
98
            $this->collectLocale($provider, $locale, $source, $target);
99
        }
100
    }
101
102
    protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void
103
    {
104
        $path = $this->hasJson($source)
105
            ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json')
106
            : $this->path($provider->basePath(), Path::LOCALES, $locale, $source);
107
108
        $filename = $this->preparePath($target, $locale);
109
110
        $content = Filesystem::load($path);
111
112
        $this->set($this->translated, $filename, $content);
113
    }
114
115
    protected function set(array &$array, string $key, array $values): void
116
    {
117
        $loaded = $array[$key] ?? [];
118
119
        $array[$key] = array_merge_recursive($loaded, $values);
120
    }
121
122
    protected function preparePath(string $path, string $locale): string
123
    {
124
        return Str::replace('{locale}', $locale, $path);
125
    }
126
127
    protected function resolveKeys(array $array): array
128
    {
129
        return Arrayable::of($array)
130
            ->renameKeys(static function ($key, $value) {
131
                return is_numeric($key) && is_string($value) ? $value : $key;
132
            })->get();
133
    }
134
}
135