Proyectos de Subversion Moodle

Rev

| 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\Writer\Common\Entity;
6
 
7
use OpenSpout\Writer\AutoFilter;
8
use OpenSpout\Writer\Common\ColumnWidth;
9
use OpenSpout\Writer\Common\Manager\SheetManager;
10
use OpenSpout\Writer\Exception\InvalidSheetNameException;
11
use OpenSpout\Writer\XLSX\Entity\SheetView;
12
 
13
/**
14
 * External representation of a worksheet.
15
 */
16
final class Sheet
17
{
18
    public const DEFAULT_SHEET_NAME_PREFIX = 'Sheet';
19
 
20
    /** @var 0|positive-int Index of the sheet, based on order in the workbook (zero-based) */
21
    private readonly int $index;
22
 
23
    /** @var string ID of the sheet's associated workbook. Used to restrict sheet name uniqueness enforcement to a single workbook */
24
    private readonly string $associatedWorkbookId;
25
 
26
    /** @var string Name of the sheet */
27
    private string $name;
28
 
29
    /** @var bool Visibility of the sheet */
30
    private bool $isVisible;
31
 
32
    /** @var SheetManager Sheet manager */
33
    private readonly SheetManager $sheetManager;
34
 
35
    private ?SheetView $sheetView = null;
36
 
37
    /** @var 0|positive-int */
38
    private int $writtenRowCount = 0;
39
 
40
    private ?AutoFilter $autoFilter = null;
41
 
42
    /** @var ColumnWidth[] Array of min-max-width arrays */
43
    private array $COLUMN_WIDTHS = [];
44
 
45
    /** @var string rows to repeat at top */
46
    private ?string $printTitleRows = null;
47
 
48
    /**
49
     * @param 0|positive-int $sheetIndex           Index of the sheet, based on order in the workbook (zero-based)
50
     * @param string         $associatedWorkbookId ID of the sheet's associated workbook
51
     * @param SheetManager   $sheetManager         To manage sheets
52
     */
53
    public function __construct(int $sheetIndex, string $associatedWorkbookId, SheetManager $sheetManager)
54
    {
55
        $this->index = $sheetIndex;
56
        $this->associatedWorkbookId = $associatedWorkbookId;
57
 
58
        $this->sheetManager = $sheetManager;
59
        $this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId);
60
 
61
        $this->setName(self::DEFAULT_SHEET_NAME_PREFIX.($sheetIndex + 1));
62
        $this->setIsVisible(true);
63
    }
64
 
65
    /**
66
     * @return 0|positive-int Index of the sheet, based on order in the workbook (zero-based)
67
     */
68
    public function getIndex(): int
69
    {
70
        return $this->index;
71
    }
72
 
73
    public function getAssociatedWorkbookId(): string
74
    {
75
        return $this->associatedWorkbookId;
76
    }
77
 
78
    /**
79
     * @return string Name of the sheet
80
     */
81
    public function getName(): string
82
    {
83
        return $this->name;
84
    }
85
 
86
    /**
87
     * Sets the name of the sheet. Note that Excel has some restrictions on the name:
88
     *  - it should not be blank
89
     *  - it should not exceed 31 characters
90
     *  - it should not contain these characters: \ / ? * : [ or ]
91
     *  - it should be unique.
92
     *
93
     * @param string $name Name of the sheet
94
     *
95
     * @throws InvalidSheetNameException if the sheet's name is invalid
96
     */
97
    public function setName(string $name): self
98
    {
99
        $this->sheetManager->throwIfNameIsInvalid($name, $this);
100
 
101
        $this->name = $name;
102
 
103
        $this->sheetManager->markSheetNameAsUsed($this);
104
 
105
        return $this;
106
    }
107
 
108
    /**
109
     * @return bool isVisible Visibility of the sheet
110
     */
111
    public function isVisible(): bool
112
    {
113
        return $this->isVisible;
114
    }
115
 
116
    /**
117
     * @param bool $isVisible Visibility of the sheet
118
     */
119
    public function setIsVisible(bool $isVisible): self
120
    {
121
        $this->isVisible = $isVisible;
122
 
123
        return $this;
124
    }
125
 
126
    /**
127
     * @return $this
128
     */
129
    public function setSheetView(SheetView $sheetView): self
130
    {
131
        $this->sheetView = $sheetView;
132
 
133
        return $this;
134
    }
135
 
136
    public function getSheetView(): ?SheetView
137
    {
138
        return $this->sheetView;
139
    }
140
 
141
    /**
142
     * @internal
143
     */
144
    public function incrementWrittenRowCount(): void
145
    {
146
        ++$this->writtenRowCount;
147
    }
148
 
149
    /**
150
     * @return 0|positive-int
151
     */
152
    public function getWrittenRowCount(): int
153
    {
154
        return $this->writtenRowCount;
155
    }
156
 
157
    /**
158
     * @return $this
159
     */
160
    public function setAutoFilter(?AutoFilter $autoFilter): self
161
    {
162
        $this->autoFilter = $autoFilter;
163
 
164
        return $this;
165
    }
166
 
167
    public function getAutoFilter(): ?AutoFilter
168
    {
169
        return $this->autoFilter;
170
    }
171
 
172
    /**
173
     * @param positive-int ...$columns One or more columns with this width
174
     */
175
    public function setColumnWidth(float $width, int ...$columns): void
176
    {
177
        // Gather sequences
178
        $sequence = [];
179
        foreach ($columns as $column) {
180
            $sequenceLength = \count($sequence);
181
            if ($sequenceLength > 0) {
182
                $previousValue = $sequence[$sequenceLength - 1];
183
                if ($column !== $previousValue + 1) {
184
                    $this->setColumnWidthForRange($width, $sequence[0], $previousValue);
185
                    $sequence = [];
186
                }
187
            }
188
            $sequence[] = $column;
189
        }
190
        $this->setColumnWidthForRange($width, $sequence[0], $sequence[\count($sequence) - 1]);
191
    }
192
 
193
    /**
194
     * @param float        $width The width to set
195
     * @param positive-int $start First column index of the range
196
     * @param positive-int $end   Last column index of the range
197
     */
198
    public function setColumnWidthForRange(float $width, int $start, int $end): void
199
    {
200
        $this->COLUMN_WIDTHS[] = new ColumnWidth($start, $end, $width);
201
    }
202
 
203
    /**
204
     * @internal
205
     *
206
     * @return ColumnWidth[]
207
     */
208
    public function getColumnWidths(): array
209
    {
210
        return $this->COLUMN_WIDTHS;
211
    }
212
 
213
    public function getPrintTitleRows(): ?string
214
    {
215
        return $this->printTitleRows;
216
    }
217
 
218
    public function setPrintTitleRows(string $printTitleRows): void
219
    {
220
        $this->printTitleRows = $printTitleRows;
221
    }
222
}