1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nikaia\TranslationSheet; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Nikaia\TranslationSheet\Client\Api; |
7
|
|
|
use Nikaia\TranslationSheet\Sheet\Styles; |
8
|
|
|
use Nikaia\TranslationSheet\Sheet\TranslationsSheetCoordinates; |
9
|
|
|
|
10
|
|
|
class Spreadsheet |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
protected $id; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $locales = []; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $translations = []; |
20
|
|
|
|
21
|
|
|
/** @var Api */ |
22
|
|
|
protected $api; |
23
|
|
|
|
24
|
19 |
|
public function __construct($id, $locales, $api = null) |
25
|
|
|
{ |
26
|
19 |
|
$this->id = $id; |
27
|
19 |
|
$this->locales = $locales; |
28
|
19 |
|
$this->api = $api ?: app(Api::class); |
29
|
19 |
|
} |
30
|
|
|
|
31
|
3 |
|
public function setLocales($locales) |
32
|
|
|
{ |
33
|
3 |
|
$this->locales = $locales; |
34
|
|
|
|
35
|
3 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
18 |
|
public function setTranslations($translations) |
39
|
|
|
{ |
40
|
18 |
|
$this->translations = $translations; |
41
|
|
|
|
42
|
18 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function getTranslations() |
46
|
|
|
{ |
47
|
1 |
|
return $this->translations; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public function getTranslationsCount() |
51
|
|
|
{ |
52
|
4 |
|
return count($this->translations); |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
public function getId() |
56
|
|
|
{ |
57
|
5 |
|
return $this->id; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function getUrl() |
61
|
|
|
{ |
62
|
1 |
|
return 'https://docs.google.com/spreadsheets/d/' . $this->id; |
63
|
|
|
} |
64
|
|
|
|
65
|
11 |
|
public function getLocales() |
66
|
|
|
{ |
67
|
11 |
|
return $this->locales; |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
public function getLocalesCount() |
71
|
|
|
{ |
72
|
5 |
|
return count($this->locales); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
public function getHeader() |
76
|
|
|
{ |
77
|
8 |
|
return array_merge( |
78
|
8 |
|
array_merge(['Full key'], $this->getLocales()), |
79
|
8 |
|
['Namespace', 'Group', 'Key', 'Source file'] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function getCamelizedHeader() |
84
|
|
|
{ |
85
|
|
|
return array_map(function ($item) { |
86
|
3 |
|
if (in_array($item, Util::asArray($this->getLocales()))) { |
87
|
3 |
|
return $item; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return Str::camel($item); |
91
|
3 |
|
}, $this->getHeader()); |
92
|
|
|
} |
93
|
|
|
|
94
|
5 |
|
public function getHeaderColumnsCount() |
95
|
|
|
{ |
96
|
5 |
|
$header = $this->getHeader(); |
97
|
|
|
|
98
|
5 |
|
return count($header); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
public function getHeaderRowsCount() |
102
|
|
|
{ |
103
|
4 |
|
return 1; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $sheetId |
108
|
|
|
* @param $sheetTitle |
109
|
|
|
* |
110
|
|
|
* @return TranslationsSheetCoordinates |
111
|
|
|
*/ |
112
|
1 |
|
public function translationsEmptySheetCoordinates($sheetId, $sheetTitle) |
113
|
|
|
{ |
114
|
1 |
|
return TranslationsSheetCoordinates::emptySheet( |
115
|
1 |
|
$this->getHeaderColumnsCount(), |
116
|
1 |
|
$this->getLocalesCount(), |
117
|
1 |
|
$this->getHeaderColumnsCount() |
118
|
1 |
|
)->setSheetId($sheetId)->setSheetTitle($sheetTitle); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $sheetId |
123
|
|
|
* @param $sheetTitle |
124
|
|
|
* @return TranslationsSheetCoordinates |
125
|
|
|
*/ |
126
|
3 |
|
public function translationsSheetCoordinates($sheetId, $sheetTitle) |
127
|
|
|
{ |
128
|
3 |
|
return TranslationsSheetCoordinates::sheetWithData( |
129
|
3 |
|
$this->getTranslationsCount(), |
130
|
3 |
|
$this->getHeaderColumnsCount(), |
131
|
3 |
|
$this->getLocalesCount(), |
132
|
3 |
|
$this->getHeaderRowsCount() |
133
|
3 |
|
)->setSheetId($sheetId)->setSheetTitle($sheetTitle); |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
public function sheetStyles() |
137
|
|
|
{ |
138
|
2 |
|
return new Styles; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function deleteAllSheets() |
142
|
|
|
{ |
143
|
|
|
// We need to create a black sheet, cause we cannot delete all sheets |
144
|
1 |
|
$this->api() |
145
|
1 |
|
->addBatchRequests([ |
146
|
1 |
|
$this->api()->addBlankSheet() |
147
|
|
|
]) |
148
|
1 |
|
->sendBatchRequests(); |
149
|
|
|
|
150
|
|
|
// Delete all sheet and keep the last created one |
151
|
1 |
|
$this->api() |
152
|
1 |
|
->addBatchRequests( |
153
|
|
|
collect($this->api()->getSheets())->slice(0, -1)->map(function ($sheet) { |
154
|
1 |
|
return $this->api()->deleteSheetRequest($sheet['properties']['sheetId']); |
155
|
1 |
|
}) |
156
|
1 |
|
->toArray() |
157
|
|
|
) |
158
|
1 |
|
->sendBatchRequests(); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return api instance initialized with the spreadsheet ID. |
163
|
|
|
* |
164
|
|
|
* @return Api |
165
|
|
|
*/ |
166
|
3 |
|
public function api() |
167
|
|
|
{ |
168
|
3 |
|
return $this->api->setSpreadsheetId($this->getId()); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|