/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/TextData/Helpers.php

https://gitlab.com/jjpa2018/dashboard · PHP · 86 lines · 61 code · 12 blank · 13 comment · 13 complexity · 1a526ca5d01dcb9d160fd4ba2c0dbad8 MD5 · raw file

  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. class Helpers
  7. {
  8. public static function convertBooleanValue(bool $value): string
  9. {
  10. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  11. return $value ? '1' : '0';
  12. }
  13. return ($value) ? Calculation::getTRUE() : Calculation::getFALSE();
  14. }
  15. /**
  16. * @param mixed $value String value from which to extract characters
  17. */
  18. public static function extractString($value): string
  19. {
  20. if (is_bool($value)) {
  21. return self::convertBooleanValue($value);
  22. }
  23. return (string) $value;
  24. }
  25. /**
  26. * @param mixed $value
  27. */
  28. public static function extractInt($value, int $minValue, int $gnumericNull = 0, bool $ooBoolOk = false): int
  29. {
  30. if ($value === null) {
  31. // usually 0, but sometimes 1 for Gnumeric
  32. $value = (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_GNUMERIC) ? $gnumericNull : 0;
  33. }
  34. if (is_bool($value) && ($ooBoolOk || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE)) {
  35. $value = (int) $value;
  36. }
  37. if (!is_numeric($value)) {
  38. throw new CalcExp(Functions::VALUE());
  39. }
  40. $value = (int) $value;
  41. if ($value < $minValue) {
  42. throw new CalcExp(Functions::VALUE());
  43. }
  44. return (int) $value;
  45. }
  46. /**
  47. * @param mixed $value
  48. */
  49. public static function extractFloat($value): float
  50. {
  51. if ($value === null) {
  52. $value = 0.0;
  53. }
  54. if (is_bool($value)) {
  55. $value = (float) $value;
  56. }
  57. if (!is_numeric($value)) {
  58. throw new CalcExp(Functions::VALUE());
  59. }
  60. return (float) $value;
  61. }
  62. /**
  63. * @param mixed $value
  64. */
  65. public static function validateInt($value): int
  66. {
  67. if ($value === null) {
  68. $value = 0;
  69. } elseif (is_bool($value)) {
  70. $value = (int) $value;
  71. }
  72. return (int) $value;
  73. }
  74. }