Passed
Push — master ( 216db0...8d8bc2 )
by
unknown
15:58 queued 08:00
created

HeaderFooter::setImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
/**
6
 * <code>
7
 * Header/Footer Formatting Syntax taken from Office Open XML Part 4 - Markup Language Reference, page 1970:.
8
 *
9
 * There are a number of formatting codes that can be written inline with the actual header / footer text, which
10
 * affect the formatting in the header or footer.
11
 *
12
 * Example: This example shows the text "Center Bold Header" on the first line (center section), and the date on
13
 * the second line (center section).
14
 *         &CCenter &"-,Bold"Bold&"-,Regular"Header_x000A_&D
15
 *
16
 * General Rules:
17
 * There is no required order in which these codes must appear.
18
 *
19
 * The first occurrence of the following codes turns the formatting ON, the second occurrence turns it OFF again:
20
 * - strikethrough
21
 * - superscript
22
 * - subscript
23
 * Superscript and subscript cannot both be ON at same time. Whichever comes first wins and the other is ignored,
24
 * while the first is ON.
25
 * &L - code for "left section" (there are three header / footer locations, "left", "center", and "right"). When
26
 * two or more occurrences of this section marker exist, the contents from all markers are concatenated, in the
27
 * order of appearance, and placed into the left section.
28
 * &P - code for "current page #"
29
 * &N - code for "total pages"
30
 * &font size - code for "text font size", where font size is a font size in points.
31
 * &K - code for "text font color"
32
 * RGB Color is specified as RRGGBB
33
 * Theme Color is specifed as TTSNN where TT is the theme color Id, S is either "+" or "-" of the tint/shade
34
 * value, NN is the tint/shade value.
35
 * &S - code for "text strikethrough" on / off
36
 * &X - code for "text super script" on / off
37
 * &Y - code for "text subscript" on / off
38
 * &C - code for "center section". When two or more occurrences of this section marker exist, the contents
39
 * from all markers are concatenated, in the order of appearance, and placed into the center section.
40
 *
41
 * &D - code for "date"
42
 * &T - code for "time"
43
 * &G - code for "picture as background"
44
 * &U - code for "text single underline"
45
 * &E - code for "double underline"
46
 * &R - code for "right section". When two or more occurrences of this section marker exist, the contents
47
 * from all markers are concatenated, in the order of appearance, and placed into the right section.
48
 * &Z - code for "this workbook's file path"
49
 * &F - code for "this workbook's file name"
50
 * &A - code for "sheet tab name"
51
 * &+ - code for add to page #.
52
 * &- - code for subtract from page #.
53
 * &"font name,font type" - code for "text font name" and "text font type", where font name and font type
54
 * are strings specifying the name and type of the font, separated by a comma. When a hyphen appears in font
55
 * name, it means "none specified". Both of font name and font type can be localized values.
56
 * &"-,Bold" - code for "bold font style"
57
 * &B - also means "bold font style".
58
 * &"-,Regular" - code for "regular font style"
59
 * &"-,Italic" - code for "italic font style"
60
 * &I - also means "italic font style"
61
 * &"-,Bold Italic" code for "bold italic font style"
62
 * &O - code for "outline style"
63
 * &H - code for "shadow style"
64
 * </code>
65
 */
66
class HeaderFooter
67
{
68
    // Header/footer image location
69
    const IMAGE_HEADER_LEFT = 'LH';
70
    const IMAGE_HEADER_LEFT_ODD = 'LH';
71
    const IMAGE_HEADER_LEFT_FIRST = 'LHFIRST';
72
    const IMAGE_HEADER_LEFT_EVEN = 'LHEVEN';
73
    const IMAGE_HEADER_CENTER = 'CH';
74
    const IMAGE_HEADER_CENTER_ODD = 'CH';
75
    const IMAGE_HEADER_CENTER_FIRST = 'CHFIRST';
76
    const IMAGE_HEADER_CENTER_EVEN = 'CHEVEN';
77
    const IMAGE_HEADER_RIGHT = 'RH';
78
    const IMAGE_HEADER_RIGHT_ODD = 'RH';
79
    const IMAGE_HEADER_RIGHT_FIRST = 'RHFIRST';
80
    const IMAGE_HEADER_RIGHT_EVEN = 'RHEVEN';
81
    const IMAGE_FOOTER_LEFT = 'LF';
82
    const IMAGE_FOOTER_LEFT_ODD = 'LF';
83
    const IMAGE_FOOTER_LEFT_FIRST = 'LFFIRST';
84
    const IMAGE_FOOTER_LEFT_EVEN = 'LFEVEN';
85
    const IMAGE_FOOTER_CENTER = 'CF';
86
    const IMAGE_FOOTER_CENTER_ODD = 'CF';
87
    const IMAGE_FOOTER_CENTER_FIRST = 'CFFIRST';
88
    const IMAGE_FOOTER_CENTER_EVEN = 'CFEVEN';
89
    const IMAGE_FOOTER_RIGHT = 'RF';
90
    const IMAGE_FOOTER_RIGHT_ODD = 'RF';
91
    const IMAGE_FOOTER_RIGHT_FIRST = 'RFFIRST';
92
    const IMAGE_FOOTER_RIGHT_EVEN = 'RFEVEN';
93
94
    /**
95
     * OddHeader.
96
     */
97
    private string $oddHeader = '';
98
99
    /**
100
     * OddFooter.
101
     */
102
    private string $oddFooter = '';
103
104
    /**
105
     * EvenHeader.
106
     */
107
    private string $evenHeader = '';
108
109
    /**
110
     * EvenFooter.
111
     */
112
    private string $evenFooter = '';
113
114
    /**
115
     * FirstHeader.
116
     */
117
    private string $firstHeader = '';
118
119
    /**
120
     * FirstFooter.
121
     */
122
    private string $firstFooter = '';
123
124
    /**
125
     * Different header for Odd/Even, defaults to false.
126
     */
127
    private bool $differentOddEven = false;
128
129
    /**
130
     * Different header for first page, defaults to false.
131
     */
132
    private bool $differentFirst = false;
133
134
    /**
135
     * Scale with document, defaults to true.
136
     */
137
    private bool $scaleWithDocument = true;
138
139
    /**
140
     * Align with margins, defaults to true.
141
     */
142
    private bool $alignWithMargins = true;
143
144
    /**
145
     * Header/footer images.
146
     *
147
     * @var HeaderFooterDrawing[]
148
     */
149
    private array $headerFooterImages = [];
150
151
    /**
152
     * Create a new HeaderFooter.
153
     */
154 10657
    public function __construct()
155
    {
156 10657
    }
157
158
    /**
159
     * Get OddHeader.
160
     */
161 504
    public function getOddHeader(): string
162
    {
163 504
        return $this->oddHeader;
164
    }
165
166
    /**
167
     * Set OddHeader.
168
     *
169
     * @return $this
170
     */
171 140
    public function setOddHeader(string $oddHeader): static
172
    {
173 140
        $this->oddHeader = $oddHeader;
174
175 140
        return $this;
176
    }
177
178
    /**
179
     * Get OddFooter.
180
     */
181 504
    public function getOddFooter(): string
182
    {
183 504
        return $this->oddFooter;
184
    }
185
186
    /**
187
     * Set OddFooter.
188
     *
189
     * @return $this
190
     */
191 142
    public function setOddFooter(string $oddFooter): static
192
    {
193 142
        $this->oddFooter = $oddFooter;
194
195 142
        return $this;
196
    }
197
198
    /**
199
     * Get EvenHeader.
200
     */
201 436
    public function getEvenHeader(): string
202
    {
203 436
        return $this->evenHeader;
204
    }
205
206
    /**
207
     * Set EvenHeader.
208
     *
209
     * @return $this
210
     */
211 124
    public function setEvenHeader(string $eventHeader): static
212
    {
213 124
        $this->evenHeader = $eventHeader;
214
215 124
        return $this;
216
    }
217
218
    /**
219
     * Get EvenFooter.
220
     */
221 436
    public function getEvenFooter(): string
222
    {
223 436
        return $this->evenFooter;
224
    }
225
226
    /**
227
     * Set EvenFooter.
228
     *
229
     * @return $this
230
     */
231 126
    public function setEvenFooter(string $evenFooter): static
232
    {
233 126
        $this->evenFooter = $evenFooter;
234
235 126
        return $this;
236
    }
237
238
    /**
239
     * Get FirstHeader.
240
     */
241 436
    public function getFirstHeader(): string
242
    {
243 436
        return $this->firstHeader;
244
    }
245
246
    /**
247
     * Set FirstHeader.
248
     *
249
     * @return $this
250
     */
251 48
    public function setFirstHeader(string $firstHeader): static
252
    {
253 48
        $this->firstHeader = $firstHeader;
254
255 48
        return $this;
256
    }
257
258
    /**
259
     * Get FirstFooter.
260
     */
261 436
    public function getFirstFooter(): string
262
    {
263 436
        return $this->firstFooter;
264
    }
265
266
    /**
267
     * Set FirstFooter.
268
     *
269
     * @return $this
270
     */
271 48
    public function setFirstFooter(string $firstFooter): static
272
    {
273 48
        $this->firstFooter = $firstFooter;
274
275 48
        return $this;
276
    }
277
278
    /**
279
     * Get DifferentOddEven.
280
     */
281 23
    public function getDifferentOddEven(): bool
282
    {
283 23
        return $this->differentOddEven;
284
    }
285
286
    /**
287
     * Set DifferentOddEven.
288
     *
289
     * @return $this
290
     */
291 49
    public function setDifferentOddEven(bool $differentOddEvent): static
292
    {
293 49
        $this->differentOddEven = $differentOddEvent;
294
295 49
        return $this;
296
    }
297
298
    /**
299
     * Get DifferentFirst.
300
     */
301 22
    public function getDifferentFirst(): bool
302
    {
303 22
        return $this->differentFirst;
304
    }
305
306
    /**
307
     * Set DifferentFirst.
308
     *
309
     * @return $this
310
     */
311 48
    public function setDifferentFirst(bool $differentFirst): static
312
    {
313 48
        $this->differentFirst = $differentFirst;
314
315 48
        return $this;
316
    }
317
318
    /**
319
     * Get ScaleWithDocument.
320
     */
321 21
    public function getScaleWithDocument(): bool
322
    {
323 21
        return $this->scaleWithDocument;
324
    }
325
326
    /**
327
     * Set ScaleWithDocument.
328
     *
329
     * @return $this
330
     */
331 48
    public function setScaleWithDocument(bool $scaleWithDocument): static
332
    {
333 48
        $this->scaleWithDocument = $scaleWithDocument;
334
335 48
        return $this;
336
    }
337
338
    /**
339
     * Get AlignWithMargins.
340
     */
341 21
    public function getAlignWithMargins(): bool
342
    {
343 21
        return $this->alignWithMargins;
344
    }
345
346
    /**
347
     * Set AlignWithMargins.
348
     *
349
     * @return $this
350
     */
351 48
    public function setAlignWithMargins(bool $alignWithMargins): static
352
    {
353 48
        $this->alignWithMargins = $alignWithMargins;
354
355 48
        return $this;
356
    }
357
358
    /**
359
     * Add header/footer image.
360
     *
361
     * @return $this
362
     */
363 2
    public function addImage(HeaderFooterDrawing $image, string $location = self::IMAGE_HEADER_LEFT): static
364
    {
365 2
        $this->headerFooterImages[$location] = $image;
366
367 2
        return $this;
368
    }
369
370
    /**
371
     * Remove header/footer image.
372
     *
373
     * @return $this
374
     */
375
    public function removeImage(string $location = self::IMAGE_HEADER_LEFT): static
376
    {
377
        if (isset($this->headerFooterImages[$location])) {
378
            unset($this->headerFooterImages[$location]);
379
        }
380
381
        return $this;
382
    }
383
384
    /**
385
     * Set header/footer images.
386
     *
387
     * @param HeaderFooterDrawing[] $images
388
     *
389
     * @return $this
390
     */
391 3
    public function setImages(array $images): static
392
    {
393 3
        $this->headerFooterImages = $images;
394
395 3
        return $this;
396
    }
397
398
    private const IMAGE_SORT_ORDER = [
399
        self::IMAGE_HEADER_LEFT,
400
        self::IMAGE_HEADER_LEFT_FIRST,
401
        self::IMAGE_HEADER_LEFT_EVEN,
402
        self::IMAGE_HEADER_CENTER,
403
        self::IMAGE_HEADER_CENTER_FIRST,
404
        self::IMAGE_HEADER_CENTER_EVEN,
405
        self::IMAGE_HEADER_RIGHT,
406
        self::IMAGE_HEADER_RIGHT_FIRST,
407
        self::IMAGE_HEADER_RIGHT_EVEN,
408
        self::IMAGE_FOOTER_LEFT,
409
        self::IMAGE_FOOTER_LEFT_FIRST,
410
        self::IMAGE_FOOTER_LEFT_EVEN,
411
        self::IMAGE_FOOTER_CENTER,
412
        self::IMAGE_FOOTER_CENTER_FIRST,
413
        self::IMAGE_FOOTER_CENTER_EVEN,
414
        self::IMAGE_FOOTER_RIGHT,
415
        self::IMAGE_FOOTER_RIGHT_FIRST,
416
        self::IMAGE_FOOTER_RIGHT_EVEN,
417
    ];
418
419
    /**
420
     * Get header/footer images.
421
     *
422
     * @return HeaderFooterDrawing[]
423
     */
424 437
    public function getImages(): array
425
    {
426
        // Sort array - not sure why needed
427 437
        $images = [];
428 437
        foreach (self::IMAGE_SORT_ORDER as $key) {
429 437
            if (isset($this->headerFooterImages[$key])) {
430 4
                $images[$key] = $this->headerFooterImages[$key];
431
            }
432
        }
433 437
        $this->headerFooterImages = $images;
434
435 437
        return $this->headerFooterImages;
436
    }
437
438
    /**
439
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
440
     */
441 1
    public function __clone()
442
    {
443 1
        $vars = get_object_vars($this);
444 1
        foreach ($vars as $key => $value) {
445 1
            if (is_object($value)) {
446
                $this->$key = clone $value;
447
            } else {
448 1
                $this->$key = $value;
449
            }
450
        }
451
    }
452
}
453