Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet;
4
 
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\Helper\Size;
7
use PhpOffice\PhpSpreadsheet\RichText\RichText;
8
use PhpOffice\PhpSpreadsheet\Shared\Drawing as SharedDrawing;
9
use PhpOffice\PhpSpreadsheet\Style\Alignment;
10
use PhpOffice\PhpSpreadsheet\Style\Color;
11
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
12
use Stringable;
13
 
14
class Comment implements IComparable, Stringable
15
{
16
    /**
17
     * Author.
18
     */
19
    private string $author;
20
 
21
    /**
22
     * Rich text comment.
23
     */
24
    private RichText $text;
25
 
26
    /**
27
     * Comment width (CSS style, i.e. XXpx or YYpt).
28
     */
29
    private string $width = '96pt';
30
 
31
    /**
32
     * Left margin (CSS style, i.e. XXpx or YYpt).
33
     */
34
    private string $marginLeft = '59.25pt';
35
 
36
    /**
37
     * Top margin (CSS style, i.e. XXpx or YYpt).
38
     */
39
    private string $marginTop = '1.5pt';
40
 
41
    /**
42
     * Visible.
43
     */
44
    private bool $visible = false;
45
 
46
    /**
47
     * Comment height (CSS style, i.e. XXpx or YYpt).
48
     */
49
    private string $height = '55.5pt';
50
 
51
    /**
52
     * Comment fill color.
53
     */
54
    private Color $fillColor;
55
 
56
    /**
57
     * Alignment.
58
     */
59
    private string $alignment;
60
 
61
    /**
62
     * Background image in comment.
63
     */
64
    private Drawing $backgroundImage;
65
 
66
    public const TEXTBOX_DIRECTION_RTL = 'rtl';
67
    public const TEXTBOX_DIRECTION_LTR = 'ltr';
68
    // MS uses 'auto' in xml but 'context' in UI
69
    public const TEXTBOX_DIRECTION_AUTO = 'auto';
70
    public const TEXTBOX_DIRECTION_CONTEXT = 'auto';
71
 
72
    private string $textboxDirection = '';
73
 
74
    /**
75
     * Create a new Comment.
76
     */
77
    public function __construct()
78
    {
79
        // Initialise variables
80
        $this->author = 'Author';
81
        $this->text = new RichText();
82
        $this->fillColor = new Color('FFFFFFE1');
83
        $this->alignment = Alignment::HORIZONTAL_GENERAL;
84
        $this->backgroundImage = new Drawing();
85
    }
86
 
87
    /**
88
     * Get Author.
89
     */
90
    public function getAuthor(): string
91
    {
92
        return $this->author;
93
    }
94
 
95
    /**
96
     * Set Author.
97
     */
98
    public function setAuthor(string $author): self
99
    {
100
        $this->author = $author;
101
 
102
        return $this;
103
    }
104
 
105
    /**
106
     * Get Rich text comment.
107
     */
108
    public function getText(): RichText
109
    {
110
        return $this->text;
111
    }
112
 
113
    /**
114
     * Set Rich text comment.
115
     */
116
    public function setText(RichText $text): self
117
    {
118
        $this->text = $text;
119
 
120
        return $this;
121
    }
122
 
123
    /**
124
     * Get comment width (CSS style, i.e. XXpx or YYpt).
125
     */
126
    public function getWidth(): string
127
    {
128
        return $this->width;
129
    }
130
 
131
    /**
132
     * Set comment width (CSS style, i.e. XXpx or YYpt). Default unit is pt.
133
     */
134
    public function setWidth(string $width): self
135
    {
136
        $width = new Size($width);
137
        if ($width->valid()) {
138
            $this->width = (string) $width;
139
        }
140
 
141
        return $this;
142
    }
143
 
144
    /**
145
     * Get comment height (CSS style, i.e. XXpx or YYpt).
146
     */
147
    public function getHeight(): string
148
    {
149
        return $this->height;
150
    }
151
 
152
    /**
153
     * Set comment height (CSS style, i.e. XXpx or YYpt). Default unit is pt.
154
     */
155
    public function setHeight(string $height): self
156
    {
157
        $height = new Size($height);
158
        if ($height->valid()) {
159
            $this->height = (string) $height;
160
        }
161
 
162
        return $this;
163
    }
164
 
165
    /**
166
     * Get left margin (CSS style, i.e. XXpx or YYpt).
167
     */
168
    public function getMarginLeft(): string
169
    {
170
        return $this->marginLeft;
171
    }
172
 
173
    /**
174
     * Set left margin (CSS style, i.e. XXpx or YYpt). Default unit is pt.
175
     */
176
    public function setMarginLeft(string $margin): self
177
    {
178
        $margin = new Size($margin);
179
        if ($margin->valid()) {
180
            $this->marginLeft = (string) $margin;
181
        }
182
 
183
        return $this;
184
    }
185
 
186
    /**
187
     * Get top margin (CSS style, i.e. XXpx or YYpt).
188
     */
189
    public function getMarginTop(): string
190
    {
191
        return $this->marginTop;
192
    }
193
 
194
    /**
195
     * Set top margin (CSS style, i.e. XXpx or YYpt). Default unit is pt.
196
     */
197
    public function setMarginTop(string $margin): self
198
    {
199
        $margin = new Size($margin);
200
        if ($margin->valid()) {
201
            $this->marginTop = (string) $margin;
202
        }
203
 
204
        return $this;
205
    }
206
 
207
    /**
208
     * Is the comment visible by default?
209
     */
210
    public function getVisible(): bool
211
    {
212
        return $this->visible;
213
    }
214
 
215
    /**
216
     * Set comment default visibility.
217
     */
218
    public function setVisible(bool $visibility): self
219
    {
220
        $this->visible = $visibility;
221
 
222
        return $this;
223
    }
224
 
225
    /**
226
     * Set fill color.
227
     */
228
    public function setFillColor(Color $color): self
229
    {
230
        $this->fillColor = $color;
231
 
232
        return $this;
233
    }
234
 
235
    /**
236
     * Get fill color.
237
     */
238
    public function getFillColor(): Color
239
    {
240
        return $this->fillColor;
241
    }
242
 
243
    public function setAlignment(string $alignment): self
244
    {
245
        $this->alignment = $alignment;
246
 
247
        return $this;
248
    }
249
 
250
    public function getAlignment(): string
251
    {
252
        return $this->alignment;
253
    }
254
 
255
    public function setTextboxDirection(string $textboxDirection): self
256
    {
257
        $this->textboxDirection = $textboxDirection;
258
 
259
        return $this;
260
    }
261
 
262
    public function getTextboxDirection(): string
263
    {
264
        return $this->textboxDirection;
265
    }
266
 
267
    /**
268
     * Get hash code.
269
     */
270
    public function getHashCode(): string
271
    {
272
        return md5(
273
            $this->author
274
            . $this->text->getHashCode()
275
            . $this->width
276
            . $this->height
277
            . $this->marginLeft
278
            . $this->marginTop
279
            . ($this->visible ? 1 : 0)
280
            . $this->fillColor->getHashCode()
281
            . $this->alignment
282
            . $this->textboxDirection
283
            . ($this->hasBackgroundImage() ? $this->backgroundImage->getHashCode() : '')
284
            . __CLASS__
285
        );
286
    }
287
 
288
    /**
289
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
290
     */
291
    public function __clone()
292
    {
293
        $vars = get_object_vars($this);
294
        foreach ($vars as $key => $value) {
295
            if (is_object($value)) {
296
                $this->$key = clone $value;
297
            } else {
298
                $this->$key = $value;
299
            }
300
        }
301
    }
302
 
303
    /**
304
     * Convert to string.
305
     */
306
    public function __toString(): string
307
    {
308
        return $this->text->getPlainText();
309
    }
310
 
311
    /**
312
     * Check is background image exists.
313
     */
314
    public function hasBackgroundImage(): bool
315
    {
316
        $path = $this->backgroundImage->getPath();
317
 
318
        if (empty($path)) {
319
            return false;
320
        }
321
 
322
        return getimagesize($path) !== false;
323
    }
324
 
325
    /**
326
     * Returns background image.
327
     */
328
    public function getBackgroundImage(): Drawing
329
    {
330
        return $this->backgroundImage;
331
    }
332
 
333
    /**
334
     * Sets background image.
335
     */
336
    public function setBackgroundImage(Drawing $objDrawing): self
337
    {
338
        if (!array_key_exists($objDrawing->getType(), Drawing::IMAGE_TYPES_CONVERTION_MAP)) {
339
            throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
340
        }
341
        $this->backgroundImage = $objDrawing;
342
 
343
        return $this;
344
    }
345
 
346
    /**
347
     * Sets size of comment as size of background image.
348
     */
349
    public function setSizeAsBackgroundImage(): self
350
    {
351
        if ($this->hasBackgroundImage()) {
352
            $this->setWidth(SharedDrawing::pixelsToPoints($this->backgroundImage->getWidth()) . 'pt');
353
            $this->setHeight(SharedDrawing::pixelsToPoints($this->backgroundImage->getHeight()) . 'pt');
354
        }
355
 
356
        return $this;
357
    }
358
}