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\TextData;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
9
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
10
 
11
class Helpers
12
{
13
    public static function convertBooleanValue(bool $value): string
14
    {
15
        if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
16
            return $value ? '1' : '0';
17
        }
18
 
19
        return ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
20
    }
21
 
22
    /**
23
     * @param mixed $value String value from which to extract characters
24
     */
25
    public static function extractString(mixed $value, bool $throwIfError = false): string
26
    {
27
        if (is_bool($value)) {
28
            return self::convertBooleanValue($value);
29
        }
30
        if ($throwIfError && is_string($value) && ErrorValue::isError($value, true)) {
31
            throw new CalcExp($value);
32
        }
33
 
34
        return (string) $value;
35
    }
36
 
37
    public static function extractInt(mixed $value, int $minValue, int $gnumericNull = 0, bool $ooBoolOk = false): int
38
    {
39
        if ($value === null) {
40
            // usually 0, but sometimes 1 for Gnumeric
41
            $value = (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_GNUMERIC) ? $gnumericNull : 0;
42
        }
43
        if (is_bool($value) && ($ooBoolOk || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE)) {
44
            $value = (int) $value;
45
        }
46
        if (!is_numeric($value)) {
47
            throw new CalcExp(ExcelError::VALUE());
48
        }
49
        $value = (int) $value;
50
        if ($value < $minValue) {
51
            throw new CalcExp(ExcelError::VALUE());
52
        }
53
 
54
        return (int) $value;
55
    }
56
 
57
    public static function extractFloat(mixed $value): float
58
    {
59
        if ($value === null) {
60
            $value = 0.0;
61
        }
62
        if (is_bool($value)) {
63
            $value = (float) $value;
64
        }
65
        if (!is_numeric($value)) {
66
            if (is_string($value) && ErrorValue::isError($value, true)) {
67
                throw new CalcExp($value);
68
            }
69
 
70
            throw new CalcExp(ExcelError::VALUE());
71
        }
72
 
73
        return (float) $value;
74
    }
75
 
76
    public static function validateInt(mixed $value, bool $throwIfError = false): int
77
    {
78
        if ($value === null) {
79
            $value = 0;
80
        } elseif (is_bool($value)) {
81
            $value = (int) $value;
82
        } elseif ($throwIfError && is_string($value) && !is_numeric($value)) {
83
            if (!ErrorValue::isError($value, true)) {
84
                $value = ExcelError::VALUE();
85
            }
86
 
87
            throw new CalcExp($value);
88
        }
89
 
90
        return (int) $value;
91
    }
92
}