[Form] fixed DateTime transformers
This commit is contained in:
parent
d7e466ac23
commit
b91008fb57
@ -56,8 +56,7 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
|
||||
* @return array Localized date.
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not an
|
||||
* instance of \DateTime or if the
|
||||
* output timezone is not supported.
|
||||
* instance of \DateTime or \DateTimeInterface
|
||||
*/
|
||||
public function transform($dateTime)
|
||||
{
|
||||
@ -81,11 +80,7 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
|
||||
$dateTime = clone $dateTime;
|
||||
}
|
||||
|
||||
try {
|
||||
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
|
||||
} catch (\Exception $e) {
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
$result = array_intersect_key(array(
|
||||
@ -118,8 +113,6 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not an array,
|
||||
* if the value could not be transformed
|
||||
* or if the input timezone is not
|
||||
* supported.
|
||||
*/
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
|
@ -75,8 +75,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
|
||||
* @return string|array Localized date string/array.
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not an instance
|
||||
* of \DateTime or if the date could not
|
||||
* be transformed.
|
||||
* of \DateTime or \DateTimeInterface or
|
||||
* if the date could not be transformed.
|
||||
*/
|
||||
public function transform($dateTime)
|
||||
{
|
||||
@ -105,8 +105,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
|
||||
* @return \DateTime Normalized date
|
||||
*
|
||||
* @throws TransformationFailedException if the given value is not a string,
|
||||
* if the date could not be parsed or
|
||||
* if the input timezone is not supported
|
||||
* if the date could not be parsed
|
||||
*/
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
@ -132,11 +131,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
|
||||
}
|
||||
|
||||
if ('UTC' !== $this->inputTimezone) {
|
||||
try {
|
||||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
|
||||
} catch (\Exception $e) {
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
return $dateTime;
|
||||
|
@ -19,7 +19,14 @@ use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Transforms a normalized date into a localized date.
|
||||
*
|
||||
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object
|
||||
*
|
||||
* @return string The formatted date.
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not an
|
||||
* instance of \DateTime or \DateTimeInterface
|
||||
*/
|
||||
public function transform($dateTime)
|
||||
{
|
||||
@ -32,7 +39,10 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
|
||||
}
|
||||
|
||||
if ($this->inputTimezone !== $this->outputTimezone) {
|
||||
if (!$dateTime instanceof \DateTimeImmutable) {
|
||||
$dateTime = clone $dateTime;
|
||||
}
|
||||
|
||||
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
|
||||
}
|
||||
|
||||
@ -40,7 +50,14 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Transforms a formatted string following RFC 3339 into a normalized date.
|
||||
*
|
||||
* @param string $rfc3339 Formatted string
|
||||
*
|
||||
* @return \DateTime Normalized date
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not a string,
|
||||
* if the value could not be transformed
|
||||
*/
|
||||
public function reverseTransform($rfc3339)
|
||||
{
|
||||
@ -58,12 +75,8 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) {
|
||||
try {
|
||||
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
|
||||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
|
||||
} catch (\Exception $e) {
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) {
|
||||
|
@ -86,35 +86,30 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
|
||||
* Transforms a DateTime object into a date string with the configured format
|
||||
* and timezone.
|
||||
*
|
||||
* @param \DateTime|\DateTimeInterface $value A DateTime object
|
||||
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object
|
||||
*
|
||||
* @return string A value as produced by PHP's date() function
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not a \DateTime
|
||||
* instance or if the output timezone
|
||||
* is not supported.
|
||||
* @throws TransformationFailedException If the given value is not an
|
||||
* instance of \DateTime or \DateTimeInterface
|
||||
*/
|
||||
public function transform($value)
|
||||
public function transform($dateTime)
|
||||
{
|
||||
if (null === $value) {
|
||||
if (null === $dateTime) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
|
||||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
|
||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
||||
}
|
||||
|
||||
if (!$value instanceof \DateTimeImmutable) {
|
||||
$value = clone $value;
|
||||
if (!$dateTime instanceof \DateTimeImmutable) {
|
||||
$dateTime = clone $dateTime;
|
||||
}
|
||||
|
||||
try {
|
||||
$value = $value->setTimezone(new \DateTimeZone($this->outputTimezone));
|
||||
} catch (\Exception $e) {
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
|
||||
|
||||
return $value->format($this->generateFormat);
|
||||
return $dateTime->format($this->generateFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,8 +120,7 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
|
||||
* @return \DateTime An instance of \DateTime
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not a string,
|
||||
* if the date could not be parsed or
|
||||
* if the input timezone is not supported.
|
||||
* or could not be transformed
|
||||
*/
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
|
@ -29,20 +29,19 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
|
||||
* @return int A timestamp
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not an instance
|
||||
* of \DateTime or if the output
|
||||
* timezone is not supported.
|
||||
* of \DateTime or \DateTimeInterface
|
||||
*/
|
||||
public function transform($value)
|
||||
public function transform($dateTime)
|
||||
{
|
||||
if (null === $value) {
|
||||
if (null === $dateTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
|
||||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
|
||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
||||
}
|
||||
|
||||
return $value->getTimestamp();
|
||||
return $dateTime->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,7 +52,7 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
|
||||
* @return \DateTime A \DateTime object
|
||||
*
|
||||
* @throws TransformationFailedException If the given value is not a timestamp
|
||||
* or if the given timestamp is invalid.
|
||||
* or if the given timestamp is invalid
|
||||
*/
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
|
Reference in New Issue
Block a user