| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 10 |
|
| 11 |
abstract class ConvertBase |
| 12 |
{ |
| 13 |
use ArrayEnabled; |
| 14 |
|
| 15 |
/** |
| 16 |
* @param mixed $value |
| 17 |
*/ |
| 18 |
protected static function validateValue($value): string |
| 19 |
{ |
| 20 |
if (is_bool($value)) { |
| 21 |
if (Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) { |
| 22 |
throw new Exception(ExcelError::VALUE()); |
| 23 |
} |
| 24 |
$value = (int) $value; |
| 25 |
} |
| 26 |
|
| 27 |
if (is_numeric($value)) { |
| 28 |
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) { |
| 29 |
$value = floor((float) $value); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
return strtoupper(StringHelper::convertToString($value)); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param mixed $places |
| 38 |
*/ |
| 39 |
protected static function validatePlaces($places = null): ?int |
| 40 |
{ |
| 41 |
if ($places === null) { |
| 42 |
return $places; |
| 43 |
} |
| 44 |
|
| 45 |
if (is_numeric($places)) { |
| 46 |
if ($places < 0 || $places > 10) { |
| 47 |
throw new Exception(ExcelError::NAN()); |
| 48 |
} |
| 49 |
|
| 50 |
return (int) $places; |
| 51 |
} |
| 52 |
|
| 53 |
throw new Exception(ExcelError::VALUE()); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Formats a number base string value with leading zeroes. |
| 58 |
* |
| 59 |
* @param string $value The "number" to pad |
| 60 |
* @param ?int $places The length that we want to pad this value |
| 61 |
* |
| 62 |
* @return string The padded "number" |
| 63 |
*/ |
| 64 |
protected static function nbrConversionFormat(string $value, ?int $places): string |
| 65 |
{ |
| 66 |
if ($places !== null) { |
| 67 |
if (strlen($value) <= $places) { |
| 68 |
return substr(str_pad($value, $places, '0', STR_PAD_LEFT), -10); |
| 69 |
} |
| 70 |
|
| 71 |
return ExcelError::NAN(); |
| 72 |
} |
| 73 |
|
| 74 |
return substr($value, -10); |
| 75 |
} |
| 76 |
} |
| 77 |
|