1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
|
7
|
|
|
class Alignment extends Supervisor |
8
|
|
|
{ |
9
|
|
|
// Horizontal alignment styles |
10
|
|
|
const HORIZONTAL_GENERAL = 'general'; |
11
|
|
|
const HORIZONTAL_LEFT = 'left'; |
12
|
|
|
const HORIZONTAL_RIGHT = 'right'; |
13
|
|
|
const HORIZONTAL_CENTER = 'center'; |
14
|
|
|
const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'; |
15
|
|
|
const HORIZONTAL_JUSTIFY = 'justify'; |
16
|
|
|
const HORIZONTAL_FILL = 'fill'; |
17
|
|
|
const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only |
18
|
|
|
|
19
|
|
|
// Vertical alignment styles |
20
|
|
|
const VERTICAL_BOTTOM = 'bottom'; |
21
|
|
|
const VERTICAL_TOP = 'top'; |
22
|
|
|
const VERTICAL_CENTER = 'center'; |
23
|
|
|
const VERTICAL_JUSTIFY = 'justify'; |
24
|
|
|
const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only |
25
|
|
|
|
26
|
|
|
// Read order |
27
|
|
|
const READORDER_CONTEXT = 0; |
28
|
|
|
const READORDER_LTR = 1; |
29
|
|
|
const READORDER_RTL = 2; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Horizontal alignment. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $horizontal = self::HORIZONTAL_GENERAL; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Vertical alignment. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $vertical = self::VERTICAL_BOTTOM; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Text rotation. |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $textRotation = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Wrap text. |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $wrapText = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Shrink to fit. |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
protected $shrinkToFit = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Indent - only possible with horizontal alignment left and right. |
68
|
|
|
* |
69
|
|
|
* @var int |
70
|
|
|
*/ |
71
|
|
|
protected $indent = 0; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Read order. |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected $readOrder = 0; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create a new Alignment. |
82
|
|
|
* |
83
|
|
|
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
84
|
|
|
* Leave this value at default unless you understand exactly what |
85
|
|
|
* its ramifications are |
86
|
|
|
* @param bool $isConditional Flag indicating if this is a conditional style or not |
87
|
|
|
* Leave this value at default unless you understand exactly what |
88
|
|
|
* its ramifications are |
89
|
|
|
*/ |
90
|
137 |
|
public function __construct($isSupervisor = false, $isConditional = false) |
91
|
|
|
{ |
92
|
|
|
// Supervisor? |
93
|
137 |
|
parent::__construct($isSupervisor); |
94
|
|
|
|
95
|
137 |
|
if ($isConditional) { |
96
|
2 |
|
$this->horizontal = null; |
97
|
2 |
|
$this->vertical = null; |
98
|
2 |
|
$this->textRotation = null; |
99
|
|
|
} |
100
|
137 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the shared style component for the currently active cell in currently active sheet. |
104
|
|
|
* Only used for style supervisor. |
105
|
|
|
* |
106
|
|
|
* @return Alignment |
107
|
|
|
*/ |
108
|
|
|
public function getSharedComponent() |
109
|
|
|
{ |
110
|
|
|
return $this->parent->getSharedComponent()->getAlignment(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Build style array from subcomponents. |
115
|
|
|
* |
116
|
|
|
* @param array $array |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
19 |
|
public function getStyleArray($array) |
121
|
|
|
{ |
122
|
19 |
|
return ['alignment' => $array]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Apply styles from array. |
127
|
|
|
* <code> |
128
|
|
|
* $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray( |
129
|
|
|
* array( |
130
|
|
|
* 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, |
131
|
|
|
* 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, |
132
|
|
|
* 'textRotation' => 0, |
133
|
|
|
* 'wrapText' => TRUE |
134
|
|
|
* ) |
135
|
|
|
* ); |
136
|
|
|
* </code>. |
137
|
|
|
* |
138
|
|
|
* @param array $pStyles Array containing style information |
139
|
|
|
* |
140
|
|
|
* @throws PhpSpreadsheetException |
141
|
|
|
* |
142
|
|
|
* @return Alignment |
143
|
|
|
*/ |
144
|
22 |
|
public function applyFromArray(array $pStyles) |
145
|
|
|
{ |
146
|
22 |
|
if ($this->isSupervisor) { |
147
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells()) |
148
|
|
|
->applyFromArray($this->getStyleArray($pStyles)); |
149
|
|
|
} else { |
150
|
22 |
|
if (isset($pStyles['horizontal'])) { |
151
|
15 |
|
$this->setHorizontal($pStyles['horizontal']); |
152
|
|
|
} |
153
|
22 |
|
if (isset($pStyles['vertical'])) { |
154
|
15 |
|
$this->setVertical($pStyles['vertical']); |
155
|
|
|
} |
156
|
22 |
|
if (isset($pStyles['textRotation'])) { |
157
|
|
|
$this->setTextRotation($pStyles['textRotation']); |
158
|
|
|
} |
159
|
22 |
|
if (isset($pStyles['wrapText'])) { |
160
|
22 |
|
$this->setWrapText($pStyles['wrapText']); |
161
|
|
|
} |
162
|
22 |
|
if (isset($pStyles['shrinkToFit'])) { |
163
|
13 |
|
$this->setShrinkToFit($pStyles['shrinkToFit']); |
164
|
|
|
} |
165
|
22 |
|
if (isset($pStyles['indent'])) { |
166
|
1 |
|
$this->setIndent($pStyles['indent']); |
167
|
|
|
} |
168
|
22 |
|
if (isset($pStyles['readOrder'])) { |
169
|
|
|
$this->setReadOrder($pStyles['readOrder']); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
22 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get Horizontal. |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
63 |
|
public function getHorizontal() |
182
|
|
|
{ |
183
|
63 |
|
if ($this->isSupervisor) { |
184
|
|
|
return $this->getSharedComponent()->getHorizontal(); |
185
|
|
|
} |
186
|
|
|
|
187
|
63 |
|
return $this->horizontal; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set Horizontal. |
192
|
|
|
* |
193
|
|
|
* @param string $pValue see self::HORIZONTAL_* |
194
|
|
|
* |
195
|
|
|
* @return Alignment |
196
|
|
|
*/ |
197
|
41 |
View Code Duplication |
public function setHorizontal($pValue) |
|
|
|
|
198
|
|
|
{ |
199
|
41 |
|
if ($pValue == '') { |
200
|
12 |
|
$pValue = self::HORIZONTAL_GENERAL; |
201
|
|
|
} |
202
|
|
|
|
203
|
41 |
|
if ($this->isSupervisor) { |
204
|
12 |
|
$styleArray = $this->getStyleArray(['horizontal' => $pValue]); |
205
|
12 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
206
|
|
|
} else { |
207
|
41 |
|
$this->horizontal = $pValue; |
208
|
|
|
} |
209
|
|
|
|
210
|
41 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get Vertical. |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
62 |
|
public function getVertical() |
219
|
|
|
{ |
220
|
62 |
|
if ($this->isSupervisor) { |
221
|
|
|
return $this->getSharedComponent()->getVertical(); |
222
|
|
|
} |
223
|
|
|
|
224
|
62 |
|
return $this->vertical; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set Vertical. |
229
|
|
|
* |
230
|
|
|
* @param string $pValue see self::VERTICAL_* |
231
|
|
|
* |
232
|
|
|
* @return Alignment |
233
|
|
|
*/ |
234
|
41 |
View Code Duplication |
public function setVertical($pValue) |
|
|
|
|
235
|
|
|
{ |
236
|
41 |
|
if ($pValue == '') { |
237
|
11 |
|
$pValue = self::VERTICAL_BOTTOM; |
238
|
|
|
} |
239
|
|
|
|
240
|
41 |
|
if ($this->isSupervisor) { |
241
|
12 |
|
$styleArray = $this->getStyleArray(['vertical' => $pValue]); |
242
|
12 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
243
|
|
|
} else { |
244
|
41 |
|
$this->vertical = $pValue; |
245
|
|
|
} |
246
|
|
|
|
247
|
41 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get TextRotation. |
252
|
|
|
* |
253
|
|
|
* @return int |
254
|
|
|
*/ |
255
|
62 |
|
public function getTextRotation() |
256
|
|
|
{ |
257
|
62 |
|
if ($this->isSupervisor) { |
258
|
|
|
return $this->getSharedComponent()->getTextRotation(); |
259
|
|
|
} |
260
|
|
|
|
261
|
62 |
|
return $this->textRotation; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set TextRotation. |
266
|
|
|
* |
267
|
|
|
* @param int $pValue |
268
|
|
|
* |
269
|
|
|
* @throws PhpSpreadsheetException |
270
|
|
|
* |
271
|
|
|
* @return Alignment |
272
|
|
|
*/ |
273
|
29 |
|
public function setTextRotation($pValue) |
274
|
|
|
{ |
275
|
|
|
// Excel2007 value 255 => PhpSpreadsheet value -165 |
276
|
29 |
|
if ($pValue == 255) { |
277
|
|
|
$pValue = -165; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// Set rotation |
281
|
29 |
|
if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) { |
282
|
29 |
|
if ($this->isSupervisor) { |
283
|
|
|
$styleArray = $this->getStyleArray(['textRotation' => $pValue]); |
284
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
285
|
|
|
} else { |
286
|
29 |
|
$this->textRotation = $pValue; |
287
|
|
|
} |
288
|
|
|
} else { |
289
|
|
|
throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.'); |
290
|
|
|
} |
291
|
|
|
|
292
|
29 |
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get Wrap Text. |
297
|
|
|
* |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
58 |
|
public function getWrapText() |
301
|
|
|
{ |
302
|
58 |
|
if ($this->isSupervisor) { |
303
|
|
|
return $this->getSharedComponent()->getWrapText(); |
304
|
|
|
} |
305
|
|
|
|
306
|
58 |
|
return $this->wrapText; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Set Wrap Text. |
311
|
|
|
* |
312
|
|
|
* @param bool $pValue |
313
|
|
|
* |
314
|
|
|
* @return Alignment |
315
|
|
|
*/ |
316
|
48 |
View Code Duplication |
public function setWrapText($pValue) |
|
|
|
|
317
|
|
|
{ |
318
|
48 |
|
if ($pValue == '') { |
319
|
30 |
|
$pValue = false; |
320
|
|
|
} |
321
|
48 |
|
if ($this->isSupervisor) { |
322
|
19 |
|
$styleArray = $this->getStyleArray(['wrapText' => $pValue]); |
323
|
19 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
324
|
|
|
} else { |
325
|
48 |
|
$this->wrapText = $pValue; |
326
|
|
|
} |
327
|
|
|
|
328
|
48 |
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get Shrink to fit. |
333
|
|
|
* |
334
|
|
|
* @return bool |
335
|
|
|
*/ |
336
|
58 |
|
public function getShrinkToFit() |
337
|
|
|
{ |
338
|
58 |
|
if ($this->isSupervisor) { |
339
|
|
|
return $this->getSharedComponent()->getShrinkToFit(); |
340
|
|
|
} |
341
|
|
|
|
342
|
58 |
|
return $this->shrinkToFit; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Set Shrink to fit. |
347
|
|
|
* |
348
|
|
|
* @param bool $pValue |
349
|
|
|
* |
350
|
|
|
* @return Alignment |
351
|
|
|
*/ |
352
|
39 |
View Code Duplication |
public function setShrinkToFit($pValue) |
|
|
|
|
353
|
|
|
{ |
354
|
39 |
|
if ($pValue == '') { |
355
|
30 |
|
$pValue = false; |
356
|
|
|
} |
357
|
39 |
|
if ($this->isSupervisor) { |
358
|
12 |
|
$styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]); |
359
|
12 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
360
|
|
|
} else { |
361
|
39 |
|
$this->shrinkToFit = $pValue; |
362
|
|
|
} |
363
|
|
|
|
364
|
39 |
|
return $this; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Get indent. |
369
|
|
|
* |
370
|
|
|
* @return int |
371
|
|
|
*/ |
372
|
62 |
|
public function getIndent() |
373
|
|
|
{ |
374
|
62 |
|
if ($this->isSupervisor) { |
375
|
|
|
return $this->getSharedComponent()->getIndent(); |
376
|
|
|
} |
377
|
|
|
|
378
|
62 |
|
return $this->indent; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Set indent. |
383
|
|
|
* |
384
|
|
|
* @param int $pValue |
385
|
|
|
* |
386
|
|
|
* @return Alignment |
387
|
|
|
*/ |
388
|
30 |
|
public function setIndent($pValue) |
389
|
|
|
{ |
390
|
30 |
|
if ($pValue > 0) { |
391
|
1 |
|
if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && |
392
|
1 |
|
$this->getHorizontal() != self::HORIZONTAL_LEFT && |
393
|
1 |
|
$this->getHorizontal() != self::HORIZONTAL_RIGHT) { |
394
|
|
|
$pValue = 0; // indent not supported |
395
|
|
|
} |
396
|
|
|
} |
397
|
30 |
|
if ($this->isSupervisor) { |
398
|
|
|
$styleArray = $this->getStyleArray(['indent' => $pValue]); |
399
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
400
|
|
|
} else { |
401
|
30 |
|
$this->indent = $pValue; |
402
|
|
|
} |
403
|
|
|
|
404
|
30 |
|
return $this; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Get read order. |
409
|
|
|
* |
410
|
|
|
* @return int |
411
|
|
|
*/ |
412
|
56 |
|
public function getReadOrder() |
413
|
|
|
{ |
414
|
56 |
|
if ($this->isSupervisor) { |
415
|
|
|
return $this->getSharedComponent()->getReadOrder(); |
416
|
|
|
} |
417
|
|
|
|
418
|
56 |
|
return $this->readOrder; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Set read order. |
423
|
|
|
* |
424
|
|
|
* @param int $pValue |
425
|
|
|
* |
426
|
|
|
* @return Alignment |
427
|
|
|
*/ |
428
|
12 |
|
public function setReadOrder($pValue) |
429
|
|
|
{ |
430
|
12 |
|
if ($pValue < 0 || $pValue > 2) { |
431
|
|
|
$pValue = 0; |
432
|
|
|
} |
433
|
12 |
|
if ($this->isSupervisor) { |
434
|
|
|
$styleArray = $this->getStyleArray(['readOrder' => $pValue]); |
435
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
436
|
|
|
} else { |
437
|
12 |
|
$this->readOrder = $pValue; |
438
|
|
|
} |
439
|
|
|
|
440
|
12 |
|
return $this; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Get hash code. |
445
|
|
|
* |
446
|
|
|
* @return string Hash code |
447
|
|
|
*/ |
448
|
76 |
|
public function getHashCode() |
449
|
|
|
{ |
450
|
76 |
|
if ($this->isSupervisor) { |
451
|
|
|
return $this->getSharedComponent()->getHashCode(); |
452
|
|
|
} |
453
|
|
|
|
454
|
76 |
|
return md5( |
455
|
76 |
|
$this->horizontal . |
456
|
76 |
|
$this->vertical . |
457
|
76 |
|
$this->textRotation . |
458
|
76 |
|
($this->wrapText ? 't' : 'f') . |
459
|
76 |
|
($this->shrinkToFit ? 't' : 'f') . |
460
|
76 |
|
$this->indent . |
461
|
76 |
|
$this->readOrder . |
462
|
76 |
|
__CLASS__ |
463
|
|
|
); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.