|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nikaia\TranslationSheet\Sheet; |
|
4
|
|
|
|
|
5
|
|
|
class TranslationsSheetCoordinates |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
protected $headerRowsCount = 1; |
|
8
|
|
|
|
|
9
|
|
|
protected $dataRowsCount = 0; |
|
10
|
|
|
|
|
11
|
|
|
protected $columnsCount = 0; |
|
12
|
|
|
|
|
13
|
|
|
protected $localesCount = 1; |
|
14
|
|
|
|
|
15
|
|
|
protected $sheetId; |
|
16
|
|
|
|
|
17
|
|
|
protected $sheetTitle; |
|
18
|
|
|
|
|
19
|
8 |
|
private function __construct() |
|
20
|
|
|
{ |
|
21
|
8 |
|
} |
|
22
|
|
|
|
|
23
|
3 |
|
public function setSheetId($sheetId) |
|
24
|
|
|
{ |
|
25
|
3 |
|
$this->sheetId = $sheetId; |
|
26
|
|
|
|
|
27
|
3 |
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function setSheetTitle($sheetTitle) |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->sheetTitle = $sheetTitle; |
|
33
|
|
|
|
|
34
|
3 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
6 |
|
public static function emptySheet($columnsCount, $localesCount, $headerRowsCount) |
|
38
|
|
|
{ |
|
39
|
6 |
|
$instance = new self; |
|
40
|
|
|
|
|
41
|
6 |
|
$instance->headerRowsCount = $headerRowsCount; |
|
42
|
6 |
|
$instance->dataRowsCount = 10; |
|
43
|
6 |
|
$instance->columnsCount = $columnsCount; |
|
44
|
6 |
|
$instance->localesCount = $localesCount; |
|
45
|
|
|
|
|
46
|
6 |
|
return $instance; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public static function sheetWithData($dataRowsCount, $columnsCount, $localesCount, $headerRowsCount) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$instance = new self; |
|
52
|
|
|
|
|
53
|
2 |
|
$instance->headerRowsCount = $headerRowsCount; |
|
54
|
2 |
|
$instance->dataRowsCount = $dataRowsCount; |
|
55
|
2 |
|
$instance->columnsCount = $columnsCount; |
|
56
|
2 |
|
$instance->localesCount = $localesCount; |
|
57
|
|
|
|
|
58
|
2 |
|
return $instance; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function headerShortRange() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->sheetTitle.'!A1:'.self::stringFromColumnIndex($this->getColumnsCount()).'1'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
View Code Duplication |
public function headerRange() |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
1 |
|
'sheetId' => $this->sheetId, |
|
70
|
1 |
|
'startRowIndex' => 0, |
|
71
|
1 |
|
'endRowIndex' => 1, |
|
72
|
1 |
|
'startColumnIndex' => 0, |
|
73
|
1 |
|
'endColumnIndex' => $this->getColumnsCount(), |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
View Code Duplication |
public function fullKeyColumnRange() |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
1 |
|
'sheetId' => $this->sheetId, |
|
81
|
1 |
|
'startRowIndex' => 1, |
|
82
|
1 |
|
'endRowIndex' => $this->getRowsCount(), |
|
83
|
1 |
|
'startColumnIndex' => 0, |
|
84
|
1 |
|
'endColumnIndex' => 1, |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function metaColumnsRange() |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
1 |
|
'sheetId' => $this->sheetId, |
|
92
|
1 |
|
'startRowIndex' => 1, |
|
93
|
1 |
|
'endRowIndex' => $this->getRowsCount(), |
|
94
|
1 |
|
'startColumnIndex' => $this->getLocalesCount() + 1, |
|
95
|
1 |
|
'endColumnIndex' => $this->getColumnsCount(), |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function dataShortRange($firstRow = 2, $noLastRow = false) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$firstColumn = 'A'; |
|
102
|
2 |
|
$lastColumn = self::stringFromColumnIndex($this->getColumnsCount()); |
|
103
|
2 |
|
$lastRow = $this->getRowsCount(); |
|
104
|
|
|
|
|
105
|
2 |
|
return $this->sheetTitle.'!'.$firstColumn.$firstRow.':'.$lastColumn.($noLastRow ? '' : $lastRow); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
public function dataRange($endRow, $firstRow = 2) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
'sheetId' => $this->sheetId, |
|
112
|
|
|
'startRowIndex' => $firstRow, |
|
113
|
|
|
'endRowIndex' => $endRow, |
|
114
|
|
|
'startColumnIndex' => 0, |
|
115
|
|
|
'endColumnIndex' => $this->getColumnsCount(), |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
3 |
|
public function translationsRange($firstColumn = 1, $rowCount = null) |
|
120
|
|
|
{ |
|
121
|
|
|
return [ |
|
122
|
3 |
|
'sheetId' => $this->sheetId, |
|
123
|
3 |
|
'startRowIndex' => 1, |
|
124
|
3 |
|
'endRowIndex' => $rowCount ?: $this->getRowsCount(), |
|
125
|
3 |
|
'startColumnIndex' => $firstColumn, |
|
126
|
3 |
|
'endColumnIndex' => $firstColumn + $this->getLocalesCount(), |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
3 |
|
public function getColumnsCount() |
|
131
|
|
|
{ |
|
132
|
3 |
|
return $this->columnsCount; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
5 |
|
public function getRowsCount() |
|
136
|
|
|
{ |
|
137
|
5 |
|
return $this->dataRowsCount + $this->headerRowsCount; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
3 |
|
public function getLocalesCount() |
|
141
|
|
|
{ |
|
142
|
3 |
|
return $this->localesCount; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
public function namespaceColumnIndex() |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $this->getLocalesCount() + 1; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
public function groupColumnIndex() |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->getLocalesCount() + 2; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
public function keyColumnIndex() |
|
156
|
|
|
{ |
|
157
|
1 |
|
return $this->getLocalesCount() + 3; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
public function sourceFileColumnIndex() |
|
161
|
|
|
{ |
|
162
|
1 |
|
return $this->getLocalesCount() + 4; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* String from column index. |
|
167
|
|
|
* |
|
168
|
|
|
* @see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Cell/Coordinate.php Source of implementation. |
|
169
|
|
|
* |
|
170
|
|
|
* @license https://raw.githubusercontent.com/PHPOffice/PhpSpreadsheet/master/LICENSE LGPL (GNU LESSER GENERAL PUBLIC LICENSE) |
|
171
|
|
|
* |
|
172
|
|
|
* @param int $columnIndex Column index (A = 1) |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public static function stringFromColumnIndex($columnIndex) |
|
177
|
|
|
{ |
|
178
|
2 |
|
static $indexCache = []; |
|
179
|
2 |
|
if (!isset($indexCache[$columnIndex])) { |
|
180
|
1 |
|
$indexValue = $columnIndex; |
|
181
|
1 |
|
$base26 = null; |
|
182
|
|
|
do { |
|
183
|
1 |
|
$characterValue = ($indexValue % 26) ?: 26; |
|
184
|
1 |
|
$indexValue = ($indexValue - $characterValue) / 26; |
|
185
|
1 |
|
$base26 = chr($characterValue + 64) . ($base26 ?: ''); |
|
186
|
1 |
|
} while ($indexValue > 0); |
|
187
|
1 |
|
$indexCache[$columnIndex] = $base26; |
|
188
|
|
|
} |
|
189
|
2 |
|
return $indexCache[$columnIndex]; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|