|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Adapters\Translations\Importers; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Common\Type; |
|
6
|
|
|
use Box\Spout\Reader\ReaderFactory; |
|
7
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\ImportTranslationInterface; |
|
8
|
|
|
use Rap2hpoutre\FastExcel\FastExcel; |
|
9
|
|
|
|
|
10
|
|
|
class ImporterFromExcel implements ImportTranslationInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $path |
|
14
|
|
|
* @param string $locale |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
3 |
|
public function import(string $path, string $locale = ''): array |
|
18
|
|
|
{ |
|
19
|
3 |
|
$sheetNames = $this->getSheets($path, $locale); |
|
20
|
3 |
|
$sheets = $this->importSheets($path, $sheetNames); |
|
21
|
|
|
|
|
22
|
3 |
|
if (count($sheetNames) <= 0) { |
|
23
|
1 |
|
return []; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
return $this->parseTranslations($sheetNames, $sheets); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $path |
|
31
|
|
|
* @param string $locale |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
3 |
|
private function getSheets(string $path, string $locale = ''): array |
|
35
|
|
|
{ |
|
36
|
3 |
|
$sheetNames = []; |
|
37
|
|
|
|
|
38
|
3 |
|
$reader = ReaderFactory::create(Type::XLSX); |
|
39
|
3 |
|
$reader->open($path); |
|
40
|
|
|
|
|
41
|
3 |
|
foreach ($reader->getSheetIterator() as $sheet) { |
|
42
|
3 |
|
array_push($sheetNames, $sheet->getName()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
if ('' != $locale && in_array($locale, $sheetNames)) { |
|
46
|
1 |
|
$sheetNames = [0 => $locale]; |
|
47
|
2 |
|
} elseif ('' != $locale && ! in_array($locale, $sheetNames)) { |
|
48
|
1 |
|
$sheetNames = []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
return $sheetNames; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $path |
|
56
|
|
|
* @param array $sheetNames |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
3 |
|
private function importSheets(string $path, array $sheetNames): array |
|
60
|
|
|
{ |
|
61
|
3 |
|
$sheets = (new FastExcel()); |
|
62
|
|
|
|
|
63
|
3 |
|
if (count($sheetNames) > 1) { |
|
64
|
1 |
|
return $sheets->importSheets($path)->toArray(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return [$sheets->sheet(key($sheetNames) + 1)->import($path)->toArray()]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $sheetNames |
|
72
|
|
|
* @param array $sheets |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
2 |
|
private function parseTranslations(array $sheetNames, array $sheets): array |
|
76
|
|
|
{ |
|
77
|
2 |
|
$translations = []; |
|
78
|
2 |
|
$parsedKeys = []; |
|
79
|
|
|
|
|
80
|
2 |
|
foreach ($sheets as $lang => $trans) { |
|
81
|
2 |
|
$translations[$sheetNames[$lang]] = $trans; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
foreach ($translations as $lang => $keys) { |
|
85
|
2 |
|
foreach ($keys as $key) { |
|
86
|
2 |
|
if ('' != $key['key2']) { |
|
87
|
2 |
|
$parsedKeys[$key['key2']]['key'] = $key['key2']; |
|
88
|
2 |
|
$parsedKeys[$key['key2']]['value'][$lang] = $key['value']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
return array_values($parsedKeys); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|