Completed
Push — develop ( e6c95b...1c5db4 )
by Adrien
32:25
created

Alignment::setShrinkToFit()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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