Passed
Push — 10.x ( e98e33...d7c7ca )
by Andrey
14:24
created

BaseProcessor::collectLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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