Completed
Pull Request — master (#5)
by
unknown
07:46
created

LaravelTranslationsReader::validateTranslations()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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