Passed
Push — master ( c380b2...9239b3 )
by Adrien
10:06
created

Base::funcBase()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 14
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 24
ccs 14
cts 14
cp 1
crap 9
rs 8.0555
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
8
class Base
9
{
10
    /**
11
     * BASE.
12
     *
13
     * Converts a number into a text representation with the given radix (base).
14
     *
15
     * Excel Function:
16
     *        BASE(Number, Radix [Min_length])
17
     *
18
     * @param mixed $number expect float
19
     * @param mixed $radix expect float
20
     * @param mixed $minLength expect int or null
21
     *
22
     * @return string the text representation with the given radix (base)
23
     */
24 15
    public static function funcBase($number, $radix, $minLength = null)
25
    {
26
        try {
27 15
            $number = (int) Helpers::validateNumericNullBool($number);
28 14
            $radix = (int) Helpers::validateNumericNullBool($radix);
29 2
        } catch (Exception $e) {
30 2
            return $e->getMessage();
31
        }
32 13
        $minLength = Functions::flattenSingleValue($minLength);
33
34 13
        if ($minLength === null || is_numeric($minLength)) {
35 11
            if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
36 4
                return Functions::NAN(); // Numeric range constraints
37
            }
38
39 7
            $outcome = strtoupper((string) base_convert($number, 10, $radix));
40 7
            if ($minLength !== null) {
41 3
                $outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding
42
            }
43
44 7
            return $outcome;
45
        }
46
47 2
        return Functions::VALUE();
48
    }
49
}
50