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\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
 
9
class Base
10
{
11
    use ArrayEnabled;
12
 
13
    /**
14
     * BASE.
15
     *
16
     * Converts a number into a text representation with the given radix (base).
17
     *
18
     * Excel Function:
19
     *        BASE(Number, Radix [Min_length])
20
     *
21
     * @param mixed $number expect float
22
     *                      Or can be an array of values
23
     * @param mixed $radix expect float
24
     *                      Or can be an array of values
25
     * @param mixed $minLength expect int or null
26
     *                      Or can be an array of values
27
     *
28
     * @return array|string the text representation with the given radix (base)
29
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
30
     *            with the same dimensions
31
     */
32
    public static function evaluate(mixed $number, mixed $radix, mixed $minLength = null): array|string
33
    {
34
        if (is_array($number) || is_array($radix) || is_array($minLength)) {
35
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $radix, $minLength);
36
        }
37
 
38
        try {
39
            $number = (float) floor(Helpers::validateNumericNullBool($number));
40
            $radix = (int) Helpers::validateNumericNullBool($radix);
41
        } catch (Exception $e) {
42
            return $e->getMessage();
43
        }
44
 
45
        return self::calculate($number, $radix, $minLength);
46
    }
47
 
48
    private static function calculate(float $number, int $radix, mixed $minLength): string
49
    {
50
        if ($minLength === null || is_numeric($minLength)) {
51
            if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
52
                return ExcelError::NAN(); // Numeric range constraints
53
            }
54
 
55
            $outcome = strtoupper((string) base_convert("$number", 10, $radix));
56
            if ($minLength !== null) {
57
                $outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding
58
            }
59
 
60
            return $outcome;
61
        }
62
 
63
        return ExcelError::VALUE();
64
    }
65
}