Completed
Push — master ( a624e1...736705 )
by Nassif
12:28
created

Util::keyValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace Nikaia\TranslationSheet;
4
5
use Illuminate\Support\Collection;
6
7
class Util
8
{
9 1
    public static function varExport($var, $indent = '')
10
    {
11 1
        switch (gettype($var)) {
12 1
            case 'string':
13 1
                return '"'.addcslashes($var, "\\\$\"\r\n\t\v\f").'"';
14 1
            case 'array':
15 1
                $indexed = array_keys($var) === range(0, count($var) - 1);
16 1
                $r = [];
17 1
                foreach ($var as $key => $value) {
18 1
                    $r[] = "$indent    "
19 1
                        .($indexed ? '' : self::varExport($key).' => ')
20 1
                        .self::varExport($value, "$indent    ");
21
                }
22
23 1
                return "[\n".implode(",\n", $r)."\n".$indent.']';
24
            case 'boolean':
25
                return $var ? 'true' : 'false';
26
            default:
27
                return var_export($var, true);
28
        }
29
    }
30
31 1
    public static function keyValues($values, $keys)
32
    {
33 1
        $values = $values instanceof Collection ? $values : new Collection($values);
34
35
        return $values->map(function ($values) use ($keys) {
36
            return array_combine($keys, $values);
37 1
        });
38
    }
39
40 3
    public static function asArray($value)
41
    {
42 3
        if (is_array($value)) {
43 2
            return $value;
44
        }
45
46
        return array_filter(array_map(function ($item) {
47 1
            return trim($item);
48 1
        }, explode(',', $value)));
49
    }
50
}
51