Failed Conditions
Push — master ( 11575e...1c99f4 )
by Mark
28:06
created

Alignment::setVertical()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 204
    public function __construct($isSupervisor = false, $isConditional = false)
91
    {
92
        // Supervisor?
93 204
        parent::__construct($isSupervisor);
94
95 204
        if ($isConditional) {
96 3
            $this->horizontal = null;
97 3
            $this->vertical = null;
98 3
            $this->textRotation = null;
99
        }
100 204
    }
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 1
    public function getSharedComponent()
109
    {
110 1
        return $this->parent->getSharedComponent()->getAlignment();
1 ignored issue
show
Bug introduced by
The method getSharedComponent() does not exist on PhpOffice\PhpSpreadsheet\Spreadsheet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        return $this->parent->/** @scrutinizer ignore-call */ getSharedComponent()->getAlignment();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
     *
128
     * <code>
129
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
130
     *        [
131
     *            'horizontal'   => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
132
     *            'vertical'     => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
133
     *            'textRotation' => 0,
134
     *            'wrapText'     => TRUE
135
     *        ]
136
     * );
137
     * </code>
138
     *
139
     * @param array $pStyles Array containing style information
140
     *
141
     * @throws PhpSpreadsheetException
142
     *
143
     * @return Alignment
144
     */
145 22
    public function applyFromArray(array $pStyles)
146
    {
147 22
        if ($this->isSupervisor) {
148
            $this->getActiveSheet()->getStyle($this->getSelectedCells())
149
                ->applyFromArray($this->getStyleArray($pStyles));
150
        } else {
151 22
            if (isset($pStyles['horizontal'])) {
152 15
                $this->setHorizontal($pStyles['horizontal']);
153
            }
154 22
            if (isset($pStyles['vertical'])) {
155 15
                $this->setVertical($pStyles['vertical']);
156
            }
157 22
            if (isset($pStyles['textRotation'])) {
158
                $this->setTextRotation($pStyles['textRotation']);
159
            }
160 22
            if (isset($pStyles['wrapText'])) {
161 22
                $this->setWrapText($pStyles['wrapText']);
162
            }
163 22
            if (isset($pStyles['shrinkToFit'])) {
164 12
                $this->setShrinkToFit($pStyles['shrinkToFit']);
165
            }
166 22
            if (isset($pStyles['indent'])) {
167 2
                $this->setIndent($pStyles['indent']);
168
            }
169 22
            if (isset($pStyles['readOrder'])) {
170
                $this->setReadOrder($pStyles['readOrder']);
171
            }
172
        }
173
174 22
        return $this;
175
    }
176
177
    /**
178
     * Get Horizontal.
179
     *
180
     * @return string
181
     */
182 103
    public function getHorizontal()
183
    {
184 103
        if ($this->isSupervisor) {
185 1
            return $this->getSharedComponent()->getHorizontal();
186
        }
187
188 103
        return $this->horizontal;
189
    }
190
191
    /**
192
     * Set Horizontal.
193
     *
194
     * @param string $pValue see self::HORIZONTAL_*
195
     *
196
     * @return Alignment
197
     */
198 73
    public function setHorizontal($pValue)
199
    {
200 73
        if ($pValue == '') {
201 39
            $pValue = self::HORIZONTAL_GENERAL;
202
        }
203
204 73
        if ($this->isSupervisor) {
205 12
            $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
206 12
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
207
        } else {
208 73
            $this->horizontal = $pValue;
209
        }
210
211 73
        return $this;
212
    }
213
214
    /**
215
     * Get Vertical.
216
     *
217
     * @return string
218
     */
219 101
    public function getVertical()
220
    {
221 101
        if ($this->isSupervisor) {
222 1
            return $this->getSharedComponent()->getVertical();
223
        }
224
225 101
        return $this->vertical;
226
    }
227
228
    /**
229
     * Set Vertical.
230
     *
231
     * @param string $pValue see self::VERTICAL_*
232
     *
233
     * @return Alignment
234
     */
235 73
    public function setVertical($pValue)
236
    {
237 73
        if ($pValue == '') {
238 37
            $pValue = self::VERTICAL_BOTTOM;
239
        }
240
241 73
        if ($this->isSupervisor) {
242 12
            $styleArray = $this->getStyleArray(['vertical' => $pValue]);
243 12
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
244
        } else {
245 73
            $this->vertical = $pValue;
246
        }
247
248 73
        return $this;
249
    }
250
251
    /**
252
     * Get TextRotation.
253
     *
254
     * @return int
255
     */
256 92
    public function getTextRotation()
257
    {
258 92
        if ($this->isSupervisor) {
259
            return $this->getSharedComponent()->getTextRotation();
260
        }
261
262 92
        return $this->textRotation;
263
    }
264
265
    /**
266
     * Set TextRotation.
267
     *
268
     * @param int $pValue
269
     *
270
     * @throws PhpSpreadsheetException
271
     *
272
     * @return Alignment
273
     */
274 61
    public function setTextRotation($pValue)
275
    {
276
        // Excel2007 value 255 => PhpSpreadsheet value -165
277 61
        if ($pValue == 255) {
278
            $pValue = -165;
279
        }
280
281
        // Set rotation
282 61
        if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
283 61
            if ($this->isSupervisor) {
284
                $styleArray = $this->getStyleArray(['textRotation' => $pValue]);
285
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
286
            } else {
287 61
                $this->textRotation = $pValue;
288
            }
289
        } else {
290
            throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
291
        }
292
293 61
        return $this;
294
    }
295
296
    /**
297
     * Get Wrap Text.
298
     *
299
     * @return bool
300
     */
301 90
    public function getWrapText()
302
    {
303 90
        if ($this->isSupervisor) {
304 1
            return $this->getSharedComponent()->getWrapText();
305
        }
306
307 90
        return $this->wrapText;
308
    }
309
310
    /**
311
     * Set Wrap Text.
312
     *
313
     * @param bool $pValue
314
     *
315
     * @return Alignment
316
     */
317 80
    public function setWrapText($pValue)
318
    {
319 80
        if ($pValue == '') {
320 62
            $pValue = false;
321
        }
322 80
        if ($this->isSupervisor) {
323 19
            $styleArray = $this->getStyleArray(['wrapText' => $pValue]);
324 19
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
325
        } else {
326 80
            $this->wrapText = $pValue;
327
        }
328
329 80
        return $this;
330
    }
331
332
    /**
333
     * Get Shrink to fit.
334
     *
335
     * @return bool
336
     */
337 89
    public function getShrinkToFit()
338
    {
339 89
        if ($this->isSupervisor) {
340
            return $this->getSharedComponent()->getShrinkToFit();
341
        }
342
343 89
        return $this->shrinkToFit;
344
    }
345
346
    /**
347
     * Set Shrink to fit.
348
     *
349
     * @param bool $pValue
350
     *
351
     * @return Alignment
352
     */
353 70
    public function setShrinkToFit($pValue)
354
    {
355 70
        if ($pValue == '') {
356 62
            $pValue = false;
357
        }
358 70
        if ($this->isSupervisor) {
359 11
            $styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]);
360 11
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
361
        } else {
362 70
            $this->shrinkToFit = $pValue;
363
        }
364
365 70
        return $this;
366
    }
367
368
    /**
369
     * Get indent.
370
     *
371
     * @return int
372
     */
373 93
    public function getIndent()
374
    {
375 93
        if ($this->isSupervisor) {
376 1
            return $this->getSharedComponent()->getIndent();
377
        }
378
379 93
        return $this->indent;
380
    }
381
382
    /**
383
     * Set indent.
384
     *
385
     * @param int $pValue
386
     *
387
     * @return Alignment
388
     */
389 63
    public function setIndent($pValue)
390
    {
391 63
        if ($pValue > 0) {
392 3
            if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
393 3
                $this->getHorizontal() != self::HORIZONTAL_LEFT &&
394 3
                $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
395
                $pValue = 0; // indent not supported
396
            }
397
        }
398 63
        if ($this->isSupervisor) {
399 1
            $styleArray = $this->getStyleArray(['indent' => $pValue]);
400 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
401
        } else {
402 63
            $this->indent = $pValue;
403
        }
404
405 63
        return $this;
406
    }
407
408
    /**
409
     * Get read order.
410
     *
411
     * @return int
412
     */
413 83
    public function getReadOrder()
414
    {
415 83
        if ($this->isSupervisor) {
416
            return $this->getSharedComponent()->getReadOrder();
417
        }
418
419 83
        return $this->readOrder;
420
    }
421
422
    /**
423
     * Set read order.
424
     *
425
     * @param int $pValue
426
     *
427
     * @return Alignment
428
     */
429 39
    public function setReadOrder($pValue)
430
    {
431 39
        if ($pValue < 0 || $pValue > 2) {
432
            $pValue = 0;
433
        }
434 39
        if ($this->isSupervisor) {
435
            $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
436
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
437
        } else {
438 39
            $this->readOrder = $pValue;
439
        }
440
441 39
        return $this;
442
    }
443
444
    /**
445
     * Get hash code.
446
     *
447
     * @return string Hash code
448
     */
449 114
    public function getHashCode()
450
    {
451 114
        if ($this->isSupervisor) {
452
            return $this->getSharedComponent()->getHashCode();
453
        }
454
455 114
        return md5(
456 114
            $this->horizontal .
457 114
            $this->vertical .
458 114
            $this->textRotation .
459 114
            ($this->wrapText ? 't' : 'f') .
460 114
            ($this->shrinkToFit ? 't' : 'f') .
461 114
            $this->indent .
462 114
            $this->readOrder .
463 114
            __CLASS__
464
        );
465
    }
466
}
467