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

Skip to content

[Form] Remove DateTimeToStringTransformer $parseUsingPipe option #22911

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

Merged
merged 1 commit into from
May 25, 2017
Merged
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 @@ -40,15 +40,6 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
*/
private $parseFormat;

/**
* Whether to parse by appending a pipe "|" to the parse format.
*
* This only works as of PHP 5.3.7.
*
* @var bool
*/
private $parseUsingPipe;

/**
* Transforms a \DateTime instance to a string.
*
Expand All @@ -57,18 +48,15 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
* @param string $inputTimezone The name of the input timezone
* @param string $outputTimezone The name of the output timezone
* @param string $format The date format
* @param bool $parseUsingPipe Whether to parse by appending a pipe "|" to the parse format
*
* @throws UnexpectedTypeException if a timezone is not a string
*/
public function __construct($inputTimezone = null, $outputTimezone = null, $format = 'Y-m-d H:i:s', $parseUsingPipe = true)
public function __construct($inputTimezone = null, $outputTimezone = null, $format = 'Y-m-d H:i:s')
{
parent::__construct($inputTimezone, $outputTimezone);

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

$this->parseUsingPipe = $parseUsingPipe || null === $parseUsingPipe;

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

try {
// On PHP versions < 5.3.7 we need to emulate the pipe operator
// and reset parts not given in the format to their equivalent
// of the UNIX base timestamp.
if (!$this->parseUsingPipe) {
list($year, $month, $day, $hour, $minute, $second) = explode('-', $dateTime->format('Y-m-d-H-i-s'));

// Check which of the date parts are present in the pattern
preg_match(
'/('.
'(?P<day>[djDl])|'.
'(?P<month>[FMmn])|'.
'(?P<year>[Yy])|'.
'(?P<hour>[ghGH])|'.
'(?P<minute>i)|'.
'(?P<second>s)|'.
'(?P<dayofyear>z)|'.
'(?P<timestamp>U)|'.
'[^djDlFMmnYyghGHiszU]'.
')*/',
$this->parseFormat,
$matches
);

// preg_match() does not guarantee to set all indices, so
// set them unless given
$matches = array_merge(array(
'day' => false,
'month' => false,
'year' => false,
'hour' => false,
'minute' => false,
'second' => false,
'dayofyear' => false,
'timestamp' => false,
), $matches);

// Reset all parts that don't exist in the format to the
// corresponding part of the UNIX base timestamp
if (!$matches['timestamp']) {
if (!$matches['dayofyear']) {
if (!$matches['day']) {
$day = 1;
}
if (!$matches['month']) {
$month = 1;
}
}
if (!$matches['year']) {
$year = 1970;
}
if (!$matches['hour']) {
$hour = 0;
}
if (!$matches['minute']) {
$minute = 0;
}
if (!$matches['second']) {
$second = 0;
}
$dateTime->setDate($year, $month, $day);
$dateTime->setTime($hour, $minute, $second);
}
}

if ($this->inputTimezone !== $this->outputTimezone) {
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,9 @@ public function testTransformExpectsDateTime()
/**
* @dataProvider dataProvider
*/
public function testReverseTransformUsingPipe($format, $input, $output)
public function testReverseTransform($format, $input, $output)
{
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, true);

$output = new \DateTime($output);

$this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
}

/**
* @dataProvider dataProvider
*/
public function testReverseTransformWithoutUsingPipe($format, $input, $output)
{
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);

$output = new \DateTime($output);

Expand Down