Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bef2047

Browse files
-
1 parent 19d2422 commit bef2047

7 files changed

+46
-43
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class BaseDateTimeTransformer implements DataTransformerInterface
3939
*
4040
* @throws InvalidArgumentException if a timezone is not valid
4141
*/
42-
public function __construct(string $inputTimezone = null, string $outputTimezone = null)
42+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, protected readonly bool $immutable = false)
4343
{
4444
$this->inputTimezone = $inputTimezone ?: date_default_timezone_get();
4545
$this->outputTimezone = $outputTimezone ?: date_default_timezone_get();

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
3333
* @param string[]|null $fields The date fields
3434
* @param bool $pad Whether to use padding
3535
*/
36-
public function __construct(string $inputTimezone = null, string $outputTimezone = null, array $fields = null, bool $pad = false, \DateTimeInterface $referenceDate = null)
36+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, array $fields = null, bool $pad = false, \DateTimeInterface $referenceDate = null, bool $immutable = false)
3737
{
38-
parent::__construct($inputTimezone, $outputTimezone);
38+
parent::__construct($inputTimezone, $outputTimezone, $immutable);
3939

4040
$this->fields = $fields ?? ['year', 'month', 'day', 'hour', 'minute', 'second'];
4141
$this->pad = $pad;
@@ -45,7 +45,7 @@ public function __construct(string $inputTimezone = null, string $outputTimezone
4545
/**
4646
* Transforms a normalized date into a localized date.
4747
*
48-
* @param \DateTimeInterface $dateTime A DateTimeInterface object
48+
* @param \DateTimeInterface $dateTime
4949
*
5050
* @throws TransformationFailedException If the given value is not a \DateTimeInterface
5151
*/
@@ -100,7 +100,7 @@ public function transform(mixed $dateTime): array
100100
* @throws TransformationFailedException If the given value is not an array,
101101
* if the value could not be transformed
102102
*/
103-
public function reverseTransform(mixed $value): ?\DateTime
103+
public function reverseTransform(mixed $value): ?\DateTimeInterface
104104
{
105105
if (null === $value) {
106106
return null;
@@ -155,7 +155,8 @@ public function reverseTransform(mixed $value): ?\DateTime
155155
}
156156

157157
try {
158-
$dateTime = new \DateTime(sprintf(
158+
$class = $this->immutable ? 'DateTimeImmutable' : 'DateTime';
159+
$dateTime = new $class(sprintf(
159160
'%s-%s-%s %s:%s:%s',
160161
empty($value['year']) ? $this->referenceDate->format('Y') : $value['year'],
161162
empty($value['month']) ? $this->referenceDate->format('m') : $value['month'],
@@ -168,7 +169,7 @@ public function reverseTransform(mixed $value): ?\DateTime
168169
);
169170

170171
if ($this->inputTimezone !== $this->outputTimezone) {
171-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
172+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
172173
}
173174
} catch (\Exception $e) {
174175
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToHtml5LocalDateTimeTransformer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ class DateTimeToHtml5LocalDateTimeTransformer extends BaseDateTimeTransformer
2525
public const HTML5_FORMAT = 'Y-m-d\\TH:i:s';
2626
public const HTML5_FORMAT_NO_SECONDS = 'Y-m-d\\TH:i';
2727

28-
public function __construct(string $inputTimezone = null, string $outputTimezone = null, private bool $withSeconds = false)
28+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, private bool $withSeconds = false, bool $immutable = false)
2929
{
30-
parent::__construct($inputTimezone, $outputTimezone);
30+
parent::__construct($inputTimezone, $outputTimezone, $immutable);
3131
}
3232

3333
/**
34-
* Transforms a \DateTime into a local date and time string.
34+
* Transforms a DateTime into a local date and time string.
3535
*
3636
* According to the HTML standard, the input string of a datetime-local
3737
* input is an RFC3339 date followed by 'T', followed by an RFC3339 time.
3838
* https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
3939
*
4040
* @param \DateTimeInterface $dateTime
4141
*
42-
* @throws TransformationFailedException If the given value is not an
43-
* instance of \DateTime or \DateTimeInterface
42+
* @throws TransformationFailedException If the given value is not a \DateTimeInterface
4443
*/
4544
public function transform(mixed $dateTime): string
4645
{
@@ -61,7 +60,7 @@ public function transform(mixed $dateTime): string
6160
}
6261

6362
/**
64-
* Transforms a local date and time string into a \DateTime.
63+
* Transforms a local date and time string into a DateTimeInterface.
6564
*
6665
* When transforming back to DateTime the regex is slightly laxer, taking into
6766
* account rules for parsing a local date and time string
@@ -72,7 +71,7 @@ public function transform(mixed $dateTime): string
7271
* @throws TransformationFailedException If the given value is not a string,
7372
* if the value could not be transformed
7473
*/
75-
public function reverseTransform(mixed $dateTimeLocal): ?\DateTime
74+
public function reverseTransform(mixed $dateTimeLocal): ?\DateTimeInterface
7675
{
7776
if (!\is_string($dateTimeLocal)) {
7877
throw new TransformationFailedException('Expected a string.');
@@ -89,13 +88,14 @@ public function reverseTransform(mixed $dateTimeLocal): ?\DateTime
8988
}
9089

9190
try {
92-
$dateTime = new \DateTime($dateTimeLocal, new \DateTimeZone($this->outputTimezone));
91+
$class = $this->immutable ? 'DateTimeImmutable' : 'DateTime';
92+
$dateTime = new $class($dateTimeLocal, new \DateTimeZone($this->outputTimezone));
9393
} catch (\Exception $e) {
9494
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
9595
}
9696

9797
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
98-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
98+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
9999
}
100100

101101
if (!checkdate($matches[2], $matches[3], $matches[1])) {

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
4141
*
4242
* @throws UnexpectedTypeException If a format is not supported or if a timezone is not a string
4343
*/
44-
public function __construct(string $inputTimezone = null, string $outputTimezone = null, int $dateFormat = null, int $timeFormat = null, int $calendar = \IntlDateFormatter::GREGORIAN, string $pattern = null)
44+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, int $dateFormat = null, int $timeFormat = null, int $calendar = \IntlDateFormatter::GREGORIAN, string $pattern = null, bool $immutable = false)
4545
{
46-
parent::__construct($inputTimezone, $outputTimezone);
46+
parent::__construct($inputTimezone, $outputTimezone, $immutable);
4747

4848
$dateFormat ??= \IntlDateFormatter::MEDIUM;
4949
$timeFormat ??= \IntlDateFormatter::SHORT;
@@ -65,7 +65,7 @@ public function __construct(string $inputTimezone = null, string $outputTimezone
6565
/**
6666
* Transforms a normalized date into a localized date string/array.
6767
*
68-
* @param \DateTimeInterface $dateTime A DateTimeInterface object
68+
* @param \DateTimeInterface $dateTime
6969
*
7070
* @throws TransformationFailedException if the given value is not a \DateTimeInterface
7171
* or if the date could not be transformed
@@ -97,7 +97,7 @@ public function transform(mixed $dateTime): string
9797
* @throws TransformationFailedException if the given value is not a string,
9898
* if the date could not be parsed
9999
*/
100-
public function reverseTransform(mixed $value): ?\DateTime
100+
public function reverseTransform(mixed $value): ?\DateTimeInterface
101101
{
102102
if (!\is_string($value)) {
103103
throw new TransformationFailedException('Expected a string.');
@@ -130,22 +130,23 @@ public function reverseTransform(mixed $value): ?\DateTime
130130
}
131131

132132
try {
133+
$class = $this->immutable ? 'DateTimeImmutable' : 'DateTime';
133134
if ($dateOnly) {
134135
// we only care about year-month-date, which has been delivered as a timestamp pointing to UTC midnight
135-
$dateTime = new \DateTime(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->outputTimezone));
136+
$dateTime = new $class(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->outputTimezone));
136137
} else {
137138
// read timestamp into DateTime object - the formatter delivers a timestamp
138-
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
139+
$dateTime = new $class(sprintf('@%s', $timestamp));
139140
}
140141
// set timezone separately, as it would be ignored if set via the constructor,
141142
// see https://php.net/datetime.construct
142-
$dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
143+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
143144
} catch (\Exception $e) {
144145
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
145146
}
146147

147148
if ($this->outputTimezone !== $this->inputTimezone) {
148-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
149+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
149150
}
150151

151152
return $dateTime;

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
2323
/**
2424
* Transforms a normalized date into a localized date.
2525
*
26-
* @param \DateTimeInterface $dateTime A DateTimeInterface object
26+
* @param \DateTimeInterface $dateTime
2727
*
2828
* @throws TransformationFailedException If the given value is not a \DateTimeInterface
2929
*/
@@ -53,7 +53,7 @@ public function transform(mixed $dateTime): string
5353
* @throws TransformationFailedException If the given value is not a string,
5454
* if the value could not be transformed
5555
*/
56-
public function reverseTransform(mixed $rfc3339): ?\DateTime
56+
public function reverseTransform(mixed $rfc3339): ?\DateTimeInterface
5757
{
5858
if (!\is_string($rfc3339)) {
5959
throw new TransformationFailedException('Expected a string.');
@@ -68,13 +68,13 @@ public function reverseTransform(mixed $rfc3339): ?\DateTime
6868
}
6969

7070
try {
71-
$dateTime = new \DateTime($rfc3339);
71+
$dateTime = $this->immutable ? new \DateTimeImmutable($rfc3339) : new \DateTime($rfc3339);
7272
} catch (\Exception $e) {
7373
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
7474
}
7575

7676
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
77-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
77+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
7878
}
7979

8080
if (!checkdate($matches[2], $matches[3], $matches[1])) {

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
3838
private string $parseFormat;
3939

4040
/**
41-
* Transforms a \DateTime instance to a string.
41+
* Transforms a DateTimeInterface instance to a string.
4242
*
43-
* @see \DateTime::format() for supported formats
43+
* @see \DateTimeInterface::format() for supported formats
4444
*
4545
* @param string|null $inputTimezone The name of the input timezone
4646
* @param string|null $outputTimezone The name of the output timezone
4747
* @param string $format The date format
4848
* @param string|null $parseFormat The parse format when different from $format
4949
*/
50-
public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s', string $parseFormat = null)
50+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s', string $parseFormat = null, bool $immutable = false)
5151
{
52-
parent::__construct($inputTimezone, $outputTimezone);
52+
parent::__construct($inputTimezone, $outputTimezone, $immutable);
5353

5454
$this->generateFormat = $format;
5555
$this->parseFormat = $parseFormat ?? $format;
@@ -71,7 +71,7 @@ public function __construct(string $inputTimezone = null, string $outputTimezone
7171
* Transforms a DateTime object into a date string with the configured format
7272
* and timezone.
7373
*
74-
* @param \DateTimeInterface $dateTime A DateTimeInterface object
74+
* @param \DateTimeInterface $dateTime
7575
*
7676
* @throws TransformationFailedException If the given value is not a \DateTimeInterface
7777
*/
@@ -99,7 +99,7 @@ public function transform(mixed $dateTime): string
9999
* @throws TransformationFailedException If the given value is not a string,
100100
* or could not be transformed
101101
*/
102-
public function reverseTransform(mixed $value): ?\DateTime
102+
public function reverseTransform(mixed $value): ?\DateTimeInterface
103103
{
104104
if (empty($value)) {
105105
return null;
@@ -110,17 +110,18 @@ public function reverseTransform(mixed $value): ?\DateTime
110110
}
111111

112112
$outputTz = new \DateTimeZone($this->outputTimezone);
113-
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
113+
$class = $this->immutable ? 'DateTimeImmutable' : 'DateTime';
114+
$dateTime = $class::createFromFormat($this->parseFormat, $value, $outputTz);
114115

115-
$lastErrors = \DateTime::getLastErrors() ?: ['error_count' => 0, 'warning_count' => 0];
116+
$lastErrors = $class::getLastErrors() ?: ['error_count' => 0, 'warning_count' => 0];
116117

117118
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
118119
throw new TransformationFailedException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors']))));
119120
}
120121

121122
try {
122123
if ($this->inputTimezone !== $this->outputTimezone) {
123-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
124+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
124125
}
125126
} catch (\Exception $e) {
126127
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
2626
/**
2727
* Transforms a DateTime object into a timestamp in the configured timezone.
2828
*
29-
* @param \DateTimeInterface $dateTime A DateTimeInterface object
29+
* @param \DateTimeInterface $dateTime
3030
*
3131
* @throws TransformationFailedException If the given value is not a \DateTimeInterface
3232
*/
@@ -51,7 +51,7 @@ public function transform(mixed $dateTime): ?int
5151
* @throws TransformationFailedException If the given value is not a timestamp
5252
* or if the given timestamp is invalid
5353
*/
54-
public function reverseTransform(mixed $value): ?\DateTime
54+
public function reverseTransform(mixed $value): ?\DateTimeInterface
5555
{
5656
if (null === $value) {
5757
return null;
@@ -62,12 +62,12 @@ public function reverseTransform(mixed $value): ?\DateTime
6262
}
6363

6464
try {
65-
$dateTime = new \DateTime();
66-
$dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
67-
$dateTime->setTimestamp($value);
65+
$dateTime = $this->immutable ? new \DateTimeImmutable() : new \DateTime();
66+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
67+
$dateTime = $dateTime->setTimestamp($value);
6868

6969
if ($this->inputTimezone !== $this->outputTimezone) {
70-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
70+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
7171
}
7272
} catch (\Exception $e) {
7373
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);

0 commit comments

Comments
 (0)