Completed
Push — master ( 2093be...99e81f )
by Ben
07:44
created

LaravelTranslationsReader::prependGroupkey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 4.0312
1
<?php
2
3
namespace Thinktomorrow\Squanto\Services;
4
5
use Illuminate\Support\Arr;
6
use League\Flysystem\Filesystem;
7
8
class LaravelTranslationsReader
9
{
10
    /**
11
     * @var \Illuminate\Support\Collection
12
     */
13
    private $translations;
14
15
    /**
16
     * Local filesystem. Already contains the path to our translation files
17
     * e.g. storage/app/trans
18
     *
19
     * @var Filesystem
20
     */
21
    private $filesystem;
22
    private $path;
23
24 24
    public function __construct(Filesystem $filesystem)
25
    {
26 24
        $this->filesystem = $filesystem;
27 24
        $this->path = $this->getSquantoLangPath();
28 24
        $this->translations = collect();
29 24
    }
30
31
    /**
32
     * Read the lang files for a specific locale
33
     *
34
     * @param $locale
35
     * @param array $excluded filenames that should be excluded from the read
36
     * @return \Illuminate\Support\Collection
37
     */
38 24
    public function read($locale, array $excluded = [])
39
    {
40
        // Empty our translations for a new read
41 24
        $this->translations = collect();
42
43 24
        $files = $this->filesystem->listContents($locale);
44
45 24
        foreach ($files as $file) {
46 21
            $filename = substr($file['path'], strrpos($file['path'], '/'));
47 21
            $filename = ltrim($filename, '/');
48 21
            $filename = str_replace('.php', '', $filename);
49
50 21
            if (in_array($filename, $excluded)) {
51 3
                continue;
52
            }
53
54 21
            $this->translations[$filename] = require $this->path . DIRECTORY_SEPARATOR . $file['path'];
55
        }
56
57 24
        return $this;
58
    }
59
60
    /**
61
     * @return \Illuminate\Support\Collection
62
     */
63 3
    public function get()
64
    {
65 3
        return $this->translations;
66
    }
67
68
    /**
69
     * Flatten per file a multi-dimensional associative array with dots.
70
71
     * @return \Illuminate\Support\Collection
72
     */
73 1
    public function flattenPerFile()
74
    {
75 2
        return $this->translations->map(function ($values) {
76 3
            return Arr::dot($values);
77 3
        });
78
    }
79
80
    /**
81
     * Flatten all files which also flattens the filenames (groups) with dots.
82
     * Note: This will remove any empty language files
83
     *
84
     * @return \Illuminate\Support\Collection
85
     */
86
    public function flatten()
87
    {
88 18
        return $this->translations->map(function ($values, $groupkey) {
89 15
            return $this->prependGroupkey($values, $groupkey);
90 18
        })->collapse();
91
    }
92
93
    /**
94
     * @param $values
95
     * @param $groupkey
96
     * @return array
97
     */
98 15
    private function prependGroupkey($values, $groupkey)
99
    {
100 15
        $values = Arr::dot($values);
101 15
        $combined = [];
102
103 15
        foreach ($values as $key => $value) {
104
            // Empty arrays will be ignored in our flattening
105 15
            if (is_array($value) && empty($value)) {
106
                continue;
107
            }
108
109 15
            $combined[$groupkey . '.' . $key] = $value;
110
        }
111
112 15
        return $combined;
113
    }
114
115 24
    private function getSquantoLangPath()
116
    {
117 24
        $path = config('squanto.lang_path');
118 24
        return is_null($path) ? app('path.lang') : $path;
119
    }
120
}
121