Completed
Push — develop ( 672893...dd9590 )
by Adrien
28:52 queued 21:58
created

PageSetup::getFirstPageNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Cell;
6
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
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
 * <code>
31
 * Paper size taken from Office Open XML Part 4 - Markup Language Reference, page 1988:
32
 *
33
 * 1 = Letter paper (8.5 in. by 11 in.)
34
 * 2 = Letter small paper (8.5 in. by 11 in.)
35
 * 3 = Tabloid paper (11 in. by 17 in.)
36
 * 4 = Ledger paper (17 in. by 11 in.)
37
 * 5 = Legal paper (8.5 in. by 14 in.)
38
 * 6 = Statement paper (5.5 in. by 8.5 in.)
39
 * 7 = Executive paper (7.25 in. by 10.5 in.)
40
 * 8 = A3 paper (297 mm by 420 mm)
41
 * 9 = A4 paper (210 mm by 297 mm)
42
 * 10 = A4 small paper (210 mm by 297 mm)
43
 * 11 = A5 paper (148 mm by 210 mm)
44
 * 12 = B4 paper (250 mm by 353 mm)
45
 * 13 = B5 paper (176 mm by 250 mm)
46
 * 14 = Folio paper (8.5 in. by 13 in.)
47
 * 15 = Quarto paper (215 mm by 275 mm)
48
 * 16 = Standard paper (10 in. by 14 in.)
49
 * 17 = Standard paper (11 in. by 17 in.)
50
 * 18 = Note paper (8.5 in. by 11 in.)
51
 * 19 = #9 envelope (3.875 in. by 8.875 in.)
52
 * 20 = #10 envelope (4.125 in. by 9.5 in.)
53
 * 21 = #11 envelope (4.5 in. by 10.375 in.)
54
 * 22 = #12 envelope (4.75 in. by 11 in.)
55
 * 23 = #14 envelope (5 in. by 11.5 in.)
56
 * 24 = C paper (17 in. by 22 in.)
57
 * 25 = D paper (22 in. by 34 in.)
58
 * 26 = E paper (34 in. by 44 in.)
59
 * 27 = DL envelope (110 mm by 220 mm)
60
 * 28 = C5 envelope (162 mm by 229 mm)
61
 * 29 = C3 envelope (324 mm by 458 mm)
62
 * 30 = C4 envelope (229 mm by 324 mm)
63
 * 31 = C6 envelope (114 mm by 162 mm)
64
 * 32 = C65 envelope (114 mm by 229 mm)
65
 * 33 = B4 envelope (250 mm by 353 mm)
66
 * 34 = B5 envelope (176 mm by 250 mm)
67
 * 35 = B6 envelope (176 mm by 125 mm)
68
 * 36 = Italy envelope (110 mm by 230 mm)
69
 * 37 = Monarch envelope (3.875 in. by 7.5 in.).
70
 * 38 = 6 3/4 envelope (3.625 in. by 6.5 in.)
71
 * 39 = US standard fanfold (14.875 in. by 11 in.)
72
 * 40 = German standard fanfold (8.5 in. by 12 in.)
73
 * 41 = German legal fanfold (8.5 in. by 13 in.)
74
 * 42 = ISO B4 (250 mm by 353 mm)
75
 * 43 = Japanese double postcard (200 mm by 148 mm)
76
 * 44 = Standard paper (9 in. by 11 in.)
77
 * 45 = Standard paper (10 in. by 11 in.)
78
 * 46 = Standard paper (15 in. by 11 in.)
79
 * 47 = Invite envelope (220 mm by 220 mm)
80
 * 50 = Letter extra paper (9.275 in. by 12 in.)
81
 * 51 = Legal extra paper (9.275 in. by 15 in.)
82
 * 52 = Tabloid extra paper (11.69 in. by 18 in.)
83
 * 53 = A4 extra paper (236 mm by 322 mm)
84
 * 54 = Letter transverse paper (8.275 in. by 11 in.)
85
 * 55 = A4 transverse paper (210 mm by 297 mm)
86
 * 56 = Letter extra transverse paper (9.275 in. by 12 in.)
87
 * 57 = SuperA/SuperA/A4 paper (227 mm by 356 mm)
88
 * 58 = SuperB/SuperB/A3 paper (305 mm by 487 mm)
89
 * 59 = Letter plus paper (8.5 in. by 12.69 in.)
90
 * 60 = A4 plus paper (210 mm by 330 mm)
91
 * 61 = A5 transverse paper (148 mm by 210 mm)
92
 * 62 = JIS B5 transverse paper (182 mm by 257 mm)
93
 * 63 = A3 extra paper (322 mm by 445 mm)
94
 * 64 = A5 extra paper (174 mm by 235 mm)
95
 * 65 = ISO B5 extra paper (201 mm by 276 mm)
96
 * 66 = A2 paper (420 mm by 594 mm)
97
 * 67 = A3 transverse paper (297 mm by 420 mm)
98
 * 68 = A3 extra transverse paper (322 mm by 445 mm)
99
 * </code>
100
 *
101
 * @category   PhpSpreadsheet
102
 *
103
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
104
 */
105
class PageSetup
106
{
107
    /* Paper size */
108
    const PAPERSIZE_LETTER = 1;
109
    const PAPERSIZE_LETTER_SMALL = 2;
110
    const PAPERSIZE_TABLOID = 3;
111
    const PAPERSIZE_LEDGER = 4;
112
    const PAPERSIZE_LEGAL = 5;
113
    const PAPERSIZE_STATEMENT = 6;
114
    const PAPERSIZE_EXECUTIVE = 7;
115
    const PAPERSIZE_A3 = 8;
116
    const PAPERSIZE_A4 = 9;
117
    const PAPERSIZE_A4_SMALL = 10;
118
    const PAPERSIZE_A5 = 11;
119
    const PAPERSIZE_B4 = 12;
120
    const PAPERSIZE_B5 = 13;
121
    const PAPERSIZE_FOLIO = 14;
122
    const PAPERSIZE_QUARTO = 15;
123
    const PAPERSIZE_STANDARD_1 = 16;
124
    const PAPERSIZE_STANDARD_2 = 17;
125
    const PAPERSIZE_NOTE = 18;
126
    const PAPERSIZE_NO9_ENVELOPE = 19;
127
    const PAPERSIZE_NO10_ENVELOPE = 20;
128
    const PAPERSIZE_NO11_ENVELOPE = 21;
129
    const PAPERSIZE_NO12_ENVELOPE = 22;
130
    const PAPERSIZE_NO14_ENVELOPE = 23;
131
    const PAPERSIZE_C = 24;
132
    const PAPERSIZE_D = 25;
133
    const PAPERSIZE_E = 26;
134
    const PAPERSIZE_DL_ENVELOPE = 27;
135
    const PAPERSIZE_C5_ENVELOPE = 28;
136
    const PAPERSIZE_C3_ENVELOPE = 29;
137
    const PAPERSIZE_C4_ENVELOPE = 30;
138
    const PAPERSIZE_C6_ENVELOPE = 31;
139
    const PAPERSIZE_C65_ENVELOPE = 32;
140
    const PAPERSIZE_B4_ENVELOPE = 33;
141
    const PAPERSIZE_B5_ENVELOPE = 34;
142
    const PAPERSIZE_B6_ENVELOPE = 35;
143
    const PAPERSIZE_ITALY_ENVELOPE = 36;
144
    const PAPERSIZE_MONARCH_ENVELOPE = 37;
145
    const PAPERSIZE_6_3_4_ENVELOPE = 38;
146
    const PAPERSIZE_US_STANDARD_FANFOLD = 39;
147
    const PAPERSIZE_GERMAN_STANDARD_FANFOLD = 40;
148
    const PAPERSIZE_GERMAN_LEGAL_FANFOLD = 41;
149
    const PAPERSIZE_ISO_B4 = 42;
150
    const PAPERSIZE_JAPANESE_DOUBLE_POSTCARD = 43;
151
    const PAPERSIZE_STANDARD_PAPER_1 = 44;
152
    const PAPERSIZE_STANDARD_PAPER_2 = 45;
153
    const PAPERSIZE_STANDARD_PAPER_3 = 46;
154
    const PAPERSIZE_INVITE_ENVELOPE = 47;
155
    const PAPERSIZE_LETTER_EXTRA_PAPER = 48;
156
    const PAPERSIZE_LEGAL_EXTRA_PAPER = 49;
157
    const PAPERSIZE_TABLOID_EXTRA_PAPER = 50;
158
    const PAPERSIZE_A4_EXTRA_PAPER = 51;
159
    const PAPERSIZE_LETTER_TRANSVERSE_PAPER = 52;
160
    const PAPERSIZE_A4_TRANSVERSE_PAPER = 53;
161
    const PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER = 54;
162
    const PAPERSIZE_SUPERA_SUPERA_A4_PAPER = 55;
163
    const PAPERSIZE_SUPERB_SUPERB_A3_PAPER = 56;
164
    const PAPERSIZE_LETTER_PLUS_PAPER = 57;
165
    const PAPERSIZE_A4_PLUS_PAPER = 58;
166
    const PAPERSIZE_A5_TRANSVERSE_PAPER = 59;
167
    const PAPERSIZE_JIS_B5_TRANSVERSE_PAPER = 60;
168
    const PAPERSIZE_A3_EXTRA_PAPER = 61;
169
    const PAPERSIZE_A5_EXTRA_PAPER = 62;
170
    const PAPERSIZE_ISO_B5_EXTRA_PAPER = 63;
171
    const PAPERSIZE_A2_PAPER = 64;
172
    const PAPERSIZE_A3_TRANSVERSE_PAPER = 65;
173
    const PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER = 66;
174
175
    /* Page orientation */
176
    const ORIENTATION_DEFAULT = 'default';
177
    const ORIENTATION_LANDSCAPE = 'landscape';
178
    const ORIENTATION_PORTRAIT = 'portrait';
179
180
    /* Print Range Set Method */
181
    const SETPRINTRANGE_OVERWRITE = 'O';
182
    const SETPRINTRANGE_INSERT = 'I';
183
184
    /**
185
     * Paper size.
186
     *
187
     * @var int
188
     */
189
    private $paperSize = self::PAPERSIZE_LETTER;
190
191
    /**
192
     * Orientation.
193
     *
194
     * @var string
195
     */
196
    private $orientation = self::ORIENTATION_DEFAULT;
197
198
    /**
199
     * Scale (Print Scale).
200
     *
201
     * Print scaling. Valid values range from 10 to 400
202
     * This setting is overridden when fitToWidth and/or fitToHeight are in use
203
     *
204
     * @var int?
205
     */
206
    private $scale = 100;
207
208
    /**
209
     * Fit To Page
210
     * Whether scale or fitToWith / fitToHeight applies.
211
     *
212
     * @var bool
213
     */
214
    private $fitToPage = false;
215
216
    /**
217
     * Fit To Height
218
     * Number of vertical pages to fit on.
219
     *
220
     * @var int?
221
     */
222
    private $fitToHeight = 1;
223
224
    /**
225
     * Fit To Width
226
     * Number of horizontal pages to fit on.
227
     *
228
     * @var int?
229
     */
230
    private $fitToWidth = 1;
231
232
    /**
233
     * Columns to repeat at left.
234
     *
235
     * @var array Containing start column and end column, empty array if option unset
236
     */
237
    private $columnsToRepeatAtLeft = ['', ''];
238
239
    /**
240
     * Rows to repeat at top.
241
     *
242
     * @var array Containing start row number and end row number, empty array if option unset
243
     */
244
    private $rowsToRepeatAtTop = [0, 0];
245
246
    /**
247
     * Center page horizontally.
248
     *
249
     * @var bool
250
     */
251
    private $horizontalCentered = false;
252
253
    /**
254
     * Center page vertically.
255
     *
256
     * @var bool
257
     */
258
    private $verticalCentered = false;
259
260
    /**
261
     * Print area.
262
     *
263
     * @var string
264
     */
265
    private $printArea = null;
266
267
    /**
268
     * First page number.
269
     *
270
     * @var int
271
     */
272
    private $firstPageNumber = null;
273
274
    /**
275
     * Create a new PageSetup.
276
     */
277 105
    public function __construct()
278
    {
279 105
    }
280
281
    /**
282
     * Get Paper Size.
283
     *
284
     * @return int
285
     */
286 61
    public function getPaperSize()
287
    {
288 61
        return $this->paperSize;
289
    }
290
291
    /**
292
     * Set Paper Size.
293
     *
294
     * @param int $pValue see self::PAPERSIZE_*
295
     *
296
     * @return PageSetup
297
     */
298 23
    public function setPaperSize($pValue)
299
    {
300 23
        $this->paperSize = $pValue;
301
302 23
        return $this;
303
    }
304
305
    /**
306
     * Get Orientation.
307
     *
308
     * @return string
309
     */
310 61
    public function getOrientation()
311
    {
312 61
        return $this->orientation;
313
    }
314
315
    /**
316
     * Set Orientation.
317
     *
318
     * @param string $pValue see self::ORIENTATION_*
319
     *
320
     * @return PageSetup
321
     */
322 23
    public function setOrientation($pValue)
323
    {
324 23
        $this->orientation = $pValue;
325
326 23
        return $this;
327
    }
328
329
    /**
330
     * Get Scale.
331
     *
332
     * @return int?
0 ignored issues
show
Documentation introduced by
The doc-type int? could not be parsed: Unknown type name "int?" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
333
     */
334 58
    public function getScale()
335
    {
336 58
        return $this->scale;
337
    }
338
339
    /**
340
     * Set Scale.
341
     * Print scaling. Valid values range from 10 to 400
342
     * This setting is overridden when fitToWidth and/or fitToHeight are in use.
343
     *
344
     * @param int|null $pValue
345
     * @param bool $pUpdate Update fitToPage so scaling applies rather than fitToHeight / fitToWidth
346
     *
347
     * @throws PhpSpreadsheetException
348
     *
349
     * @return PageSetup
350
     */
351 9
    public function setScale($pValue, $pUpdate = true)
352
    {
353
        // Microsoft Office Excel 2007 only allows setting a scale between 10 and 400 via the user interface,
354
        // but it is apparently still able to handle any scale >= 0, where 0 results in 100
355 9
        if (($pValue >= 0) || is_null($pValue)) {
356 9
            $this->scale = $pValue;
357 9
            if ($pUpdate) {
358 9
                $this->fitToPage = false;
359
            }
360
        } else {
361
            throw new PhpSpreadsheetException('Scale must not be negative');
362
        }
363
364 9
        return $this;
365
    }
366
367
    /**
368
     * Get Fit To Page.
369
     *
370
     * @return bool
371
     */
372 58
    public function getFitToPage()
373
    {
374 58
        return $this->fitToPage;
375
    }
376
377
    /**
378
     * Set Fit To Page.
379
     *
380
     * @param bool $pValue
381
     *
382
     * @return PageSetup
383
     */
384 4
    public function setFitToPage($pValue)
385
    {
386 4
        $this->fitToPage = $pValue;
387
388 4
        return $this;
389
    }
390
391
    /**
392
     * Get Fit To Height.
393
     *
394
     * @return int?
0 ignored issues
show
Documentation introduced by
The doc-type int? could not be parsed: Unknown type name "int?" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
395
     */
396 58
    public function getFitToHeight()
397
    {
398 58
        return $this->fitToHeight;
399
    }
400
401
    /**
402
     * Set Fit To Height.
403
     *
404
     * @param int|null $pValue
405
     * @param bool $pUpdate Update fitToPage so it applies rather than scaling
406
     *
407
     * @return PageSetup
408
     */
409 9
    public function setFitToHeight($pValue, $pUpdate = true)
410
    {
411 9
        $this->fitToHeight = $pValue;
412 9
        if ($pUpdate) {
413
            $this->fitToPage = true;
414
        }
415
416 9
        return $this;
417
    }
418
419
    /**
420
     * Get Fit To Width.
421
     *
422
     * @return int?
0 ignored issues
show
Documentation introduced by
The doc-type int? could not be parsed: Unknown type name "int?" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
423
     */
424 58
    public function getFitToWidth()
425
    {
426 58
        return $this->fitToWidth;
427
    }
428
429
    /**
430
     * Set Fit To Width.
431
     *
432
     * @param int|null $pValue
433
     * @param bool $pUpdate Update fitToPage so it applies rather than scaling
434
     *
435
     * @return PageSetup
436
     */
437 9
    public function setFitToWidth($pValue, $pUpdate = true)
438
    {
439 9
        $this->fitToWidth = $pValue;
440 9
        if ($pUpdate) {
441
            $this->fitToPage = true;
442
        }
443
444 9
        return $this;
445
    }
446
447
    /**
448
     * Is Columns to repeat at left set?
449
     *
450
     * @return bool
451
     */
452 58
    public function isColumnsToRepeatAtLeftSet()
453
    {
454 58
        if (is_array($this->columnsToRepeatAtLeft)) {
455 58
            if ($this->columnsToRepeatAtLeft[0] != '' && $this->columnsToRepeatAtLeft[1] != '') {
456
                return true;
457
            }
458
        }
459
460 58
        return false;
461
    }
462
463
    /**
464
     * Get Columns to repeat at left.
465
     *
466
     * @return array Containing start column and end column, empty array if option unset
467
     */
468
    public function getColumnsToRepeatAtLeft()
469
    {
470
        return $this->columnsToRepeatAtLeft;
471
    }
472
473
    /**
474
     * Set Columns to repeat at left.
475
     *
476
     * @param array $pValue Containing start column and end column, empty array if option unset
477
     *
478
     * @return PageSetup
479
     */
480
    public function setColumnsToRepeatAtLeft(array $pValue)
481
    {
482
        $this->columnsToRepeatAtLeft = $pValue;
483
484
        return $this;
485
    }
486
487
    /**
488
     * Set Columns to repeat at left by start and end.
489
     *
490
     * @param string $pStart eg: 'A'
491
     * @param string $pEnd eg: 'B'
492
     *
493
     * @return PageSetup
494
     */
495
    public function setColumnsToRepeatAtLeftByStartAndEnd($pStart, $pEnd)
496
    {
497
        $this->columnsToRepeatAtLeft = [$pStart, $pEnd];
498
499
        return $this;
500
    }
501
502
    /**
503
     * Is Rows to repeat at top set?
504
     *
505
     * @return bool
506
     */
507 62
    public function isRowsToRepeatAtTopSet()
508
    {
509 62
        if (is_array($this->rowsToRepeatAtTop)) {
510 62
            if ($this->rowsToRepeatAtTop[0] != 0 && $this->rowsToRepeatAtTop[1] != 0) {
511 1
                return true;
512
            }
513
        }
514
515 61
        return false;
516
    }
517
518
    /**
519
     * Get Rows to repeat at top.
520
     *
521
     * @return array Containing start column and end column, empty array if option unset
522
     */
523 1
    public function getRowsToRepeatAtTop()
524
    {
525 1
        return $this->rowsToRepeatAtTop;
526
    }
527
528
    /**
529
     * Set Rows to repeat at top.
530
     *
531
     * @param array $pValue Containing start column and end column, empty array if option unset
532
     *
533
     * @return PageSetup
534
     */
535 1
    public function setRowsToRepeatAtTop(array $pValue)
536
    {
537 1
        $this->rowsToRepeatAtTop = $pValue;
538
539 1
        return $this;
540
    }
541
542
    /**
543
     * Set Rows to repeat at top by start and end.
544
     *
545
     * @param int $pStart eg: 1
546
     * @param int $pEnd eg: 1
547
     *
548
     * @return PageSetup
549
     */
550 1
    public function setRowsToRepeatAtTopByStartAndEnd($pStart, $pEnd)
551
    {
552 1
        $this->rowsToRepeatAtTop = [$pStart, $pEnd];
553
554 1
        return $this;
555
    }
556
557
    /**
558
     * Get center page horizontally.
559
     *
560
     * @return bool
561
     */
562 58
    public function getHorizontalCentered()
563
    {
564 58
        return $this->horizontalCentered;
565
    }
566
567
    /**
568
     * Set center page horizontally.
569
     *
570
     * @param bool $value
571
     *
572
     * @return PageSetup
573
     */
574 4
    public function setHorizontalCentered($value)
575
    {
576 4
        $this->horizontalCentered = $value;
577
578 4
        return $this;
579
    }
580
581
    /**
582
     * Get center page vertically.
583
     *
584
     * @return bool
585
     */
586 58
    public function getVerticalCentered()
587
    {
588 58
        return $this->verticalCentered;
589
    }
590
591
    /**
592
     * Set center page vertically.
593
     *
594
     * @param bool $value
595
     *
596
     * @return PageSetup
597
     */
598 4
    public function setVerticalCentered($value)
599
    {
600 4
        $this->verticalCentered = $value;
601
602 4
        return $this;
603
    }
604
605
    /**
606
     * Get print area.
607
     *
608
     * @param int $index Identifier for a specific print area range if several ranges have been set
609
     *                            Default behaviour, or a index value of 0, will return all ranges as a comma-separated string
610
     *                            Otherwise, the specific range identified by the value of $index will be returned
611
     *                            Print areas are numbered from 1
612
     *
613
     * @throws PhpSpreadsheetException
614
     *
615
     * @return string
616
     */
617
    public function getPrintArea($index = 0)
618
    {
619
        if ($index == 0) {
620
            return $this->printArea;
621
        }
622
        $printAreas = explode(',', $this->printArea);
623
        if (isset($printAreas[$index - 1])) {
624
            return $printAreas[$index - 1];
625
        }
626
        throw new PhpSpreadsheetException('Requested Print Area does not exist');
627
    }
628
629
    /**
630
     * Is print area set?
631
     *
632
     * @param int $index Identifier for a specific print area range if several ranges have been set
633
     *                            Default behaviour, or an index value of 0, will identify whether any print range is set
634
     *                            Otherwise, existence of the range identified by the value of $index will be returned
635
     *                            Print areas are numbered from 1
636
     *
637
     * @return bool
638
     */
639 62
    public function isPrintAreaSet($index = 0)
640
    {
641 62
        if ($index == 0) {
642 62
            return !is_null($this->printArea);
643
        }
644
        $printAreas = explode(',', $this->printArea);
645
646
        return isset($printAreas[$index - 1]);
647
    }
648
649
    /**
650
     * Clear a print area.
651
     *
652
     * @param int $index Identifier for a specific print area range if several ranges have been set
653
     *                            Default behaviour, or an index value of 0, will clear all print ranges that are set
654
     *                            Otherwise, the range identified by the value of $index will be removed from the series
655
     *                            Print areas are numbered from 1
656
     *
657
     * @return PageSetup
658
     */
659
    public function clearPrintArea($index = 0)
660
    {
661
        if ($index == 0) {
662
            $this->printArea = null;
663
        } else {
664
            $printAreas = explode(',', $this->printArea);
665
            if (isset($printAreas[$index - 1])) {
666
                unset($printAreas[$index - 1]);
667
                $this->printArea = implode(',', $printAreas);
668
            }
669
        }
670
671
        return $this;
672
    }
673
674
    /**
675
     * Set print area. e.g. 'A1:D10' or 'A1:D10,G5:M20'.
676
     *
677
     * @param string $value
678
     * @param int $index Identifier for a specific print area range allowing several ranges to be set
679
     *                            When the method is "O"verwrite, then a positive integer index will overwrite that indexed
680
     *                                entry in the print areas list; a negative index value will identify which entry to
681
     *                                overwrite working bacward through the print area to the list, with the last entry as -1.
682
     *                                Specifying an index value of 0, will overwrite <b>all</b> existing print ranges.
683
     *                            When the method is "I"nsert, then a positive index will insert after that indexed entry in
684
     *                                the print areas list, while a negative index will insert before the indexed entry.
685
     *                                Specifying an index value of 0, will always append the new print range at the end of the
686
     *                                list.
687
     *                            Print areas are numbered from 1
688
     * @param string $method Determines the method used when setting multiple print areas
689
     *                            Default behaviour, or the "O" method, overwrites existing print area
690
     *                            The "I" method, inserts the new print area before any specified index, or at the end of the list
691
     *
692
     * @throws PhpSpreadsheetException
693
     *
694
     * @return PageSetup
695
     */
696
    public function setPrintArea($value, $index = 0, $method = self::SETPRINTRANGE_OVERWRITE)
697
    {
698 View Code Duplication
        if (strpos($value, '!') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
699
            throw new PhpSpreadsheetException('Cell coordinate must not specify a worksheet.');
700
        } elseif (strpos($value, ':') === false) {
701
            throw new PhpSpreadsheetException('Cell coordinate must be a range of cells.');
702
        } elseif (strpos($value, '$') !== false) {
703
            throw new PhpSpreadsheetException('Cell coordinate must not be absolute.');
704
        }
705
        $value = strtoupper($value);
706
707
        if ($method == self::SETPRINTRANGE_OVERWRITE) {
708
            if ($index == 0) {
709
                $this->printArea = $value;
710
            } else {
711
                $printAreas = explode(',', $this->printArea);
712
                if ($index < 0) {
713
                    $index = count($printAreas) - abs($index) + 1;
714
                }
715
                if (($index <= 0) || ($index > count($printAreas))) {
716
                    throw new PhpSpreadsheetException('Invalid index for setting print range.');
717
                }
718
                $printAreas[$index - 1] = $value;
719
                $this->printArea = implode(',', $printAreas);
720
            }
721
        } elseif ($method == self::SETPRINTRANGE_INSERT) {
722
            if ($index == 0) {
723
                $this->printArea .= ($this->printArea == '') ? $value : ',' . $value;
724
            } else {
725
                $printAreas = explode(',', $this->printArea);
726
                if ($index < 0) {
727
                    $index = abs($index) - 1;
728
                }
729
                if ($index > count($printAreas)) {
730
                    throw new PhpSpreadsheetException('Invalid index for setting print range.');
731
                }
732
                $printAreas = array_merge(array_slice($printAreas, 0, $index), [$value], array_slice($printAreas, $index));
733
                $this->printArea = implode(',', $printAreas);
734
            }
735
        } else {
736
            throw new PhpSpreadsheetException('Invalid method for setting print range.');
737
        }
738
739
        return $this;
740
    }
741
742
    /**
743
     * Add a new print area (e.g. 'A1:D10' or 'A1:D10,G5:M20') to the list of print areas.
744
     *
745
     * @param string $value
746
     * @param int $index Identifier for a specific print area range allowing several ranges to be set
747
     *                            A positive index will insert after that indexed entry in the print areas list, while a
748
     *                                negative index will insert before the indexed entry.
749
     *                                Specifying an index value of 0, will always append the new print range at the end of the
750
     *                                list.
751
     *                            Print areas are numbered from 1
752
     *
753
     * @throws PhpSpreadsheetException
754
     *
755
     * @return PageSetup
756
     */
757
    public function addPrintArea($value, $index = -1)
758
    {
759
        return $this->setPrintArea($value, $index, self::SETPRINTRANGE_INSERT);
760
    }
761
762
    /**
763
     * Set print area.
764
     *
765
     * @param int $column1 Column 1
766
     * @param int $row1 Row 1
767
     * @param int $column2 Column 2
768
     * @param int $row2 Row 2
769
     * @param int $index Identifier for a specific print area range allowing several ranges to be set
770
     *                                When the method is "O"verwrite, then a positive integer index will overwrite that indexed
771
     *                                    entry in the print areas list; a negative index value will identify which entry to
772
     *                                    overwrite working bacward through the print area to the list, with the last entry as -1.
773
     *                                    Specifying an index value of 0, will overwrite <b>all</b> existing print ranges.
774
     *                                When the method is "I"nsert, then a positive index will insert after that indexed entry in
775
     *                                    the print areas list, while a negative index will insert before the indexed entry.
776
     *                                    Specifying an index value of 0, will always append the new print range at the end of the
777
     *                                    list.
778
     *                                Print areas are numbered from 1
779
     * @param string $method Determines the method used when setting multiple print areas
780
     *                                Default behaviour, or the "O" method, overwrites existing print area
781
     *                                The "I" method, inserts the new print area before any specified index, or at the end of the list
782
     *
783
     * @throws PhpSpreadsheetException
784
     *
785
     * @return PageSetup
786
     */
787 View Code Duplication
    public function setPrintAreaByColumnAndRow($column1, $row1, $column2, $row2, $index = 0, $method = self::SETPRINTRANGE_OVERWRITE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
788
    {
789
        return $this->setPrintArea(
790
            Cell::stringFromColumnIndex($column1) . $row1 . ':' . Cell::stringFromColumnIndex($column2) . $row2,
791
            $index,
792
            $method
793
        );
794
    }
795
796
    /**
797
     * Add a new print area to the list of print areas.
798
     *
799
     * @param int $column1 Start Column for the print area
800
     * @param int $row1 Start Row for the print area
801
     * @param int $column2 End Column for the print area
802
     * @param int $row2 End Row for the print area
803
     * @param int $index Identifier for a specific print area range allowing several ranges to be set
804
     *                                A positive index will insert after that indexed entry in the print areas list, while a
805
     *                                    negative index will insert before the indexed entry.
806
     *                                    Specifying an index value of 0, will always append the new print range at the end of the
807
     *                                    list.
808
     *                                Print areas are numbered from 1
809
     *
810
     * @throws PhpSpreadsheetException
811
     *
812
     * @return PageSetup
813
     */
814 View Code Duplication
    public function addPrintAreaByColumnAndRow($column1, $row1, $column2, $row2, $index = -1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
815
    {
816
        return $this->setPrintArea(
817
            Cell::stringFromColumnIndex($column1) . $row1 . ':' . Cell::stringFromColumnIndex($column2) . $row2,
818
            $index,
819
            self::SETPRINTRANGE_INSERT
820
        );
821
    }
822
823
    /**
824
     * Get first page number.
825
     *
826
     * @return int
827
     */
828 56
    public function getFirstPageNumber()
829
    {
830 56
        return $this->firstPageNumber;
831
    }
832
833
    /**
834
     * Set first page number.
835
     *
836
     * @param int $value
837
     *
838
     * @return PageSetup
839
     */
840
    public function setFirstPageNumber($value)
841
    {
842
        $this->firstPageNumber = $value;
843
844
        return $this;
845
    }
846
847
    /**
848
     * Reset first page number.
849
     *
850
     * @return PageSetup
851
     */
852
    public function resetFirstPageNumber()
853
    {
854
        return $this->setFirstPageNumber(null);
855
    }
856
857
    /**
858
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
859
     */
860 View Code Duplication
    public function __clone()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
861
    {
862
        $vars = get_object_vars($this);
863
        foreach ($vars as $key => $value) {
864
            if (is_object($value)) {
865
                $this->$key = clone $value;
866
            } else {
867
                $this->$key = $value;
868
            }
869
        }
870
    }
871
}
872