Completed
Pull Request — master (#33)
by ARCANEDEV
02:10
created

TransChecker::hasMissing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\LaravelLang;
2
3
use Arcanedev\LaravelLang\Contracts\TransChecker as TransCheckerInterface;
4
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerInterface;
5
use Illuminate\Support\Arr;
6
use Illuminate\Translation\Translator;
7
8
/**
9
 * Class     TransChecker
10
 *
11
 * @package  Arcanedev\LaravelLang
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class TransChecker implements TransCheckerInterface
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Config values.
23
     *
24
     * @var array
25
     */
26
    private $configs;
27
28
    /**
29
     * The translator instance.
30
     *
31
     * @var \Illuminate\Translation\Translator
32
     */
33
    private $translator;
34
35
    /**
36
     * The translator manager instance.
37
     *
38
     * @var \Arcanedev\LaravelLang\Contracts\TransManager
39
     */
40
    private $manager;
41
42
    /**
43
     * The missing translations.
44
     *
45
     * @var array
46
     */
47
    private $missing = [];
48
49
    /* -----------------------------------------------------------------
50
     |  Constructor
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Make TransChecker instance.
56
     *
57
     * @param  \Illuminate\Translation\Translator             $translator
58
     * @param  \Arcanedev\LaravelLang\Contracts\TransManager  $manager
59
     * @param  array                                          $configs
60
     */
61 12
    public function __construct(
62
        Translator $translator,
63
        TransManagerInterface $manager,
64
        array $configs
65
    ) {
66 12
        $this->translator = $translator;
67 12
        $this->manager    = $manager;
68 12
        $this->configs    = $configs;
69 12
    }
70
71
    /* -----------------------------------------------------------------
72
     |  Getters & Setter
73
     | -----------------------------------------------------------------
74
     */
75
76
    /**
77
     * Get the default locale being used.
78
     *
79
     * @return string
80
     */
81 2
    public function getDefaultLocale()
82
    {
83 2
        return $this->translator->getLocale();
84
    }
85
86
    /**
87
     * Get the locales to check.
88
     *
89
     * @return array
90
     */
91 2
    public function getLocales()
92
    {
93 2
        return Arr::get($this->configs, 'locales', []);
94
    }
95
96
    /**
97
     * Get the ignored translation attributes.
98
     *
99
     * @return array
100
     */
101 2
    public function getIgnoredTranslations()
102
    {
103 2
        return Arr::get($this->configs, 'check.ignore', []);
104
    }
105
106
    /* -----------------------------------------------------------------
107
     |  Main Methods
108
     | -----------------------------------------------------------------
109
     */
110
111
    /**
112
     * Check the missing translations.
113
     *
114
     * @return array
115
     */
116 2
    public function check()
117
    {
118 2
        $this->missing = [];
119 2
        $from          = $this->getDefaultLocale();
120 2
        $locales       = $this->getLocales();
121 2
        $ignored       = $this->getIgnoredTranslations();
122 2
        $fromTrans     = $this->getTranslations($from, $ignored);
123
124 2
        foreach ($locales as $to) {
125 2
            $toTrans = $this->getTranslations($to, $ignored);
126
127 2
            $this->diffMissing($toTrans, $fromTrans, $from);
128 2
            $this->diffMissing($fromTrans, $toTrans, $to);
129
        }
130
131 2
        return $this->missing;
132
    }
133
134
    /* -----------------------------------------------------------------
135
     |  Other Methods
136
     | -----------------------------------------------------------------
137
     */
138
139
    /**
140
     * Get locale translations from multiple groups.
141
     *
142
     * @param  string  $locale
143
     * @param  array   $ignored
144
     *
145
     * @return array
146
     */
147 2
    private function getTranslations($locale, array $ignored)
148
    {
149 2
        $appLocale    = $this->manager->getFrom('app', $locale);
150 2
        $vendorLocale = $this->manager->getFrom('vendor', $locale);
151
152 2
        return is_null($appLocale)
153 2
            ? $vendorLocale->mergeTranslations($appLocale, $ignored)
154 2
            : $appLocale->mergeTranslations($vendorLocale, $ignored);
155
    }
156
157
    /**
158
     * Diff the missing translations.
159
     *
160
     * @param  array   $toTranslations
161
     * @param  array   $fromTranslations
162
     * @param  string  $locale
163
     */
164 2
    private function diffMissing(array $toTranslations, array $fromTranslations, $locale)
165
    {
166 2
        $diff = array_diff_key($toTranslations, $fromTranslations);
167
168 2
        if (count($diff) === 0)
169
            return;
170
171 2
        foreach ($diff as $transKey => $transValue) {
172 2
            $this->addMissing($locale, $transKey);
173
        }
174 2
    }
175
176
    /**
177
     * Adding missing translation to collection.
178
     *
179
     * @param  string  $locale
180
     * @param  string  $transKey
181
     */
182 2
    private function addMissing($locale, $transKey)
183
    {
184 2
        if ( ! $this->hasMissing($locale, $transKey)) {
185 2
            $this->missing[$locale][] = $transKey;
186
        }
187 2
    }
188
189
    /**
190
     * Check if a missing translation exists in collection.
191
     *
192
     * @param  string  $locale
193
     * @param  string  $transKey
194
     *
195
     * @return bool
196
     */
197 2
    private function hasMissing($locale, $transKey)
198
    {
199 2
        if ( ! isset($this->missing[$locale]))
200 2
            return false;
201
202 2
        return in_array($transKey, $this->missing[$locale]);
203
    }
204
}
205