diff --git a/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php b/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php index 17c01a6fc..bb54b020e 100644 --- a/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php +++ b/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php @@ -8,6 +8,7 @@ namespace Propel\Generator\Behavior\Timestampable; +use DateTime; use Propel\Generator\Builder\Om\AbstractOMBuilder; use Propel\Generator\Builder\Om\ObjectBuilder; use Propel\Generator\Model\Behavior; diff --git a/src/Propel/Runtime/Util/PropelDateTime.php b/src/Propel/Runtime/Util/PropelDateTime.php index 679d540b8..1af002e46 100644 --- a/src/Propel/Runtime/Util/PropelDateTime.php +++ b/src/Propel/Runtime/Util/PropelDateTime.php @@ -9,6 +9,7 @@ namespace Propel\Runtime\Util; use DateTime; +use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; use Exception; @@ -70,13 +71,18 @@ protected static function isTimestamp($value): bool * Usually `new \Datetime()` does not contain milliseconds so you need a method like this. * * @param string|null $time Optional, in seconds. Floating point allowed. + * @param string $dateTimeClass Optional, class name of the created object. * * @throws \InvalidArgumentException * - * @return DateTimeInterface An instance of $dateTimeClass + * @return \DateTimeInterface An instance of $dateTimeClass */ public static function createHighPrecision(?string $time = null, string $dateTimeClass = 'DateTime'): DateTimeInterface { + if (!is_subclass_of($dateTimeClass, DateTime::class) && !is_subclass_of($dateTimeClass, DateTimeImmutable::class)) { + throw new InvalidArgumentException('`' . $dateTimeClass . '` needs to be an instance of DateTime or DateTimeImmutable'); + } + $dateTime = $dateTimeClass::createFromFormat('U.u', $time ?: self::getMicrotime()); if ($dateTime === false) { throw new InvalidArgumentException('Cannot create a datetime object from `' . $time . '`'); @@ -91,6 +97,8 @@ public static function createHighPrecision(?string $time = null, string $dateTim * Format the output of microtime(true) making sure that the decimal point separator is always ".", ignoring * what is set with the current locale. Otherwise, self::createHighPrecision would return false. * + * @param float $mtime Time in milliseconds. + * * @return string */ public static function formatMicrotime(float $mtime): string