| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation; |
| 4 |
|
| 5 |
class CalculationBase |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Get a list of all implemented functions as an array of function objects. |
| 9 |
* |
| 10 |
* @return array<string, array{category: string, functionCall: string|string[], argumentCount: string, passCellReference?: bool, passByReference?: bool[], custom?: bool}> |
| 11 |
*/ |
| 12 |
public static function getFunctions(): array |
| 13 |
{ |
| 14 |
return FunctionArray::$phpSpreadsheetFunctions; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get address of list of all implemented functions as an array of function objects. |
| 19 |
* |
| 20 |
* @return array<string, array<string, mixed>> |
| 21 |
*/ |
| 22 |
protected static function &getFunctionsAddress(): array |
| 23 |
{ |
| 24 |
return FunctionArray::$phpSpreadsheetFunctions; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param array{category: string, functionCall: string|string[], argumentCount: string, passCellReference?: bool, passByReference?: bool[], custom?: bool} $value |
| 29 |
*/ |
| 30 |
public static function addFunction(string $key, array $value): bool |
| 31 |
{ |
| 32 |
$key = strtoupper($key); |
| 33 |
if ( |
| 34 |
array_key_exists($key, FunctionArray::$phpSpreadsheetFunctions) |
| 35 |
&& !self::isDummy($key) |
| 36 |
) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
$value['custom'] = true; |
| 40 |
FunctionArray::$phpSpreadsheetFunctions[$key] = $value; |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
private static function isDummy(string $key): bool |
| 46 |
{ |
| 47 |
// key is already known to exist |
| 48 |
$functionCall = FunctionArray::$phpSpreadsheetFunctions[$key]['functionCall'] ?? null; |
| 49 |
if (!is_array($functionCall)) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
if (($functionCall[1] ?? '') !== 'DUMMY') { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
public static function removeFunction(string $key): bool |
| 60 |
{ |
| 61 |
$key = strtoupper($key); |
| 62 |
if (array_key_exists($key, FunctionArray::$phpSpreadsheetFunctions)) { |
| 63 |
if (FunctionArray::$phpSpreadsheetFunctions[$key]['custom'] ?? false) { |
| 64 |
unset(FunctionArray::$phpSpreadsheetFunctions[$key]); |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Registers custom functions and aliases that TablePress uses. |
| 75 |
* |
| 76 |
* These functions don't exist in Excel and need to be aliases or replaced by a custom implementation, to maintain backward compatibility. |
| 77 |
* These functions are deprecated in TablePress and should be replaced with their corresponding function! |
| 78 |
* |
| 79 |
* For functions with an underscore (_) in their name, the Calculation::CALCULATION_REGEXP_FUNCTION regexp has been adjusted. |
| 80 |
*/ |
| 81 |
protected function register_tablepress_aliases_and_custom_functions() { |
| 82 |
// Trigonometric ARC functions only have an A prefix in Excel. |
| 83 |
FunctionArray::$phpSpreadsheetFunctions['ARCCOS'] = FunctionArray::$phpSpreadsheetFunctions['ACOS']; |
| 84 |
FunctionArray::$phpSpreadsheetFunctions['ARCCOSH'] = FunctionArray::$phpSpreadsheetFunctions['ACOSH']; |
| 85 |
FunctionArray::$phpSpreadsheetFunctions['ARCCOT'] = FunctionArray::$phpSpreadsheetFunctions['ACOT']; |
| 86 |
FunctionArray::$phpSpreadsheetFunctions['ARCCOTH'] = FunctionArray::$phpSpreadsheetFunctions['ACOTH']; |
| 87 |
FunctionArray::$phpSpreadsheetFunctions['ARCSIN'] = FunctionArray::$phpSpreadsheetFunctions['ASIN']; |
| 88 |
FunctionArray::$phpSpreadsheetFunctions['ARCSINH'] = FunctionArray::$phpSpreadsheetFunctions['ASINH']; |
| 89 |
FunctionArray::$phpSpreadsheetFunctions['ARCTAN'] = FunctionArray::$phpSpreadsheetFunctions['ATAN']; |
| 90 |
FunctionArray::$phpSpreadsheetFunctions['ARCTAN2'] = FunctionArray::$phpSpreadsheetFunctions['ATAN2']; |
| 91 |
FunctionArray::$phpSpreadsheetFunctions['ARCTANH'] = FunctionArray::$phpSpreadsheetFunctions['ATANH']; |
| 92 |
|
| 93 |
// Aliases for functions with different names in Excel. |
| 94 |
FunctionArray::$phpSpreadsheetFunctions['MEAN'] = FunctionArray::$phpSpreadsheetFunctions['AVERAGE']; |
| 95 |
FunctionArray::$phpSpreadsheetFunctions['CEIL'] = FunctionArray::$phpSpreadsheetFunctions['CEILING']; |
| 96 |
FunctionArray::$phpSpreadsheetFunctions['RAND_INT'] = FunctionArray::$phpSpreadsheetFunctions['RANDBETWEEN']; |
| 97 |
FunctionArray::$phpSpreadsheetFunctions['RAND_FLOAT'] = FunctionArray::$phpSpreadsheetFunctions['RAND']; |
| 98 |
|
| 99 |
// Custom functions for which there is no corresponding function in Excel. |
| 100 |
FunctionArray::$phpSpreadsheetFunctions['NUMBER_FORMAT'] = array( |
| 101 |
'category' => Category::CATEGORY_TEXT_AND_DATA, |
| 102 |
'functionCall' => [TextData\Format::class, 'NUMBER_FORMAT'], |
| 103 |
'argumentCount' => '1,2', |
| 104 |
); |
| 105 |
FunctionArray::$phpSpreadsheetFunctions['NUMBER_FORMAT_EU'] = array( |
| 106 |
'category' => Category::CATEGORY_TEXT_AND_DATA, |
| 107 |
'functionCall' => [TextData\Format::class, 'NUMBER_FORMAT_EU'], |
| 108 |
'argumentCount' => '1,2', |
| 109 |
); |
| 110 |
FunctionArray::$phpSpreadsheetFunctions['RANGE'] = array( |
| 111 |
'category' => Category::CATEGORY_STATISTICAL, |
| 112 |
'functionCall' => [Statistical\Range::class, 'RANGE'], |
| 113 |
'argumentCount' => '1+', |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|