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\Worksheet\AutoFilter;
4
 
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
7
 
8
class Column
9
{
10
    const AUTOFILTER_FILTERTYPE_FILTER = 'filters';
11
    const AUTOFILTER_FILTERTYPE_CUSTOMFILTER = 'customFilters';
12
    //    Supports no more than 2 rules, with an And/Or join criteria
13
    //        if more than 1 rule is defined
14
    const AUTOFILTER_FILTERTYPE_DYNAMICFILTER = 'dynamicFilter';
15
    //    Even though the filter rule is constant, the filtered data can vary
16
    //        e.g. filtered by date = TODAY
17
    const AUTOFILTER_FILTERTYPE_TOPTENFILTER = 'top10';
18
 
19
    /**
20
     * Types of autofilter rules.
21
     *
22
     * @var string[]
23
     */
24
    private static array $filterTypes = [
25
        //    Currently we're not handling
26
        //        colorFilter
27
        //        extLst
28
        //        iconFilter
29
        self::AUTOFILTER_FILTERTYPE_FILTER,
30
        self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER,
31
        self::AUTOFILTER_FILTERTYPE_DYNAMICFILTER,
32
        self::AUTOFILTER_FILTERTYPE_TOPTENFILTER,
33
    ];
34
 
35
    // Multiple Rule Connections
36
    const AUTOFILTER_COLUMN_JOIN_AND = 'and';
37
    const AUTOFILTER_COLUMN_JOIN_OR = 'or';
38
 
39
    /**
40
     * Join options for autofilter rules.
41
     *
42
     * @var string[]
43
     */
44
    private static array $ruleJoins = [
45
        self::AUTOFILTER_COLUMN_JOIN_AND,
46
        self::AUTOFILTER_COLUMN_JOIN_OR,
47
    ];
48
 
49
    /**
50
     * Autofilter.
51
     */
52
    private ?AutoFilter $parent;
53
 
54
    /**
55
     * Autofilter Column Index.
56
     */
57
    private string $columnIndex;
58
 
59
    /**
60
     * Autofilter Column Filter Type.
61
     */
62
    private string $filterType = self::AUTOFILTER_FILTERTYPE_FILTER;
63
 
64
    /**
65
     * Autofilter Multiple Rules And/Or.
66
     */
67
    private string $join = self::AUTOFILTER_COLUMN_JOIN_OR;
68
 
69
    /**
70
     * Autofilter Column Rules.
71
     *
72
     * @var Column\Rule[]
73
     */
74
    private array $ruleset = [];
75
 
76
    /**
77
     * Autofilter Column Dynamic Attributes.
78
     *
79
     * @var (float|int|string)[]
80
     */
81
    private array $attributes = [];
82
 
83
    /**
84
     * Create a new Column.
85
     *
86
     * @param string $column Column (e.g. A)
87
     * @param ?AutoFilter $parent Autofilter for this column
88
     */
89
    public function __construct(string $column, ?AutoFilter $parent = null)
90
    {
91
        $this->columnIndex = $column;
92
        $this->parent = $parent;
93
    }
94
 
95
    public function setEvaluatedFalse(): void
96
    {
97
        if ($this->parent !== null) {
98
            $this->parent->setEvaluated(false);
99
        }
100
    }
101
 
102
    /**
103
     * Get AutoFilter column index as string eg: 'A'.
104
     */
105
    public function getColumnIndex(): string
106
    {
107
        return $this->columnIndex;
108
    }
109
 
110
    /**
111
     * Set AutoFilter column index as string eg: 'A'.
112
     *
113
     * @param string $column Column (e.g. A)
114
     *
115
     * @return $this
116
     */
117
    public function setColumnIndex(string $column): static
118
    {
119
        $this->setEvaluatedFalse();
120
        // Uppercase coordinate
121
        $column = strtoupper($column);
122
        if ($this->parent !== null) {
123
            $this->parent->testColumnInRange($column);
124
        }
125
 
126
        $this->columnIndex = $column;
127
 
128
        return $this;
129
    }
130
 
131
    /**
132
     * Get this Column's AutoFilter Parent.
133
     */
134
    public function getParent(): ?AutoFilter
135
    {
136
        return $this->parent;
137
    }
138
 
139
    /**
140
     * Set this Column's AutoFilter Parent.
141
     *
142
     * @return $this
143
     */
144
    public function setParent(?AutoFilter $parent = null): static
145
    {
146
        $this->setEvaluatedFalse();
147
        $this->parent = $parent;
148
 
149
        return $this;
150
    }
151
 
152
    /**
153
     * Get AutoFilter Type.
154
     */
155
    public function getFilterType(): string
156
    {
157
        return $this->filterType;
158
    }
159
 
160
    /**
161
     * Set AutoFilter Type.
162
     *
163
     * @return $this
164
     */
165
    public function setFilterType(string $filterType): static
166
    {
167
        $this->setEvaluatedFalse();
168
        if (!in_array($filterType, self::$filterTypes)) {
169
            throw new PhpSpreadsheetException('Invalid filter type for column AutoFilter.');
170
        }
171
        if ($filterType === self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER && count($this->ruleset) > 2) {
172
            throw new PhpSpreadsheetException('No more than 2 rules are allowed in a Custom Filter');
173
        }
174
 
175
        $this->filterType = $filterType;
176
 
177
        return $this;
178
    }
179
 
180
    /**
181
     * Get AutoFilter Multiple Rules And/Or Join.
182
     */
183
    public function getJoin(): string
184
    {
185
        return $this->join;
186
    }
187
 
188
    /**
189
     * Set AutoFilter Multiple Rules And/Or.
190
     *
191
     * @param string $join And/Or
192
     *
193
     * @return $this
194
     */
195
    public function setJoin(string $join): static
196
    {
197
        $this->setEvaluatedFalse();
198
        // Lowercase And/Or
199
        $join = strtolower($join);
200
        if (!in_array($join, self::$ruleJoins)) {
201
            throw new PhpSpreadsheetException('Invalid rule connection for column AutoFilter.');
202
        }
203
 
204
        $this->join = $join;
205
 
206
        return $this;
207
    }
208
 
209
    /**
210
     * Set AutoFilter Attributes.
211
     *
212
     * @param (float|int|string)[] $attributes
213
     *
214
     * @return $this
215
     */
216
    public function setAttributes(array $attributes): static
217
    {
218
        $this->setEvaluatedFalse();
219
        $this->attributes = $attributes;
220
 
221
        return $this;
222
    }
223
 
224
    /**
225
     * Set An AutoFilter Attribute.
226
     *
227
     * @param string $name Attribute Name
228
     * @param float|int|string $value Attribute Value
229
     *
230
     * @return $this
231
     */
232
    public function setAttribute(string $name, $value): static
233
    {
234
        $this->setEvaluatedFalse();
235
        $this->attributes[$name] = $value;
236
 
237
        return $this;
238
    }
239
 
240
    /**
241
     * Get AutoFilter Column Attributes.
242
     *
243
     * @return (float|int|string)[]
244
     */
245
    public function getAttributes(): array
246
    {
247
        return $this->attributes;
248
    }
249
 
250
    /**
251
     * Get specific AutoFilter Column Attribute.
252
     *
253
     * @param string $name Attribute Name
254
     */
255
    public function getAttribute(string $name): null|float|int|string
256
    {
257
        if (isset($this->attributes[$name])) {
258
            return $this->attributes[$name];
259
        }
260
 
261
        return null;
262
    }
263
 
264
    public function ruleCount(): int
265
    {
266
        return count($this->ruleset);
267
    }
268
 
269
    /**
270
     * Get all AutoFilter Column Rules.
271
     *
272
     * @return Column\Rule[]
273
     */
274
    public function getRules(): array
275
    {
276
        return $this->ruleset;
277
    }
278
 
279
    /**
280
     * Get a specified AutoFilter Column Rule.
281
     *
282
     * @param int $index Rule index in the ruleset array
283
     */
284
    public function getRule(int $index): Column\Rule
285
    {
286
        if (!isset($this->ruleset[$index])) {
287
            $this->ruleset[$index] = new Column\Rule($this);
288
        }
289
 
290
        return $this->ruleset[$index];
291
    }
292
 
293
    /**
294
     * Create a new AutoFilter Column Rule in the ruleset.
295
     */
296
    public function createRule(): Column\Rule
297
    {
298
        $this->setEvaluatedFalse();
299
        if ($this->filterType === self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER && count($this->ruleset) >= 2) {
300
            throw new PhpSpreadsheetException('No more than 2 rules are allowed in a Custom Filter');
301
        }
302
        $this->ruleset[] = new Column\Rule($this);
303
 
304
        return end($this->ruleset);
305
    }
306
 
307
    /**
308
     * Add a new AutoFilter Column Rule to the ruleset.
309
     *
310
     * @return $this
311
     */
312
    public function addRule(Column\Rule $rule): static
313
    {
314
        $this->setEvaluatedFalse();
315
        $rule->setParent($this);
316
        $this->ruleset[] = $rule;
317
 
318
        return $this;
319
    }
320
 
321
    /**
322
     * Delete a specified AutoFilter Column Rule
323
     * If the number of rules is reduced to 1, then we reset And/Or logic to Or.
324
     *
325
     * @param int $index Rule index in the ruleset array
326
     *
327
     * @return $this
328
     */
329
    public function deleteRule(int $index): static
330
    {
331
        $this->setEvaluatedFalse();
332
        if (isset($this->ruleset[$index])) {
333
            unset($this->ruleset[$index]);
334
            //    If we've just deleted down to a single rule, then reset And/Or joining to Or
335
            if (count($this->ruleset) <= 1) {
336
                $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
337
            }
338
        }
339
 
340
        return $this;
341
    }
342
 
343
    /**
344
     * Delete all AutoFilter Column Rules.
345
     *
346
     * @return $this
347
     */
348
    public function clearRules(): static
349
    {
350
        $this->setEvaluatedFalse();
351
        $this->ruleset = [];
352
        $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
353
 
354
        return $this;
355
    }
356
 
357
    /**
358
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
359
     */
360
    public function __clone()
361
    {
362
        $vars = get_object_vars($this);
363
        /** @var Column\Rule[] $value */
364
        foreach ($vars as $key => $value) {
365
            if ($key === 'parent') {
366
                // Detach from autofilter parent
367
                $this->parent = null;
368
            } elseif ($key === 'ruleset') {
369
                // The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter objects
370
                $this->ruleset = [];
371
                foreach ($value as $k => $v) {
372
                    $cloned = clone $v;
373
                    $cloned->setParent($this); // attach the new cloned Rule to this new cloned Autofilter Cloned object
374
                    $this->ruleset[$k] = $cloned;
375
                }
376
            } else {
377
                $this->$key = $value;
378
            }
379
        }
380
    }
381
}