| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\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<mixed>|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($number, $radix, $minLength = null) |
| 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 |
/** |
| 49 |
* @param mixed $minLength |
| 50 |
*/ |
| 51 |
private static function calculate(float $number, int $radix, $minLength): string |
| 52 |
{ |
| 53 |
if ($minLength === null || is_numeric($minLength)) { |
| 54 |
if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) { |
| 55 |
return ExcelError::NAN(); // Numeric range constraints |
| 56 |
} |
| 57 |
|
| 58 |
$outcome = strtoupper((string) base_convert("$number", 10, $radix)); |
| 59 |
if ($minLength !== null) { |
| 60 |
$outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding |
| 61 |
} |
| 62 |
|
| 63 |
return $outcome; |
| 64 |
} |
| 65 |
|
| 66 |
return ExcelError::VALUE(); |
| 67 |
} |
| 68 |
} |
| 69 |
|