Passed
Pull Request — main (#154)
by Andrey
26:32 queued 11:35
created

Base::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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