Skip to content

Commit

Permalink
refactor: extract getDateTimeFormat() method
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 29, 2024
1 parent c24497d commit 56c6c46
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions system/DataCaster/Cast/DatetimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ public static function get(
/**
* @see https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters
*/
$format = match ($params[0] ?? '') {
'' => $helper->dateFormat['datetime'],
'ms' => $helper->dateFormat['datetime-ms'],
'us' => $helper->dateFormat['datetime-us'],
default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
};
$format = self::getDateTimeFormat($params, $helper);

return Time::createFromFormat($format, $value);
}
Expand All @@ -68,13 +63,23 @@ public static function set(
throw new InvalidArgumentException($message);
}

$format = match ($params[0] ?? '') {
'' => $helper->dateFormat['datetime'],
'ms' => $helper->dateFormat['datetime-ms'],
'us' => $helper->dateFormat['datetime-us'],
default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
};
$format = self::getDateTimeFormat($params, $helper);

return $value->format($format);
}

/**
* Gets DateTime format from the DB connection.
*
* @param list<string> $params Additional param
*/
protected static function getDateTimeFormat(array $params, BaseConnection $db): string
{
return match ($params[0] ?? '') {
'' => $db->dateFormat['datetime'],
'ms' => $db->dateFormat['datetime-ms'],
'us' => $db->dateFormat['datetime-us'],
default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
};
}
}

0 comments on commit 56c6c46

Please sign in to comment.