-
Notifications
You must be signed in to change notification settings - Fork 41
DateTimeFormatterStrategy passes special characters used by date_create_from_format
to date_format
#69
DateTimeFormatterStrategy passes special characters used by date_create_from_format
to date_format
#69
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I just don't quite understand the purpose of this. Why would the format you provide to the DateTimeFormatterStrategy
contain these characters?
Additionally, this feels very much like a project-specific requirement which could be handled by either:
- Decoration (likely by decorating the constructor to filter the format before assignment)
- Domain-level code that triggers before creating the strategy instance.
I need more background to assess whether this is something we want to support.
@@ -48,7 +48,8 @@ public function __construct($format = DateTime::RFC3339, DateTimeZone $timezone | |||
public function extract($value) | |||
{ | |||
if ($value instanceof DateTime) { | |||
return $value->format($this->format); | |||
$extractionFormat = preg_replace('/(?<![\\\\])[+|!\*]/', '', $this->format); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For those reviewing the issue later to understand what it does, the above matches an of the characters +|!*
NOT preceded by a \\
.
@@ -48,7 +48,8 @@ public function __construct($format = DateTime::RFC3339, DateTimeZone $timezone | |||
public function extract($value) | |||
{ | |||
if ($value instanceof DateTime) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we don't accept DateTimeInterface
@weierophinney?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@malukenho We do as of #70. ;-)
@weierophinney Well, the thing is, if you use the Strategies to e.g. transport data via JSON by an API, the clients hydrate that value back into a datetime object and always uses the the current time if neither "!" at the beginning or "|" at the end is specified. This actually is pretty annoying if you want to use a date for comparison reasons. Lets assume the following example with the current time (10:14 pm): $data = [
'birthdate' => (new DateTime('-18 years'))->format('Y-m-d'),
];
$user = new class
{
/**
* @var DateTime
*/
public $birthdate;
public function isEighteen(): bool
{
$today = new DateTime();
$diff = $this->birthdate->diff($today, true);
return $diff->y >= 18;
}
public function wasEighteen(DateTime $specificDate): bool
{
$diff = $this->birthdate->diff($specificDate, true);
return $diff->y >= 18;
}
};
$hydrator = new Zend\Hydrator\Reflection;
$hydrator->addStrategy('birthdate', new Zend\Hydrator\Strategy\DateTimeFormatterStrategy('Y-m-d'));
$hydrator->hydrate($data, $user);
var_dump($user->isEighteen());
var_dump($user->wasEighteen(new DateTime('-5 minutes')));
Ofcourse, the user was already 18 even 5 minutes ago. |
…_format` special characters
…be passed to `date_format` on extraction
…etect format upon strategy creation
So, is there any possibility that this change is being merged? If not, please feel free to either close this PR or give me a short feedback. Thanks in advance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation, @boesing !
The only change I'd ask for now is a note in the documentation detailing that you can do this when you need to be concerned about comparisons.
DateTimeFormatterStrategy passes special characters used by `date_create_from_format` to `date_format`
Hey guys,
I would like to use special chars for the date format, e.g. to ensure that if I pass a MySQL
date
string to the hydrator, an object with time reset to 0 is being returned.Therefore, I had to strip un-escaped special chars (*, !, | and +) (documentation) from the format being passed to the
DateTimeInterface::format
method.