Completed
Push — develop ( 8380fb...ce4b86 )
by Adrien
34:13
created

Alignment::getHashCode()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 0
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.2
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 172
    public function __construct($isSupervisor = false, $isConditional = false)
91
    {
92
        // Supervisor?
93 172
        parent::__construct($isSupervisor);
94
95 172
        if ($isConditional) {
96 3
            $this->horizontal = null;
97 3
            $this->vertical = null;
98 3
            $this->textRotation = null;
99
        }
100 172
    }
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();
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 13
                $this->setShrinkToFit($pStyles['shrinkToFit']);
165
            }
166 22
            if (isset($pStyles['indent'])) {
167 1
                $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 91
    public function getHorizontal()
183
    {
184 91
        if ($this->isSupervisor) {
185
            return $this->getSharedComponent()->getHorizontal();
186
        }
187
188 91
        return $this->horizontal;
189
    }
190
191
    /**
192
     * Set Horizontal.
193
     *
194
     * @param string $pValue see self::HORIZONTAL_*
195
     *
196
     * @return Alignment
197
     */
198 62
    public function setHorizontal($pValue)
199
    {
200 62
        if ($pValue == '') {
201 29
            $pValue = self::HORIZONTAL_GENERAL;
202
        }
203
204 62
        if ($this->isSupervisor) {
205 12
            $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
206 12
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
207
        } else {
208 62
            $this->horizontal = $pValue;
209
        }
210
211 62
        return $this;
212
    }
213
214
    /**
215
     * Get Vertical.
216
     *
217
     * @return string
218
     */
219 90
    public function getVertical()
220
    {
221 90
        if ($this->isSupervisor) {
222
            return $this->getSharedComponent()->getVertical();
223
        }
224
225 90
        return $this->vertical;
226
    }
227
228
    /**
229
     * Set Vertical.
230
     *
231
     * @param string $pValue see self::VERTICAL_*
232
     *
233
     * @return Alignment
234
     */
235 62
    public function setVertical($pValue)
236
    {
237 62
        if ($pValue == '') {
238 28
            $pValue = self::VERTICAL_BOTTOM;
239
        }
240
241 62
        if ($this->isSupervisor) {
242 12
            $styleArray = $this->getStyleArray(['vertical' => $pValue]);
243 12
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
244
        } else {
245 62
            $this->vertical = $pValue;
246
        }
247
248 62
        return $this;
249
    }
250
251
    /**
252
     * Get TextRotation.
253
     *
254
     * @return int
255
     */
256 83
    public function getTextRotation()
257
    {
258 83
        if ($this->isSupervisor) {
259
            return $this->getSharedComponent()->getTextRotation();
260
        }
261
262 83
        return $this->textRotation;
263
    }
264
265
    /**
266
     * Set TextRotation.
267
     *
268
     * @param int $pValue
269
     *
270
     * @throws PhpSpreadsheetException
271
     *
272
     * @return Alignment
273
     */
274 50
    public function setTextRotation($pValue)
275
    {
276
        // Excel2007 value 255 => PhpSpreadsheet value -165
277 50
        if ($pValue == 255) {
278
            $pValue = -165;
279
        }
280
281
        // Set rotation
282 50
        if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
283 50
            if ($this->isSupervisor) {
284
                $styleArray = $this->getStyleArray(['textRotation' => $pValue]);
285
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
286
            } else {
287 50
                $this->textRotation = $pValue;
288
            }
289
        } else {
290
            throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
291
        }
292
293 50
        return $this;
294
    }
295
296
    /**
297
     * Get Wrap Text.
298
     *
299
     * @return bool
300
     */
301 79
    public function getWrapText()
302
    {
303 79
        if ($this->isSupervisor) {
304
            return $this->getSharedComponent()->getWrapText();
305
        }
306
307 79
        return $this->wrapText;
308
    }
309
310
    /**
311
     * Set Wrap Text.
312
     *
313
     * @param bool $pValue
314
     *
315
     * @return Alignment
316
     */
317 69
    public function setWrapText($pValue)
318
    {
319 69
        if ($pValue == '') {
320 51
            $pValue = false;
321
        }
322 69
        if ($this->isSupervisor) {
323 19
            $styleArray = $this->getStyleArray(['wrapText' => $pValue]);
324 19
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
325
        } else {
326 69
            $this->wrapText = $pValue;
327
        }
328
329 69
        return $this;
330
    }
331
332
    /**
333
     * Get Shrink to fit.
334
     *
335
     * @return bool
336
     */
337 79
    public function getShrinkToFit()
338
    {
339 79
        if ($this->isSupervisor) {
340
            return $this->getSharedComponent()->getShrinkToFit();
341
        }
342
343 79
        return $this->shrinkToFit;
344
    }
345
346
    /**
347
     * Set Shrink to fit.
348
     *
349
     * @param bool $pValue
350
     *
351
     * @return Alignment
352
     */
353 60
    public function setShrinkToFit($pValue)
354
    {
355 60
        if ($pValue == '') {
356 51
            $pValue = false;
357
        }
358 60
        if ($this->isSupervisor) {
359 12
            $styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]);
360 12
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
361
        } else {
362 60
            $this->shrinkToFit = $pValue;
363
        }
364
365 60
        return $this;
366
    }
367
368
    /**
369
     * Get indent.
370
     *
371
     * @return int
372
     */
373 83
    public function getIndent()
374
    {
375 83
        if ($this->isSupervisor) {
376
            return $this->getSharedComponent()->getIndent();
377
        }
378
379 83
        return $this->indent;
380
    }
381
382
    /**
383
     * Set indent.
384
     *
385
     * @param int $pValue
386
     *
387
     * @return Alignment
388
     */
389 51
    public function setIndent($pValue)
390
    {
391 51
        if ($pValue > 0) {
392 1
            if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
393 1
                $this->getHorizontal() != self::HORIZONTAL_LEFT &&
394 1
                $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
395
                $pValue = 0; // indent not supported
396
            }
397
        }
398 51
        if ($this->isSupervisor) {
399
            $styleArray = $this->getStyleArray(['indent' => $pValue]);
400
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
401
        } else {
402 51
            $this->indent = $pValue;
403
        }
404
405 51
        return $this;
406
    }
407
408
    /**
409
     * Get read order.
410
     *
411
     * @return int
412
     */
413 73
    public function getReadOrder()
414
    {
415 73
        if ($this->isSupervisor) {
416
            return $this->getSharedComponent()->getReadOrder();
417
        }
418
419 73
        return $this->readOrder;
420
    }
421
422
    /**
423
     * Set read order.
424
     *
425
     * @param int $pValue
426
     *
427
     * @return Alignment
428
     */
429 29
    public function setReadOrder($pValue)
430
    {
431 29
        if ($pValue < 0 || $pValue > 2) {
432
            $pValue = 0;
433
        }
434 29
        if ($this->isSupervisor) {
435
            $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
436
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
437
        } else {
438 29
            $this->readOrder = $pValue;
439
        }
440
441 29
        return $this;
442
    }
443
444
    /**
445
     * Get hash code.
446
     *
447
     * @return string Hash code
448
     */
449 95
    public function getHashCode()
450
    {
451 95
        if ($this->isSupervisor) {
452
            return $this->getSharedComponent()->getHashCode();
453
        }
454
455 95
        return md5(
456 95
            $this->horizontal .
457 95
            $this->vertical .
458 95
            $this->textRotation .
459 95
            ($this->wrapText ? 't' : 'f') .
460 95
            ($this->shrinkToFit ? 't' : 'f') .
461 95
            $this->indent .
462 95
            $this->readOrder .
463 95
            __CLASS__
464
        );
465
    }
466
}
467