Skip to content

Commit

Permalink
add class check
Browse files Browse the repository at this point in the history
  • Loading branch information
smhg committed Dec 10, 2024
1 parent 531713a commit 289ca29
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/Propel/Runtime/Util/PropelDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Propel\Runtime\Util;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Exception;
Expand Down Expand Up @@ -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 . '`');
Expand All @@ -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
Expand Down

0 comments on commit 289ca29

Please sign in to comment.