Passed
Pull Request — main (#123)
by Andrey
42:12 queued 26:54
created

Base   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 45
c 4
b 2
f 0
dl 0
loc 120
rs 10
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 3 1
A compare() 0 11 1
A putResult() 0 3 1
A translated() 0 7 1
A filenames() 0 3 1
A extra() 0 7 2
A __construct() 0 7 1
A excludes() 0 9 1
A locales() 0 3 1
A extract() 0 7 2
A resource() 0 7 1
A get() 0 13 3
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
    public function __construct(array $keys, array $translations, bool $full = false)
45
    {
46
        $this->keys = $keys;
47
48
        $this->translations = $translations;
49
50
        $this->full = $full;
51
    }
52
53
    public function get(): array
54
    {
55
        foreach ($this->filenames() as $filename) {
56
            foreach ($this->locales($filename) as $locale) {
57
                $result = $this->compare($filename, $locale);
58
59
                $path = $this->resolvePath($filename, $locale);
60
61
                $this->putResult($path, $result);
62
            }
63
        }
64
65
        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
    protected function compare(string $filename, string $locale): array
71
    {
72
        $local      = $this->resource($filename, $locale);
73
        $translated = $this->translated($filename, $locale);
74
75
        return $this->merge(
76
            $this->extract($filename, $local),
77
            $this->extract($filename, $translated),
78
            $this->excludes($filename, $local),
79
            $this->extra($filename, $local),
80
            $this->extra($filename, $translated),
81
        );
82
    }
83
84
    protected function resource(string $filename, string $locale): array
85
    {
86
        $filename = $this->resolvePath($filename, $locale);
87
88
        $path = $this->resourcesPath($filename);
89
90
        return Filesystem::load($path);
91
    }
92
93
    protected function translated(string $filename, string $locale): array
94
    {
95
        $values = $this->translations[$filename][$locale];
96
97
        $keys = $this->keys[$filename];
98
99
        return Arr::only($values, $keys);
100
    }
101
102
    protected function excludes(string $filename, array $user): array
103
    {
104
        $excludes = Config::excludes();
105
106
        $key = $this->filename($filename);
107
108
        $values = Arr::get($excludes, $key, []);
109
110
        return Arr::only($user, $values);
111
    }
112
113
    protected function extract(string $filename, array $array): array
114
    {
115
        if ($this->hasValidation($filename)) {
116
            return Arr::except($array, $this->exclude);
117
        }
118
119
        return $array;
120
    }
121
122
    protected function extra(string $filename, array $array): array
123
    {
124
        if ($this->hasValidation($filename)) {
125
            return Arr::only($array, $this->exclude);
126
        }
127
128
        return [];
129
    }
130
131
    protected function putResult(string $filename, array $array): void
132
    {
133
        $this->result[$filename] = $array;
134
    }
135
136
    protected function getResult(): array
137
    {
138
        return $this->result;
139
    }
140
141
    protected function filenames(): array
142
    {
143
        return array_keys($this->keys);
144
    }
145
146
    protected function locales(string $filename): array
147
    {
148
        return array_keys($this->translations[$filename]);
149
    }
150
}
151