Helpers.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
  3. use DateTime;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  7. use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
  8. class Helpers
  9. {
  10. /**
  11. * Identify if a year is a leap year or not.
  12. *
  13. * @param int|string $year The year to test
  14. *
  15. * @return bool TRUE if the year is a leap year, otherwise FALSE
  16. */
  17. public static function isLeapYear($year): bool
  18. {
  19. return (($year % 4) === 0) && (($year % 100) !== 0) || (($year % 400) === 0);
  20. }
  21. /**
  22. * getDateValue.
  23. *
  24. * @param mixed $dateValue
  25. *
  26. * @return float Excel date/time serial value
  27. */
  28. public static function getDateValue($dateValue, bool $allowBool = true): float
  29. {
  30. if (is_object($dateValue)) {
  31. $retval = SharedDateHelper::PHPToExcel($dateValue);
  32. if (is_bool($retval)) {
  33. throw new Exception(ExcelError::VALUE());
  34. }
  35. return $retval;
  36. }
  37. self::nullFalseTrueToNumber($dateValue, $allowBool);
  38. if (!is_numeric($dateValue)) {
  39. $saveReturnDateType = Functions::getReturnDateType();
  40. Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
  41. $dateValue = DateValue::fromString($dateValue);
  42. Functions::setReturnDateType($saveReturnDateType);
  43. if (!is_numeric($dateValue)) {
  44. throw new Exception(ExcelError::VALUE());
  45. }
  46. }
  47. if ($dateValue < 0 && Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) {
  48. throw new Exception(ExcelError::NAN());
  49. }
  50. return (float) $dateValue;
  51. }
  52. /**
  53. * getTimeValue.
  54. *
  55. * @param string $timeValue
  56. *
  57. * @return mixed Excel date/time serial value, or string if error
  58. */
  59. public static function getTimeValue($timeValue)
  60. {
  61. $saveReturnDateType = Functions::getReturnDateType();
  62. Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
  63. $timeValue = TimeValue::fromString($timeValue);
  64. Functions::setReturnDateType($saveReturnDateType);
  65. return $timeValue;
  66. }
  67. /**
  68. * Adjust date by given months.
  69. *
  70. * @param mixed $dateValue
  71. */
  72. public static function adjustDateByMonths($dateValue = 0, float $adjustmentMonths = 0): DateTime
  73. {
  74. // Execute function
  75. $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  76. $oMonth = (int) $PHPDateObject->format('m');
  77. $oYear = (int) $PHPDateObject->format('Y');
  78. $adjustmentMonthsString = (string) $adjustmentMonths;
  79. if ($adjustmentMonths > 0) {
  80. $adjustmentMonthsString = '+' . $adjustmentMonths;
  81. }
  82. if ($adjustmentMonths != 0) {
  83. $PHPDateObject->modify($adjustmentMonthsString . ' months');
  84. }
  85. $nMonth = (int) $PHPDateObject->format('m');
  86. $nYear = (int) $PHPDateObject->format('Y');
  87. $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
  88. if ($monthDiff != $adjustmentMonths) {
  89. $adjustDays = (int) $PHPDateObject->format('d');
  90. $adjustDaysString = '-' . $adjustDays . ' days';
  91. $PHPDateObject->modify($adjustDaysString);
  92. }
  93. return $PHPDateObject;
  94. }
  95. /**
  96. * Help reduce perceived complexity of some tests.
  97. *
  98. * @param mixed $value
  99. * @param mixed $altValue
  100. */
  101. public static function replaceIfEmpty(&$value, $altValue): void
  102. {
  103. $value = $value ?: $altValue;
  104. }
  105. /**
  106. * Adjust year in ambiguous situations.
  107. */
  108. public static function adjustYear(string $testVal1, string $testVal2, string &$testVal3): void
  109. {
  110. if (!is_numeric($testVal1) || $testVal1 < 31) {
  111. if (!is_numeric($testVal2) || $testVal2 < 12) {
  112. if (is_numeric($testVal3) && $testVal3 < 12) {
  113. $testVal3 += 2000;
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Return result in one of three formats.
  120. *
  121. * @return mixed
  122. */
  123. public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = false)
  124. {
  125. $retType = Functions::getReturnDateType();
  126. if ($retType === Functions::RETURNDATE_PHP_DATETIME_OBJECT) {
  127. return new DateTime(
  128. $dateArray['year']
  129. . '-' . $dateArray['month']
  130. . '-' . $dateArray['day']
  131. . ' ' . $dateArray['hour']
  132. . ':' . $dateArray['minute']
  133. . ':' . $dateArray['second']
  134. );
  135. }
  136. $excelDateValue =
  137. SharedDateHelper::formattedPHPToExcel(
  138. $dateArray['year'],
  139. $dateArray['month'],
  140. $dateArray['day'],
  141. $dateArray['hour'],
  142. $dateArray['minute'],
  143. $dateArray['second']
  144. );
  145. if ($retType === Functions::RETURNDATE_EXCEL) {
  146. return $noFrac ? floor($excelDateValue) : (float) $excelDateValue;
  147. }
  148. // RETURNDATE_UNIX_TIMESTAMP)
  149. return (int) SharedDateHelper::excelToTimestamp($excelDateValue);
  150. }
  151. /**
  152. * Return result in one of three formats.
  153. *
  154. * @return mixed
  155. */
  156. public static function returnIn3FormatsFloat(float $excelDateValue)
  157. {
  158. $retType = Functions::getReturnDateType();
  159. if ($retType === Functions::RETURNDATE_EXCEL) {
  160. return $excelDateValue;
  161. }
  162. if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
  163. return (int) SharedDateHelper::excelToTimestamp($excelDateValue);
  164. }
  165. // RETURNDATE_PHP_DATETIME_OBJECT
  166. return SharedDateHelper::excelToDateTimeObject($excelDateValue);
  167. }
  168. /**
  169. * Return result in one of three formats.
  170. *
  171. * @return mixed
  172. */
  173. public static function returnIn3FormatsObject(DateTime $PHPDateObject)
  174. {
  175. $retType = Functions::getReturnDateType();
  176. if ($retType === Functions::RETURNDATE_PHP_DATETIME_OBJECT) {
  177. return $PHPDateObject;
  178. }
  179. if ($retType === Functions::RETURNDATE_EXCEL) {
  180. return (float) SharedDateHelper::PHPToExcel($PHPDateObject);
  181. }
  182. // RETURNDATE_UNIX_TIMESTAMP
  183. $stamp = SharedDateHelper::PHPToExcel($PHPDateObject);
  184. $stamp = is_bool($stamp) ? ((int) $stamp) : $stamp;
  185. return (int) SharedDateHelper::excelToTimestamp($stamp);
  186. }
  187. private static function baseDate(): int
  188. {
  189. if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) {
  190. return 0;
  191. }
  192. if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_MAC_1904) {
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. /**
  198. * Many functions accept null/false/true argument treated as 0/0/1.
  199. *
  200. * @param mixed $number
  201. */
  202. public static function nullFalseTrueToNumber(&$number, bool $allowBool = true): void
  203. {
  204. $number = Functions::flattenSingleValue($number);
  205. $nullVal = self::baseDate();
  206. if ($number === null) {
  207. $number = $nullVal;
  208. } elseif ($allowBool && is_bool($number)) {
  209. $number = $nullVal + (int) $number;
  210. }
  211. }
  212. /**
  213. * Many functions accept null argument treated as 0.
  214. *
  215. * @param mixed $number
  216. *
  217. * @return float|int
  218. */
  219. public static function validateNumericNull($number)
  220. {
  221. $number = Functions::flattenSingleValue($number);
  222. if ($number === null) {
  223. return 0;
  224. }
  225. if (is_int($number)) {
  226. return $number;
  227. }
  228. if (is_numeric($number)) {
  229. return (float) $number;
  230. }
  231. throw new Exception(ExcelError::VALUE());
  232. }
  233. /**
  234. * Many functions accept null/false/true argument treated as 0/0/1.
  235. *
  236. * @param mixed $number
  237. *
  238. * @return float
  239. */
  240. public static function validateNotNegative($number)
  241. {
  242. if (!is_numeric($number)) {
  243. throw new Exception(ExcelError::VALUE());
  244. }
  245. if ($number >= 0) {
  246. return (float) $number;
  247. }
  248. throw new Exception(ExcelError::NAN());
  249. }
  250. public static function silly1900(DateTime $PHPDateObject, string $mod = '-1 day'): void
  251. {
  252. $isoDate = $PHPDateObject->format('c');
  253. if ($isoDate < '1900-03-01') {
  254. $PHPDateObject->modify($mod);
  255. }
  256. }
  257. public static function dateParse(string $string): array
  258. {
  259. return self::forceArray(date_parse($string));
  260. }
  261. public static function dateParseSucceeded(array $dateArray): bool
  262. {
  263. return $dateArray['error_count'] === 0;
  264. }
  265. /**
  266. * Despite documentation, date_parse probably never returns false.
  267. * Just in case, this routine helps guarantee it.
  268. *
  269. * @param array|false $dateArray
  270. */
  271. private static function forceArray($dateArray): array
  272. {
  273. return is_array($dateArray) ? $dateArray : ['error_count' => 1];
  274. }
  275. }