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\Calculation\Information;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Cell\Cell;
9
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
10
use PhpOffice\PhpSpreadsheet\NamedRange;
11
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
12
 
13
class Value
14
{
15
    use ArrayEnabled;
16
 
17
    /**
18
     * IS_BLANK.
19
     *
20
     * @param mixed $value Value to check
21
     *                      Or can be an array of values
22
     *
23
     * @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array
24
     *            with the same dimensions
25
     */
26
    public static function isBlank(mixed $value = null): array|bool
27
    {
28
        if (is_array($value)) {
29
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
30
        }
31
 
32
        return $value === null;
33
    }
34
 
35
    /**
36
     * IS_REF.
37
     *
38
     * @param mixed $value Value to check
39
     */
40
    public static function isRef(mixed $value, ?Cell $cell = null): bool
41
    {
42
        if ($cell === null) {
43
            return false;
44
        }
45
 
46
        $cellValue = Functions::trimTrailingRange($value);
47
        if (preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/ui', $cellValue) === 1) {
48
            [$worksheet, $cellValue] = Worksheet::extractSheetTitle($cellValue, true, true);
49
            if (!empty($worksheet) && $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheet) === null) {
50
                return false;
51
            }
52
            [$column, $row] = Coordinate::indexesFromString($cellValue ?? '');
53
            if ($column > 16384 || $row > 1048576) {
54
                return false;
55
            }
56
 
57
            return true;
58
        }
59
 
60
        $namedRange = $cell->getWorksheet()->getParentOrThrow()->getNamedRange($value);
61
 
62
        return $namedRange instanceof NamedRange;
63
    }
64
 
65
    /**
66
     * IS_EVEN.
67
     *
68
     * @param mixed $value Value to check
69
     *                      Or can be an array of values
70
     *
71
     * @return array|bool|string If an array of numbers is passed as an argument, then the returned result will also be an array
72
     *            with the same dimensions
73
     */
74
    public static function isEven(mixed $value = null): array|string|bool
75
    {
76
        if (is_array($value)) {
77
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
78
        }
79
 
80
        if ($value === null) {
81
            return ExcelError::NAME();
82
        } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
83
            return ExcelError::VALUE();
84
        }
85
 
86
        return ((int) fmod($value, 2)) === 0;
87
    }
88
 
89
    /**
90
     * IS_ODD.
91
     *
92
     * @param mixed $value Value to check
93
     *                      Or can be an array of values
94
     *
95
     * @return array|bool|string If an array of numbers is passed as an argument, then the returned result will also be an array
96
     *            with the same dimensions
97
     */
98
    public static function isOdd(mixed $value = null): array|string|bool
99
    {
100
        if (is_array($value)) {
101
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
102
        }
103
 
104
        if ($value === null) {
105
            return ExcelError::NAME();
106
        } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
107
            return ExcelError::VALUE();
108
        }
109
 
110
        return ((int) fmod($value, 2)) !== 0;
111
    }
112
 
113
    /**
114
     * IS_NUMBER.
115
     *
116
     * @param mixed $value Value to check
117
     *                      Or can be an array of values
118
     *
119
     * @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array
120
     *            with the same dimensions
121
     */
122
    public static function isNumber(mixed $value = null): array|bool
123
    {
124
        if (is_array($value)) {
125
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
126
        }
127
 
128
        if (is_string($value)) {
129
            return false;
130
        }
131
 
132
        return is_numeric($value);
133
    }
134
 
135
    /**
136
     * IS_LOGICAL.
137
     *
138
     * @param mixed $value Value to check
139
     *                      Or can be an array of values
140
     *
141
     * @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array
142
     *            with the same dimensions
143
     */
144
    public static function isLogical(mixed $value = null): array|bool
145
    {
146
        if (is_array($value)) {
147
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
148
        }
149
 
150
        return is_bool($value);
151
    }
152
 
153
    /**
154
     * IS_TEXT.
155
     *
156
     * @param mixed $value Value to check
157
     *                      Or can be an array of values
158
     *
159
     * @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array
160
     *            with the same dimensions
161
     */
162
    public static function isText(mixed $value = null): array|bool
163
    {
164
        if (is_array($value)) {
165
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
166
        }
167
 
168
        return is_string($value) && !ErrorValue::isError($value);
169
    }
170
 
171
    /**
172
     * IS_NONTEXT.
173
     *
174
     * @param mixed $value Value to check
175
     *                      Or can be an array of values
176
     *
177
     * @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array
178
     *            with the same dimensions
179
     */
180
    public static function isNonText(mixed $value = null): array|bool
181
    {
182
        if (is_array($value)) {
183
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
184
        }
185
 
186
        return !self::isText($value);
187
    }
188
 
189
    /**
190
     * ISFORMULA.
191
     *
192
     * @param mixed $cellReference The cell to check
193
     * @param ?Cell $cell The current cell (containing this formula)
194
     */
195
    public static function isFormula(mixed $cellReference = '', ?Cell $cell = null): array|bool|string
196
    {
197
        if ($cell === null) {
198
            return ExcelError::REF();
199
        }
200
 
201
        $fullCellReference = Functions::expandDefinedName((string) $cellReference, $cell);
202
 
203
        if (str_contains($cellReference, '!')) {
204
            $cellReference = Functions::trimSheetFromCellReference($cellReference);
205
            $cellReferences = Coordinate::extractAllCellReferencesInRange($cellReference);
206
            if (count($cellReferences) > 1) {
207
                return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $cellReferences, $cell);
208
            }
209
        }
210
 
211
        $fullCellReference = Functions::trimTrailingRange($fullCellReference);
212
 
213
        $worksheetName = '';
214
        if (1 == preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches)) {
215
            $fullCellReference = $matches[6] . $matches[7];
216
            $worksheetName = str_replace("''", "'", trim($matches[2], "'"));
217
        }
218
 
219
        $worksheet = (!empty($worksheetName))
220
            ? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
221
            : $cell->getWorksheet();
222
 
223
        return ($worksheet !== null) ? $worksheet->getCell($fullCellReference)->isFormula() : ExcelError::REF();
224
    }
225
 
226
    /**
227
     * N.
228
     *
229
     * Returns a value converted to a number
230
     *
231
     * @param null|mixed $value The value you want converted
232
     *
233
     * @return number|string N converts values listed in the following table
234
     *        If value is or refers to N returns
235
     *        A number            That number value
236
     *        A date              The Excel serialized number of that date
237
     *        TRUE                1
238
     *        FALSE               0
239
     *        An error value      The error value
240
     *        Anything else       0
241
     */
242
    public static function asNumber($value = null)
243
    {
244
        while (is_array($value)) {
245
            $value = array_shift($value);
246
        }
247
 
248
        switch (gettype($value)) {
249
            case 'double':
250
            case 'float':
251
            case 'integer':
252
                return $value;
253
            case 'boolean':
254
                return (int) $value;
255
            case 'string':
256
                //    Errors
257
                if (($value !== '') && ($value[0] == '#')) {
258
                    return $value;
259
                }
260
 
261
                break;
262
        }
263
 
264
        return 0;
265
    }
266
 
267
    /**
268
     * TYPE.
269
     *
270
     * Returns a number that identifies the type of a value
271
     *
272
     * @param null|mixed $value The value you want tested
273
     *
274
     * @return int N converts values listed in the following table
275
     *        If value is or refers to N returns
276
     *        A number            1
277
     *        Text                2
278
     *        Logical Value       4
279
     *        An error value      16
280
     *        Array or Matrix     64
281
     */
282
    public static function type($value = null): int
283
    {
284
        $value = Functions::flattenArrayIndexed($value);
285
        if (count($value) > 1) {
286
            end($value);
287
            $a = key($value);
288
            //    Range of cells is an error
289
            if (Functions::isCellValue($a)) {
290
                return 16;
291
            //    Test for Matrix
292
            } elseif (Functions::isMatrixValue($a)) {
293
                return 64;
294
            }
295
        } elseif (empty($value)) {
296
            //    Empty Cell
297
            return 1;
298
        }
299
 
300
        $value = Functions::flattenSingleValue($value);
301
        if (($value === null) || (is_float($value)) || (is_int($value))) {
302
            return 1;
303
        } elseif (is_bool($value)) {
304
            return 4;
305
        } elseif (is_array($value)) {
306
            return 64;
307
        } elseif (is_string($value)) {
308
            //    Errors
309
            if (($value !== '') && ($value[0] == '#')) {
310
                return 16;
311
            }
312
 
313
            return 2;
314
        }
315
 
316
        return 0;
317
    }
318
}