Skip to content

Commit 0c52f17

Browse files
authored
Added support for the base function (#1344)
1 parent 25e3e45 commit 0c52f17

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
77

88
## [Unreleased]
99

10+
- Added support for the BASE function
1011
- Added support for the ARABIC function
1112
- Conditionals - Extend Support for (NOT)CONTAINSBLANKS [#1278](https://github.com/PHPOffice/PhpSpreadsheet/pull/1278)
1213
- Handle Error in Formula Processing Better for Xls [#1267](https://github.com/PHPOffice/PhpSpreadsheet/pull/1267)

src/PhpSpreadsheet/Calculation/Calculation.php

+5
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ class Calculation
333333
'functionCall' => [Functions::class, 'DUMMY'],
334334
'argumentCount' => '1',
335335
],
336+
'BASE' => [
337+
'category' => Category::CATEGORY_MATH_AND_TRIG,
338+
'functionCall' => [MathTrig::class, 'BASE'],
339+
'argumentCount' => '2,3',
340+
],
336341
'BESSELI' => [
337342
'category' => Category::CATEGORY_ENGINEERING,
338343
'functionCall' => [Engineering::class, 'BESSELI'],

src/PhpSpreadsheet/Calculation/MathTrig.php

+43
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,49 @@ public static function ATAN2($xCoordinate = null, $yCoordinate = null)
142142
return Functions::VALUE();
143143
}
144144

145+
/**
146+
* BASE.
147+
*
148+
* Converts a number into a text representation with the given radix (base).
149+
*
150+
* Excel Function:
151+
* BASE(Number, Radix [Min_length])
152+
*
153+
* @category Mathematical and Trigonometric Functions
154+
*
155+
* @param float $number
156+
* @param float $radix
157+
* @param int $minLength
158+
*
159+
* @return string the text representation with the given radix (base)
160+
*/
161+
public static function BASE($number, $radix, $minLength = null)
162+
{
163+
$number = Functions::flattenSingleValue($number);
164+
$radix = Functions::flattenSingleValue($radix);
165+
$minLength = Functions::flattenSingleValue($minLength);
166+
167+
if (is_numeric($number) && is_numeric($radix) && ($minLength === null || is_numeric($minLength))) {
168+
// Truncate to an integer
169+
$number = (int) $number;
170+
$radix = (int) $radix;
171+
$minLength = (int) $minLength;
172+
173+
if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
174+
return Functions::NAN(); // Numeric range constraints
175+
}
176+
177+
$outcome = strtoupper((string) base_convert($number, 10, $radix));
178+
if ($minLength !== null) {
179+
$outcome = str_pad($outcome, $minLength, '0', STR_PAD_LEFT); // String padding
180+
}
181+
182+
return $outcome;
183+
}
184+
185+
return Functions::VALUE();
186+
}
187+
145188
/**
146189
* CEILING.
147190
*

src/PhpSpreadsheet/Calculation/functionlist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ AVERAGEA
2323
AVERAGEIF
2424
AVERAGEIFS
2525
BAHTTEXT
26+
BASE
2627
BESSELI
2728
BESSELJ
2829
BESSELK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6+
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class BaseTest extends TestCase
10+
{
11+
public function setUp()
12+
{
13+
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14+
}
15+
16+
/**
17+
* @dataProvider providerBASE
18+
*
19+
* @param mixed $expectedResult
20+
*/
21+
public function testBASE($expectedResult, ...$args)
22+
{
23+
$result = MathTrig::BASE(...$args);
24+
$this->assertEquals($expectedResult, $result);
25+
}
26+
27+
public function providerBASE()
28+
{
29+
return require 'data/Calculation/MathTrig/BASE.php';
30+
}
31+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
return [
4+
[
5+
'111',
6+
7,
7+
2,
8+
],
9+
[
10+
'64',
11+
100,
12+
16,
13+
],
14+
[
15+
'CA',
16+
202,
17+
16,
18+
],
19+
[
20+
'0000001111',
21+
15,
22+
2,
23+
10,
24+
],
25+
[
26+
'0000001111',
27+
15.3,
28+
2.6,
29+
10,
30+
],
31+
[
32+
'#VALUE!',
33+
'ABC',
34+
2,
35+
10,
36+
],
37+
[
38+
'#VALUE!',
39+
15,
40+
'ABC',
41+
10,
42+
],
43+
[
44+
'#VALUE!',
45+
15,
46+
2,
47+
'ABC',
48+
],
49+
[
50+
'#NUM!',
51+
-1,
52+
2,
53+
],
54+
[
55+
'#NUM!',
56+
15,
57+
-1,
58+
],
59+
];

0 commit comments

Comments
 (0)