Passed
Push — main ( f4d82a...ca2db5 )
by Andrey
03:23 queued 12s
created

Base::sortAndMerge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Comparators;
21
22
use Helldar\Contracts\LangPublisher\Comparator;
23
use Helldar\LaravelLangPublisher\Concerns\Has;
24
use Helldar\LaravelLangPublisher\Concerns\Paths;
25
use Helldar\LaravelLangPublisher\Facades\Helpers\Config;
26
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem;
27
use Helldar\Support\Facades\Helpers\Arr;
28
29
abstract class Base implements Comparator
30
{
31
    use Has;
32
    use Paths;
33
34
    protected $full;
35
36
    protected $keys = [];
37
38
    protected $translations = [];
39
40
    protected $result = [];
41
42
    protected $exclude = ['attributes', 'custom'];
43
44 62
    public function __construct(array $keys, array $translations, bool $full = false)
45
    {
46 62
        $this->keys = $keys;
47
48 62
        $this->translations = $translations;
49
50 62
        $this->full = $full;
51 62
    }
52
53 62
    public function get(): array
54
    {
55 62
        foreach ($this->filenames() as $filename) {
56 62
            foreach ($this->locales($filename) as $locale) {
57 62
                $result = $this->compare($filename, $locale);
58
59 62
                $path = $this->resolvePath($filename, $locale);
60
61 62
                $this->putResult($path, $result);
62
            }
63
        }
64
65 62
        return $this->getResult();
66
    }
67
68
    abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array;
69
70 62
    protected function compare(string $filename, string $locale): array
71
    {
72 62
        $local      = $this->resource($filename, $locale);
73 62
        $translated = $this->translated($filename, $locale);
74
75 62
        return $this->merge(
76 62
            $this->extract($filename, $local),
77 62
            $this->extract($filename, $translated),
78 62
            $this->excludes($filename, $local),
79 62
            $this->extra($filename, $local),
80 62
            $this->extra($filename, $translated),
81
        );
82
    }
83
84 62
    protected function resource(string $filename, string $locale): array
85
    {
86 62
        $filename = $this->resolvePath($filename, $locale);
87
88 62
        $path = $this->resourcesPath($filename);
89
90 62
        return Filesystem::load($path);
91
    }
92
93 62
    protected function translated(string $filename, string $locale): array
94
    {
95 62
        $values = $this->translations[$filename][$locale];
96
97 62
        $keys = $this->keys[$filename];
98
99 62
        return Arr::only($values, $keys);
100
    }
101
102 62
    protected function excludes(string $filename, array $user): array
103
    {
104 62
        $excludes = Config::excludes();
105
106 62
        $key = $this->filename($filename);
107
108 62
        $values = Arr::get($excludes, $key, []);
109
110 62
        return Arr::only($user, $values);
111
    }
112
113 62
    protected function extract(string $filename, array $array): array
114
    {
115 62
        if ($this->hasValidation($filename)) {
116 62
            return Arr::except($array, $this->exclude);
117
        }
118
119 62
        return $array;
120
    }
121
122 62
    protected function extra(string $filename, array $array): array
123
    {
124 62
        if ($this->hasValidation($filename)) {
125 62
            return Arr::only($array, $this->exclude);
126
        }
127
128 62
        return [];
129
    }
130
131 62
    protected function putResult(string $filename, array $array): void
132
    {
133 62
        $this->result[$filename] = $array;
134 62
    }
135
136 62
    protected function getResult(): array
137
    {
138 62
        return $this->result;
139
    }
140
141 62
    protected function filenames(): array
142
    {
143 62
        return array_keys($this->keys);
144
    }
145
146 62
    protected function locales(string $filename): array
147
    {
148 62
        return array_keys($this->translations[$filename]);
149
    }
150
151 62
    protected function sortAndMerge(array ...$arrays): array
152
    {
153 62
        $array = Arr::merge(...$arrays);
154
155 62
        return $this->sort($array);
156
    }
157
158 62
    protected function sort(array $array): array
159
    {
160 62
        return Arr::ksort($array);
161
    }
162
}
163