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