Completed
Push — master ( 197f46...f51c73 )
by Scott
02:54
created

LangJsonable::getLangJson()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.439
cc 5
eloc 15
nc 6
nop 1
1
<?php namespace Bedard\Shop\Traits;
2
3
use Lang;
4
5
trait LangJsonable
6
{
7
    /**
8
     * Get a set of language strings and convert them to JSON.
9
     *
10
     * @param  array    $keys
11
     * @return string
12
     */
13
    public function getLangJson($keys)
14
    {
15
        $lang = [];
16
17
        foreach ($keys as $key => $value) {
18
            $isFiltered = gettype($value) === 'array';
19
20
            // 'bedard.shop::lang.foo.bar'
21
            if (is_int($key)) {
22
                $lang[$value] = Lang::get($value);
23
            }
24
25
            // 'form@backend::lang.form' => ['cancel', 'save', 'saving']
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
            else {
27
                if (! $isFiltered) {
28
                    $key = $value;
29
                }
30
31
                $alias = explode('@', $key);
32
                $languageString = $alias[count($alias) - 1];
33
34
                $lang[$alias[0]] = $isFiltered
35
                    ? array_intersect_key(Lang::get($languageString), array_flip($value))
36
                    : Lang::get($languageString);
37
            }
38
        }
39
40
        return json_encode($lang);
41
    }
42
}
43