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

Skip to content

Commit 657f7ec

Browse files
minor #22911 [Form] Remove DateTimeToStringTransformer $parseUsingPipe option (ogizanagi)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Remove DateTimeToStringTransformer $parseUsingPipe option | Q | A | ------------- | --- | Branch? | 2.7 <!-- see comment below --> | Bug fix? | no | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A Looks like this part of the code should not have been used anymore since 2 years 😅 (6dc8979) Commits ------- a841496 [Form] Remove DateTimeToStringTransformer $parseUsingPipe option
2 parents 49d6604 + a841496 commit 657f7ec

File tree

2 files changed

+4
-92
lines changed

2 files changed

+4
-92
lines changed

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

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
4040
*/
4141
private $parseFormat;
4242

43-
/**
44-
* Whether to parse by appending a pipe "|" to the parse format.
45-
*
46-
* This only works as of PHP 5.3.7.
47-
*
48-
* @var bool
49-
*/
50-
private $parseUsingPipe;
51-
5243
/**
5344
* Transforms a \DateTime instance to a string.
5445
*
@@ -57,18 +48,15 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
5748
* @param string $inputTimezone The name of the input timezone
5849
* @param string $outputTimezone The name of the output timezone
5950
* @param string $format The date format
60-
* @param bool $parseUsingPipe Whether to parse by appending a pipe "|" to the parse format
6151
*
6252
* @throws UnexpectedTypeException if a timezone is not a string
6353
*/
64-
public function __construct($inputTimezone = null, $outputTimezone = null, $format = 'Y-m-d H:i:s', $parseUsingPipe = true)
54+
public function __construct($inputTimezone = null, $outputTimezone = null, $format = 'Y-m-d H:i:s')
6555
{
6656
parent::__construct($inputTimezone, $outputTimezone);
6757

6858
$this->generateFormat = $this->parseFormat = $format;
6959

70-
$this->parseUsingPipe = $parseUsingPipe || null === $parseUsingPipe;
71-
7260
// See http://php.net/manual/en/datetime.createfromformat.php
7361
// The character "|" in the format makes sure that the parts of a date
7462
// that are *not* specified in the format are reset to the corresponding
@@ -77,7 +65,7 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $form
7765
// where the time corresponds to the current server time.
7866
// With "|" and "Y-m-d", "2010-02-03" becomes "2010-02-03 00:00:00",
7967
// which is at least deterministic and thus used here.
80-
if ($this->parseUsingPipe && false === strpos($this->parseFormat, '|')) {
68+
if (false === strpos($this->parseFormat, '|')) {
8169
$this->parseFormat .= '|';
8270
}
8371
}
@@ -147,70 +135,6 @@ public function reverseTransform($value)
147135
}
148136

149137
try {
150-
// On PHP versions < 5.3.7 we need to emulate the pipe operator
151-
// and reset parts not given in the format to their equivalent
152-
// of the UNIX base timestamp.
153-
if (!$this->parseUsingPipe) {
154-
list($year, $month, $day, $hour, $minute, $second) = explode('-', $dateTime->format('Y-m-d-H-i-s'));
155-
156-
// Check which of the date parts are present in the pattern
157-
preg_match(
158-
'/('.
159-
'(?P<day>[djDl])|'.
160-
'(?P<month>[FMmn])|'.
161-
'(?P<year>[Yy])|'.
162-
'(?P<hour>[ghGH])|'.
163-
'(?P<minute>i)|'.
164-
'(?P<second>s)|'.
165-
'(?P<dayofyear>z)|'.
166-
'(?P<timestamp>U)|'.
167-
'[^djDlFMmnYyghGHiszU]'.
168-
')*/',
169-
$this->parseFormat,
170-
$matches
171-
);
172-
173-
// preg_match() does not guarantee to set all indices, so
174-
// set them unless given
175-
$matches = array_merge(array(
176-
'day' => false,
177-
'month' => false,
178-
'year' => false,
179-
'hour' => false,
180-
'minute' => false,
181-
'second' => false,
182-
'dayofyear' => false,
183-
'timestamp' => false,
184-
), $matches);
185-
186-
// Reset all parts that don't exist in the format to the
187-
// corresponding part of the UNIX base timestamp
188-
if (!$matches['timestamp']) {
189-
if (!$matches['dayofyear']) {
190-
if (!$matches['day']) {
191-
$day = 1;
192-
}
193-
if (!$matches['month']) {
194-
$month = 1;
195-
}
196-
}
197-
if (!$matches['year']) {
198-
$year = 1970;
199-
}
200-
if (!$matches['hour']) {
201-
$hour = 0;
202-
}
203-
if (!$matches['minute']) {
204-
$minute = 0;
205-
}
206-
if (!$matches['second']) {
207-
$second = 0;
208-
}
209-
$dateTime->setDate($year, $month, $day);
210-
$dateTime->setTime($hour, $minute, $second);
211-
}
212-
}
213-
214138
if ($this->inputTimezone !== $this->outputTimezone) {
215139
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
216140
}

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToStringTransformerTest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,9 @@ public function testTransformExpectsDateTime()
120120
/**
121121
* @dataProvider dataProvider
122122
*/
123-
public function testReverseTransformUsingPipe($format, $input, $output)
123+
public function testReverseTransform($format, $input, $output)
124124
{
125-
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, true);
126-
127-
$output = new \DateTime($output);
128-
129-
$this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
130-
}
131-
132-
/**
133-
* @dataProvider dataProvider
134-
*/
135-
public function testReverseTransformWithoutUsingPipe($format, $input, $output)
136-
{
137-
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
125+
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);
138126

139127
$output = new \DateTime($output);
140128

0 commit comments

Comments
 (0)