-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add support for DateTimeImmutable #19889
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,12 +33,13 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer | |
* @param string $outputTimezone The output timezone | ||
* @param array $fields The date fields | ||
* @param bool $pad Whether to use padding | ||
* @param bool $immutable Whether to use \DateTimeImmutable instead of \DateTime | ||
* | ||
* @throws UnexpectedTypeException if a timezone is not a string | ||
*/ | ||
public function __construct($inputTimezone = null, $outputTimezone = null, array $fields = null, $pad = false) | ||
public function __construct($inputTimezone = null, $outputTimezone = null, array $fields = null, $pad = false, $immutable = false) | ||
{ | ||
parent::__construct($inputTimezone, $outputTimezone); | ||
parent::__construct($inputTimezone, $outputTimezone, $immutable); | ||
|
||
if (null === $fields) { | ||
$fields = array('year', 'month', 'day', 'hour', 'minute', 'second'); | ||
|
@@ -108,7 +109,7 @@ public function transform($dateTime) | |
* | ||
* @param array $value Localized date | ||
* | ||
* @return \DateTime Normalized date | ||
* @return \DateTimeInterface Normalized date | ||
* | ||
* @throws TransformationFailedException If the given value is not an array, | ||
* if the value could not be transformed | ||
|
@@ -170,7 +171,8 @@ public function reverseTransform($value) | |
} | ||
|
||
try { | ||
$dateTime = new \DateTime(sprintf( | ||
$dateTimeClass = $this->getDateTimeClass(); | ||
$dateTime = new $dateTimeClass(sprintf( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to |
||
'%s-%s-%s %s:%s:%s', | ||
empty($value['year']) ? '1970' : $value['year'], | ||
empty($value['month']) ? '1' : $value['month'], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the hint should be explicit here IMO:
@return \DateTime|DateTimeImmutable
since no other implementation is used.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think leaving it as
\DateTimeInterface
is still semantically correct, and is more concise