Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace OpenSpout\Common\Entity\Style;
6
 
7
use OpenSpout\Common\Exception\InvalidArgumentException;
8
 
9
/**
10
 * Represents a style to be applied to a cell.
11
 */
12
final class Style
13
{
14
    /**
15
     * Default values.
16
     */
17
    public const DEFAULT_FONT_SIZE = 11;
18
    public const DEFAULT_FONT_COLOR = Color::BLACK;
19
    public const DEFAULT_FONT_NAME = 'Arial';
20
 
21
    /** @var int Style ID */
22
    private int $id = -1;
23
 
24
    /** @var bool Whether the font should be bold */
25
    private bool $fontBold = false;
26
 
27
    /** @var bool Whether the bold property was set */
28
    private bool $hasSetFontBold = false;
29
 
30
    /** @var bool Whether the font should be italic */
31
    private bool $fontItalic = false;
32
 
33
    /** @var bool Whether the italic property was set */
34
    private bool $hasSetFontItalic = false;
35
 
36
    /** @var bool Whether the font should be underlined */
37
    private bool $fontUnderline = false;
38
 
39
    /** @var bool Whether the underline property was set */
40
    private bool $hasSetFontUnderline = false;
41
 
42
    /** @var bool Whether the font should be struck through */
43
    private bool $fontStrikethrough = false;
44
 
45
    /** @var bool Whether the strikethrough property was set */
46
    private bool $hasSetFontStrikethrough = false;
47
 
48
    /** @var int Font size */
49
    private int $fontSize = self::DEFAULT_FONT_SIZE;
50
 
51
    /** @var bool Whether the font size property was set */
52
    private bool $hasSetFontSize = false;
53
 
54
    /** @var string Font color */
55
    private string $fontColor = self::DEFAULT_FONT_COLOR;
56
 
57
    /** @var bool Whether the font color property was set */
58
    private bool $hasSetFontColor = false;
59
 
60
    /** @var string Font name */
61
    private string $fontName = self::DEFAULT_FONT_NAME;
62
 
63
    /** @var bool Whether the font name property was set */
64
    private bool $hasSetFontName = false;
65
 
66
    /** @var bool Whether specific font properties should be applied */
67
    private bool $shouldApplyFont = false;
68
 
69
    /** @var bool Whether specific cell alignment should be applied */
70
    private bool $shouldApplyCellAlignment = false;
71
 
72
    /** @var string Cell alignment */
73
    private string $cellAlignment;
74
 
75
    /** @var bool Whether the cell alignment property was set */
76
    private bool $hasSetCellAlignment = false;
77
 
78
    /** @var bool Whether specific cell vertical alignment should be applied */
79
    private bool $shouldApplyCellVerticalAlignment = false;
80
 
81
    /** @var string Cell vertical alignment */
82
    private string $cellVerticalAlignment;
83
 
84
    /** @var bool Whether the cell vertical alignment property was set */
85
    private bool $hasSetCellVerticalAlignment = false;
86
 
87
    /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
88
    private bool $shouldWrapText = false;
89
 
90
    /** @var bool Whether the wrap text property was set */
91
    private bool $hasSetWrapText = false;
92
 
1441 ariadna 93
    /** @var int Text rotation */
94
    private int $textRotation = 0;
95
 
96
    /** @var bool Whether the text rotation property was set */
97
    private bool $hasSetTextRotation = false;
98
 
1 efrain 99
    /** @var bool Whether the cell should shrink to fit to content */
100
    private bool $shouldShrinkToFit = false;
101
 
102
    /** @var bool Whether the shouldShrinkToFit text property was set */
103
    private bool $hasSetShrinkToFit = false;
104
 
105
    private ?Border $border = null;
106
 
107
    /** @var null|string Background color */
108
    private ?string $backgroundColor = null;
109
 
110
    /** @var null|string Format */
111
    private ?string $format = null;
112
 
113
    private bool $isRegistered = false;
114
 
115
    private bool $isEmpty = true;
116
 
117
    public function __sleep(): array
118
    {
119
        $vars = get_object_vars($this);
120
        unset($vars['id'], $vars['isRegistered']);
121
 
122
        return array_keys($vars);
123
    }
124
 
125
    public function getId(): int
126
    {
127
        \assert(0 <= $this->id);
128
 
129
        return $this->id;
130
    }
131
 
132
    public function setId(int $id): self
133
    {
134
        $this->id = $id;
135
 
136
        return $this;
137
    }
138
 
139
    public function getBorder(): ?Border
140
    {
141
        return $this->border;
142
    }
143
 
144
    public function setBorder(Border $border): self
145
    {
146
        $this->border = $border;
147
        $this->isEmpty = false;
148
 
149
        return $this;
150
    }
151
 
152
    public function isFontBold(): bool
153
    {
154
        return $this->fontBold;
155
    }
156
 
157
    public function setFontBold(): self
158
    {
159
        $this->fontBold = true;
160
        $this->hasSetFontBold = true;
161
        $this->shouldApplyFont = true;
162
        $this->isEmpty = false;
163
 
164
        return $this;
165
    }
166
 
167
    public function hasSetFontBold(): bool
168
    {
169
        return $this->hasSetFontBold;
170
    }
171
 
172
    public function isFontItalic(): bool
173
    {
174
        return $this->fontItalic;
175
    }
176
 
177
    public function setFontItalic(): self
178
    {
179
        $this->fontItalic = true;
180
        $this->hasSetFontItalic = true;
181
        $this->shouldApplyFont = true;
182
        $this->isEmpty = false;
183
 
184
        return $this;
185
    }
186
 
187
    public function hasSetFontItalic(): bool
188
    {
189
        return $this->hasSetFontItalic;
190
    }
191
 
192
    public function isFontUnderline(): bool
193
    {
194
        return $this->fontUnderline;
195
    }
196
 
197
    public function setFontUnderline(): self
198
    {
199
        $this->fontUnderline = true;
200
        $this->hasSetFontUnderline = true;
201
        $this->shouldApplyFont = true;
202
        $this->isEmpty = false;
203
 
204
        return $this;
205
    }
206
 
207
    public function hasSetFontUnderline(): bool
208
    {
209
        return $this->hasSetFontUnderline;
210
    }
211
 
212
    public function isFontStrikethrough(): bool
213
    {
214
        return $this->fontStrikethrough;
215
    }
216
 
217
    public function setFontStrikethrough(): self
218
    {
219
        $this->fontStrikethrough = true;
220
        $this->hasSetFontStrikethrough = true;
221
        $this->shouldApplyFont = true;
222
        $this->isEmpty = false;
223
 
224
        return $this;
225
    }
226
 
227
    public function hasSetFontStrikethrough(): bool
228
    {
229
        return $this->hasSetFontStrikethrough;
230
    }
231
 
232
    public function getFontSize(): int
233
    {
234
        return $this->fontSize;
235
    }
236
 
237
    /**
238
     * @param int $fontSize Font size, in pixels
239
     */
240
    public function setFontSize(int $fontSize): self
241
    {
242
        $this->fontSize = $fontSize;
243
        $this->hasSetFontSize = true;
244
        $this->shouldApplyFont = true;
245
        $this->isEmpty = false;
246
 
247
        return $this;
248
    }
249
 
250
    public function hasSetFontSize(): bool
251
    {
252
        return $this->hasSetFontSize;
253
    }
254
 
255
    public function getFontColor(): string
256
    {
257
        return $this->fontColor;
258
    }
259
 
260
    /**
261
     * Sets the font color.
262
     *
263
     * @param string $fontColor ARGB color (@see Color)
264
     */
265
    public function setFontColor(string $fontColor): self
266
    {
267
        $this->fontColor = $fontColor;
268
        $this->hasSetFontColor = true;
269
        $this->shouldApplyFont = true;
270
        $this->isEmpty = false;
271
 
272
        return $this;
273
    }
274
 
275
    public function hasSetFontColor(): bool
276
    {
277
        return $this->hasSetFontColor;
278
    }
279
 
280
    public function getFontName(): string
281
    {
282
        return $this->fontName;
283
    }
284
 
285
    /**
286
     * @param string $fontName Name of the font to use
287
     */
288
    public function setFontName(string $fontName): self
289
    {
290
        $this->fontName = $fontName;
291
        $this->hasSetFontName = true;
292
        $this->shouldApplyFont = true;
293
        $this->isEmpty = false;
294
 
295
        return $this;
296
    }
297
 
298
    public function hasSetFontName(): bool
299
    {
300
        return $this->hasSetFontName;
301
    }
302
 
303
    public function getCellAlignment(): string
304
    {
305
        return $this->cellAlignment;
306
    }
307
 
308
    public function getCellVerticalAlignment(): string
309
    {
310
        return $this->cellVerticalAlignment;
311
    }
312
 
313
    /**
314
     * @param string $cellAlignment The cell alignment
315
     */
316
    public function setCellAlignment(string $cellAlignment): self
317
    {
318
        if (!CellAlignment::isValid($cellAlignment)) {
319
            throw new InvalidArgumentException('Invalid cell alignment value');
320
        }
321
 
322
        $this->cellAlignment = $cellAlignment;
323
        $this->hasSetCellAlignment = true;
324
        $this->shouldApplyCellAlignment = true;
325
        $this->isEmpty = false;
326
 
327
        return $this;
328
    }
329
 
330
    /**
331
     * @param string $cellVerticalAlignment The cell vertical alignment
332
     */
333
    public function setCellVerticalAlignment(string $cellVerticalAlignment): self
334
    {
335
        if (!CellVerticalAlignment::isValid($cellVerticalAlignment)) {
336
            throw new InvalidArgumentException('Invalid cell vertical alignment value');
337
        }
338
 
339
        $this->cellVerticalAlignment = $cellVerticalAlignment;
340
        $this->hasSetCellVerticalAlignment = true;
341
        $this->shouldApplyCellVerticalAlignment = true;
342
        $this->isEmpty = false;
343
 
344
        return $this;
345
    }
346
 
347
    public function hasSetCellAlignment(): bool
348
    {
349
        return $this->hasSetCellAlignment;
350
    }
351
 
352
    public function hasSetCellVerticalAlignment(): bool
353
    {
354
        return $this->hasSetCellVerticalAlignment;
355
    }
356
 
357
    /**
358
     * @return bool Whether specific cell alignment should be applied
359
     */
360
    public function shouldApplyCellAlignment(): bool
361
    {
362
        return $this->shouldApplyCellAlignment;
363
    }
364
 
365
    public function shouldApplyCellVerticalAlignment(): bool
366
    {
367
        return $this->shouldApplyCellVerticalAlignment;
368
    }
369
 
370
    public function shouldWrapText(): bool
371
    {
372
        return $this->shouldWrapText;
373
    }
374
 
375
    /**
376
     * @param bool $shouldWrap Should the text be wrapped
377
     */
378
    public function setShouldWrapText(bool $shouldWrap = true): self
379
    {
380
        $this->shouldWrapText = $shouldWrap;
381
        $this->hasSetWrapText = true;
382
        $this->isEmpty = false;
383
 
384
        return $this;
385
    }
386
 
387
    public function hasSetWrapText(): bool
388
    {
389
        return $this->hasSetWrapText;
390
    }
391
 
1441 ariadna 392
    public function textRotation(): int
393
    {
394
        return $this->textRotation;
395
    }
396
 
1 efrain 397
    /**
1441 ariadna 398
     * @param int $rotation Rotate text
399
     */
400
    public function setTextRotation(int $rotation): self
401
    {
402
        $this->textRotation = $rotation;
403
        $this->hasSetTextRotation = true;
404
        $this->isEmpty = false;
405
 
406
        return $this;
407
    }
408
 
409
    public function hasSetTextRotation(): bool
410
    {
411
        return $this->hasSetTextRotation;
412
    }
413
 
414
    /**
1 efrain 415
     * @return bool Whether specific font properties should be applied
416
     */
417
    public function shouldApplyFont(): bool
418
    {
419
        return $this->shouldApplyFont;
420
    }
421
 
422
    /**
423
     * Sets the background color.
424
     *
425
     * @param string $color ARGB color (@see Color)
426
     */
427
    public function setBackgroundColor(string $color): self
428
    {
429
        $this->backgroundColor = $color;
430
        $this->isEmpty = false;
431
 
432
        return $this;
433
    }
434
 
435
    public function getBackgroundColor(): ?string
436
    {
437
        return $this->backgroundColor;
438
    }
439
 
440
    /**
441
     * Sets format.
442
     */
443
    public function setFormat(string $format): self
444
    {
445
        $this->format = $format;
446
        $this->isEmpty = false;
447
 
448
        return $this;
449
    }
450
 
451
    public function getFormat(): ?string
452
    {
453
        return $this->format;
454
    }
455
 
456
    public function isRegistered(): bool
457
    {
458
        return $this->isRegistered;
459
    }
460
 
461
    public function markAsRegistered(?int $id): void
462
    {
463
        $this->setId($id);
464
        $this->isRegistered = true;
465
    }
466
 
467
    public function isEmpty(): bool
468
    {
469
        return $this->isEmpty;
470
    }
471
 
472
    /**
473
     * Sets should shrink to fit.
474
     */
475
    public function setShouldShrinkToFit(bool $shrinkToFit = true): self
476
    {
477
        $this->hasSetShrinkToFit = true;
478
        $this->shouldShrinkToFit = $shrinkToFit;
479
 
480
        return $this;
481
    }
482
 
483
    /**
484
     * @return bool Whether format should be applied
485
     */
486
    public function shouldShrinkToFit(): bool
487
    {
488
        return $this->shouldShrinkToFit;
489
    }
490
 
491
    public function hasSetShrinkToFit(): bool
492
    {
493
        return $this->hasSetShrinkToFit;
494
    }
495
}