Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix timestampable use of datetimeclass #2017

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

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 Expand Up @@ -101,9 +103,17 @@ protected function getColumnConstant(string $columnName, AbstractOMBuilder $buil
public function preUpdate(AbstractOMBuilder $builder): string
{
if ($this->withUpdatedAt()) {
$valueSource = strtoupper($this->getTable()->getColumn($this->getParameter('update_column'))->getType()) === 'INTEGER'
$updateColumn = $this->getTable()->getColumn($this->getParameter('update_column'));

$dateTimeClass = DateTime::class;

if ($builder instanceof ObjectBuilder) {
$dateTimeClass = $builder->getDateTimeClass($updateColumn);
}

$valueSource = strtoupper($updateColumn->getType()) === 'INTEGER'
? 'time()'
: '\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision()';
: "PropelDateTime::createHighPrecision(null, '$dateTimeClass')";

return 'if ($this->isModified() && !$this->isColumnModified(' . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "({$valueSource});
Expand All @@ -123,22 +133,44 @@ public function preUpdate(AbstractOMBuilder $builder): string
public function preInsert(AbstractOMBuilder $builder): string
{
$script = '$time = time();
$highPrecision = \\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision();';
$mtime = PropelDateTime::formatMicrotime(microtime(true));';

if ($this->withCreatedAt()) {
$valueSource = strtoupper($this->getTable()->getColumn($this->getParameter('create_column'))->getType()) === 'INTEGER'
$createColumn = $this->getTable()->getColumn($this->getParameter('create_column'));

$dateTimeClass = DateTime::class;

if ($builder instanceof ObjectBuilder) {
$dateTimeClass = $builder->getDateTimeClass($createColumn);
}

$script .= "
\$highPrecisionCreate = PropelDateTime::createHighPrecision(\$mtime, '$dateTimeClass');";

$valueSource = strtoupper($createColumn->getType()) === 'INTEGER'
? '$time'
: '$highPrecision';
: '$highPrecisionCreate';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('create_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('create_column') . "({$valueSource});
}";
}

if ($this->withUpdatedAt()) {
$valueSource = strtoupper($this->getTable()->getColumn($this->getParameter('update_column'))->getType()) === 'INTEGER'
$updateColumn = $this->getTable()->getColumn($this->getParameter('update_column'));

$dateTimeClass = DateTime::class;

if ($builder instanceof ObjectBuilder) {
$dateTimeClass = $builder->getDateTimeClass($updateColumn);
}

$script .= "
\$highPrecisionUpdate = PropelDateTime::createHighPrecision(\$mtime, '$dateTimeClass');";

$valueSource = strtoupper($updateColumn->getType()) === 'INTEGER'
? '$time'
: '$highPrecision';
: '$highPrecisionUpdate';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "({$valueSource});
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Builder/Om/ObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7359,7 +7359,7 @@ protected function addMagicCall(string &$script): void
*
* @return string
*/
protected function getDateTimeClass(Column $column): string
public function getDateTimeClass(Column $column): string
{
if (PropelTypes::isPhpObjectType($column->getPhpType())) {
return $column->getPhpType();
Expand Down
32 changes: 26 additions & 6 deletions 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,34 +71,53 @@ 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 \DateTime
* @return \DateTimeInterface An instance of $dateTimeClass
*/
public static function createHighPrecision(?string $time = null): DateTime
public static function createHighPrecision(?string $time = null, string $dateTimeClass = 'DateTime'): DateTimeInterface
smhg marked this conversation as resolved.
Show resolved Hide resolved
{
$dateTime = DateTime::createFromFormat('U.u', $time ?: self::getMicrotime());
$allowStringIsA = true;

if (!is_a($dateTimeClass, DateTime::class, $allowStringIsA) && !is_a($dateTimeClass, DateTimeImmutable::class, $allowStringIsA)) {
smhg marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidArgumentException('`' . $dateTimeClass . '` needs to be an instance of DateTime or DateTimeImmutable');
}

$dateTime = $dateTimeClass::createFromFormat('U.u', $time ?: self::getMicrotime());
smhg marked this conversation as resolved.
Show resolved Hide resolved
if ($dateTime === false) {
throw new InvalidArgumentException('Cannot create a datetime object from `' . $time . '`');
}

$dateTime->setTimeZone(new DateTimeZone(date_default_timezone_get()));
$dateTime = $dateTime->setTimeZone(new DateTimeZone(date_default_timezone_get()));

return $dateTime;
}

/**
* Get the current microtime with milliseconds. Making sure that the decimal point separator is always ".", ignoring
* 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
{
return number_format($mtime, 6, '.', '');
}

/**
* Get the current microtime with milliseconds.
*
* @return string
*/
public static function getMicrotime(): string
{
$mtime = microtime(true);

return number_format($mtime, 6, '.', '');
return self::formatMicrotime($mtime);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Propel/Tests/Runtime/Util/PropelDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
use Propel\Runtime\Exception\PropelException;
Expand Down Expand Up @@ -245,11 +246,11 @@ public function testIsTimestamp()
public function testCreateHighPrecision()
{
$createHP = PropelDateTime::createHighPrecision();
$this->assertInstanceOf(DateTime::class, $createHP);
$this->assertInstanceOf(DateTimeInterface::class, $createHP);

setlocale(LC_ALL, 'de_DE.UTF-8');
$createHP = PropelDateTime::createHighPrecision();
$this->assertInstanceOf(DateTime::class, $createHP);
$this->assertInstanceOf(DateTimeInterface::class, $createHP);
}

/**
Expand Down
Loading