Completed
Push — master ( f3d591...31e394 )
by Ben
02:18
created

LaravelTranslationsReader::flattenPerFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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