Completed
Push — master ( 141c37...4e2b2d )
by Scott
04:28
created

LangJsonable::getLangJson()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.0534
c 1
b 0
f 0
cc 4
eloc 12
nc 5
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
            if (! $isFiltered) {
21
                $key = $value;
22
            }
23
24
            $alias = explode('@', $key);
25
            $languageString = $alias[count($alias) - 1];
26
27
            $lang[$alias[0]] = $isFiltered
28
                ? array_intersect_key(Lang::get($languageString), array_flip($value))
29
                : Lang::get($languageString);
30
        }
31
32
        return json_encode($lang);
33
    }
34
}
35