Skip to content

Commit

Permalink
Ensure DateType marshalling works for both mutable and immutable types
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Aug 14, 2020
1 parent 0296bc8 commit b534211
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Type/DateTimeType.php
Expand Up @@ -449,9 +449,9 @@ public function useMutable()
* @param string $value The value to parse and convert to an object.
* @return \Cake\I18n\I18nDateTimeInterface|null
*/
protected function _parseLocaleValue(string $value)
protected function _parseLocaleValue(string $value): ?I18nDateTimeInterface
{
/** @var \Cake\I18n\I18nDateTimeInterface $class */
/** @psalm-var class-string<\Cake\I18n\I18nDateTimeInterface> $class */
$class = $this->_className;

return $class::parseDateTime($value, $this->_localeMarshalFormat);
Expand All @@ -464,9 +464,9 @@ protected function _parseLocaleValue(string $value)
* @param string $value The value to parse and convert to an object.
* @return \DateTimeInterface|null
*/
protected function _parseValue(string $value)
protected function _parseValue(string $value): ?DateTimeInterface
{
/** @var \DateTime|\DateTimeImmutable $class */
/** @psalm-var class-string<\DateTime>|class-string<\DateTimeImmutable> $class */
$class = $this->_className;

foreach ($this->_marshalFormats as $format) {
Expand Down
9 changes: 6 additions & 3 deletions Type/DateType.php
Expand Up @@ -18,6 +18,7 @@

use Cake\I18n\Date;
use Cake\I18n\FrozenDate;
use Cake\I18n\I18nDateTimeInterface;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
Expand Down Expand Up @@ -80,7 +81,9 @@ public function useMutable()
public function marshal($value): ?DateTimeInterface
{
$date = parent::marshal($value);
if ($date instanceof DateTime) {
if ($date && !$date instanceof I18nDateTimeInterface) {
// Clear time manually when I18n types aren't available and raw DateTime used
/** @psalm-var \DateTime|\DateTimeImmutable $date */
$date->setTime(0, 0, 0);
}

Expand All @@ -90,9 +93,9 @@ public function marshal($value): ?DateTimeInterface
/**
* @inheritDoc
*/
protected function _parseLocaleValue(string $value)
protected function _parseLocaleValue(string $value): ?I18nDateTimeInterface
{
/** @var \Cake\I18n\I18nDateTimeInterface $class */
/** @psalm-var class-string<\Cake\I18n\I18nDateTimeInterface> $class */
$class = $this->_className;

return $class::parseDate($value, $this->_localeMarshalFormat);
Expand Down
6 changes: 4 additions & 2 deletions Type/TimeType.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\Database\Type;

use Cake\I18n\I18nDateTimeInterface;

/**
* Time type converter.
*
Expand All @@ -38,9 +40,9 @@ class TimeType extends DateTimeType
/**
* @inheritDoc
*/
protected function _parseLocaleValue(string $value)
protected function _parseLocaleValue(string $value): ?I18nDateTimeInterface
{
/** @var \Cake\I18n\I18nDateTimeInterface $class */
/** @psalm-var class-string<\Cake\I18n\I18nDateTimeInterface> $class */
$class = $this->_className;

/** @psalm-suppress PossiblyInvalidArgument */
Expand Down

0 comments on commit b534211

Please sign in to comment.