Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

New DateTimeFormatter Filter (#3617) #3632

Closed
wants to merge 9 commits into from

Conversation

davidwindell
Copy link
Contributor

This PR introduces a simple DateTimeFormatter Filter class that will attempt to format a provided string using the specified date() style format.

The main advantage of this is to allow pre-formatting of a provided datetime value on a Zend\Form\Element\DateTime where the HTML5 spec and different browsers may pass values with or without seconds which can cause the validator to fail.

See #3617

* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2005-2013 :D

@marc-mabe
Copy link
Member

Changing the default format for Zend\Form should be done within a seperate PR and btw. removing the const is a BC break.

In my opinion the name should be DateTimeFormatter

@Maks3w
Copy link
Member

Maks3w commented Jan 31, 2013

Where are the tests? ;)

*
* @var string
*/
protected $format = self::DATETIME_FORMAT;
protected $format = PhpDateTime::W3C;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC Break

@davidwindell
Copy link
Contributor Author

@Maks3w tests attached

/**
* Sets filter options
*
* @param string|array|\Zend\Config\Config $options
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @param array|Traversable $options

@Maks3w
Copy link
Member

Maks3w commented Feb 1, 2013

@weierophinney What do you thing about this?

@ghost ghost assigned weierophinney Feb 15, 2013
namespace Zend\Filter;

use DateTime;
use Exception;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use an exception that is part of the Zend\Filter component? If no suitable exception exists you could create it, and have it extend the appropriate SPL exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second the recommendation from @Freeaqingme

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi both, I am "using" \Exception to catch the Exception thrown by DateTime (http://www.php.net/manual/en/datetime.construct.php) which is just \Exception. Are you saying I should create an Exception in Filter\Exception that Extends \Exception?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I'd remove the import, and use FQCN when catching:

catch (\Exception $e)

This will make it more clear that you actually intend to catch a global exception -- which is a rare occurrence in the framework.

@weierophinney
Copy link
Member

This looks good. @davidwindell - Make the changes requested, remove the "[WIP]" notation, and I'll do final review for merge.

@davidwindell
Copy link
Contributor Author

Thanks @weierophinney, I've made the requested changes.

$result = $this->normalizeDateTime($value);
} catch (\Exception $ex) {
// DateTime threw an exception, an invalid date string was provided
return $value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter should throw the exception else it returns not normalized dates

$filter->filter('now'); // 2013-03-13 10:00
$filter->filter('2013-03-13 10:00'); // 2013-03-13 10:00
$filter->filter('abcdefg'); // abcdefg ? -> please throw an Exception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marc-mabe this is not the behaviour of other filters, see https://github.com/zendframework/zf2/blob/master/library/Zend/Filter/UriNormalize.php#L105

Filters should not throw exceptions or they will cause problems with InputFilters

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats interesting because other filters throw exceptions - see https://github.com/zendframework/zf2/blob/master/library/Zend/Filter/HtmlEntities.php

@weierophinney What's the right way to go here? For me a filter should throw exceptions on failures else it's impossible to detect such failures (see my example above). Only implementations of Zend\Validator\ValidatorInterface::isValid shouldn't throw exceptions but the filters should do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now re throwing the blanket DateTime \Exception as a InvalidArgumentException

$result = $this->normalizeDateTime($value);
} catch (\Exception $ex) {
// DateTime threw an exception, an invalid date string was provided
throw new Exception\InvalidArgumentException($ex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be throw new Exception\InvalidArgumentException("Invalid input date '{$value}'", 0, $ex); to throw the exception of DateTime as previous exception

weierophinney added a commit that referenced this pull request Mar 25, 2013
New DateTimeFormatter Filter (#3617)
weierophinney added a commit that referenced this pull request Mar 25, 2013
- Incorporate feedback from @marc-mabe
- Also: use $e as exception (for consistency)
weierophinney added a commit that referenced this pull request Mar 25, 2013
@weierophinney
Copy link
Member

Merged.

@davidwindell davidwindell deleted the zf3617 branch March 25, 2013 14:14
weierophinney added a commit that referenced this pull request May 23, 2013
…-unaware-of-datetime-formatter

Add service definition for DateTimeFormatter (related to #3632)
weierophinney added a commit to zendframework/zend-filter that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-filter that referenced this pull request May 15, 2015
- Incorporate feedback from @marc-mabe
- Also: use $e as exception (for consistency)
weierophinney added a commit to zendframework/zend-filter that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-filter that referenced this pull request May 15, 2015
…otfix/filter-plugin-manager-unaware-of-datetime-formatter

Add service definition for DateTimeFormatter (related to zendframework/zendframework#3632)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants