Skip to content

Reverse DateTimeToStringTransformer must use format #1134

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 0 commits into from
Jun 1, 2011

Conversation

hlecorche
Copy link
Contributor

In DateTimeToStringTransformer, transform function uses "format" but not "reverseTransform".

If the format is different than "Y-m-d", reverseTransform throws TransformationFailedException.

@stloyd
Copy link
Contributor

stloyd commented May 28, 2011

Can you also add additional test that would cover use case you mentioned ?

@hlecorche
Copy link
Contributor Author

Yes, I will add additional test.

But before, I met two bugs (one for "DateTime::createFromFormat"and the other one for "DateTime") :

  • "DateTime::createFromFormat" doesn't throw exception if date is invalid (it returns false)
  • "DateTime" tries to modify incorrect dates: If date is "2011-02-29", the outpout date will be "2011-03-01" (This bug is everywhere in symfony)

The solution looks like that:

public function reverseTransform($value)
    {
        if (empty($value)) {
            return null;
        }

        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }

        try {
            $dateTime = \DateTime::createFromFormat($this->format, $value, new \DateTimeZone($this->outputTimezone));
            $last_errors = \DateTime::getLastErrors();
            if($last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0){
                throw new \Exception('One message...');
            }

            if ($this->inputTimezone !== $this->outputTimezone) {
                $dateTime->setTimeZone(new \DateTimeZone($this->inputTimezone));
            }
        } catch (\Exception $e) {
            throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
        }

        return $dateTime;
    }

But, have you suggestions to replace the "One message..." (inside the Exception). Or other solutions?

@hlecorche hlecorche closed this May 28, 2011
@hlecorche hlecorche reopened this May 28, 2011
@stof
Copy link
Member

stof commented May 28, 2011

@hlecorche These are not a Symfony bugs but the expected behavior of DateTime objects.

@hlecorche
Copy link
Contributor Author

@stof: Yes, I agree, I expressed myself badly. It is not a bug, but do we accept this behavior for a form? (A user can give one invalid date)

And, with "DateTime::createFromFormat", we must check (because, contrary to "new DateTime()", it doesn't throw exception.

@hlecorche
Copy link
Contributor Author

I propose this code:

public function reverseTransform($value)
    {
        if (empty($value)) {
            return null;
        }

        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }

        try {
            $dateTime = \DateTime::createFromFormat($this->format, $value, new \DateTimeZone($this->outputTimezone));
            $last_errors = \DateTime::getLastErrors();
            if($last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0){
                throw new \Exception('Date is invalid.');
            }

            if ($this->inputTimezone !== $this->outputTimezone) {
                $dateTime->setTimeZone(new \DateTimeZone($this->inputTimezone));
            }
        } catch (\Exception $e) {
            throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
        }

        return $dateTime;
    }

This code allows :

  • To use "format"
  • To check if DateTime::createFromFormat doesn't return error (parsing error, ...). It's mandatory because DateTime::createFromFormat doesn't throw exception
  • To check if the date is valid (with DateTime, 2011-02-29 is valid and is converted to 2011-03-01. With this code, 2011-02-29 is invalid)

Do you agree? Do you have any suggestions?

If you agree, I will commit this code and will add additional tests.

@hlecorche hlecorche merged commit af2f920 into symfony:master Jun 1, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants