Format.php: Handle null values

This commit is contained in:
Sukhwinder Dhillon 2022-07-06 14:30:29 +02:00 committed by Johannes Meyer
parent 35977d67ed
commit 21e4c68a58
1 changed files with 20 additions and 0 deletions

View File

@ -54,6 +54,10 @@ class Format
public static function seconds($value) public static function seconds($value)
{ {
if ($value === null) {
return '';
}
$absValue = abs($value); $absValue = abs($value);
if ($absValue < 60) { if ($absValue < 60) {
@ -70,6 +74,10 @@ class Format
protected static function formatForUnits($value, &$units, $base) protected static function formatForUnits($value, &$units, $base)
{ {
if ($value === null) {
return '';
}
$sign = ''; $sign = '';
if ($value < 0) { if ($value < 0) {
$value = abs($value); $value = abs($value);
@ -105,6 +113,10 @@ class Format
*/ */
public static function secondsByMonth($dateTimeOrTimestamp) public static function secondsByMonth($dateTimeOrTimestamp)
{ {
if ($dateTimeOrTimestamp === null) {
return 0;
}
if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) {
$dt = new DateTime(); $dt = new DateTime();
$dt->setTimestamp($dateTimeOrTimestamp); $dt->setTimestamp($dateTimeOrTimestamp);
@ -122,6 +134,10 @@ class Format
*/ */
public static function secondsByYear($dateTimeOrTimestamp) public static function secondsByYear($dateTimeOrTimestamp)
{ {
if ($dateTimeOrTimestamp === null) {
return 0;
}
return (self::isLeapYear($dateTimeOrTimestamp) ? 366 : 365) * 24 * 3600; return (self::isLeapYear($dateTimeOrTimestamp) ? 366 : 365) * 24 * 3600;
} }
@ -134,6 +150,10 @@ class Format
*/ */
public static function isLeapYear($dateTimeOrTimestamp) public static function isLeapYear($dateTimeOrTimestamp)
{ {
if ($dateTimeOrTimestamp === null) {
return false;
}
if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) {
$dt = new DateTime(); $dt = new DateTime();
$dt->setTimestamp($dateTimeOrTimestamp); $dt->setTimestamp($dateTimeOrTimestamp);