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\MathTrig;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
 
9
class Helpers
10
{
11
    /**
12
     * Many functions accept null/false/true argument treated as 0/0/1.
13
     *
14
     * @return float|string quotient or DIV0 if denominator is too small
15
     */
16
    public static function verySmallDenominator(float $numerator, float $denominator): string|float
17
    {
18
        return (abs($denominator) < 1.0E-12) ? ExcelError::DIV0() : ($numerator / $denominator);
19
    }
20
 
21
    /**
22
     * Many functions accept null/false/true argument treated as 0/0/1.
23
     */
24
    public static function validateNumericNullBool(mixed $number): int|float
25
    {
26
        $number = Functions::flattenSingleValue($number);
27
        if ($number === null) {
28
            return 0;
29
        }
30
        if (is_bool($number)) {
31
            return (int) $number;
32
        }
33
        if (is_numeric($number)) {
34
            return 0 + $number;
35
        }
36
 
37
        throw new Exception(ExcelError::throwError($number));
38
    }
39
 
40
    /**
41
     * Validate numeric, but allow substitute for null.
42
     */
43
    public static function validateNumericNullSubstitution(mixed $number, null|float|int $substitute): float|int
44
    {
45
        $number = Functions::flattenSingleValue($number);
46
        if ($number === null && $substitute !== null) {
47
            return $substitute;
48
        }
49
        if (is_numeric($number)) {
50
            return 0 + $number;
51
        }
52
 
53
        throw new Exception(ExcelError::throwError($number));
54
    }
55
 
56
    /**
57
     * Confirm number >= 0.
58
     */
59
    public static function validateNotNegative(float|int $number, ?string $except = null): void
60
    {
61
        if ($number >= 0) {
62
            return;
63
        }
64
 
65
        throw new Exception($except ?? ExcelError::NAN());
66
    }
67
 
68
    /**
69
     * Confirm number > 0.
70
     */
71
    public static function validatePositive(float|int $number, ?string $except = null): void
72
    {
73
        if ($number > 0) {
74
            return;
75
        }
76
 
77
        throw new Exception($except ?? ExcelError::NAN());
78
    }
79
 
80
    /**
81
     * Confirm number != 0.
82
     */
83
    public static function validateNotZero(float|int $number): void
84
    {
85
        if ($number) {
86
            return;
87
        }
88
 
89
        throw new Exception(ExcelError::DIV0());
90
    }
91
 
92
    public static function returnSign(float $number): int
93
    {
94
        return $number ? (($number > 0) ? 1 : -1) : 0;
95
    }
96
 
97
    public static function getEven(float $number): float
98
    {
99
        $significance = 2 * self::returnSign($number);
100
 
101
        return $significance ? (ceil($number / $significance) * $significance) : 0;
102
    }
103
 
104
    /**
105
     * Return NAN or value depending on argument.
106
     */
107
    public static function numberOrNan(float $result): float|string
108
    {
109
        return is_nan($result) ? ExcelError::NAN() : $result;
110
    }
111
}