beberlei / assert

Thin assertion library for use in libraries and business-model
Other
2.41k stars 188 forks source link

Why doesn't method stringify return the ISO timestamp when value implements DateTimeInterface? #279

Closed pfazzi closed 5 years ago

pfazzi commented 5 years ago

It could be useful that method Assertion::stringify($value) returns the ISO representation of the $value if the value argument implements DateTimeInterface. It will be good in cases like this:

Assertion::lessOrEqualThan(
    DateTimeImmutable::createFromFormat('!Y-m-d', $stringDate),
    new DateTimeImmutable('today')
);

The rendered message now is:

Provided "DateTimeImmutable" is not less or equal than "DateTimeImmutable".

I'd like to have something like:

Provided "2019-05-07" is not less or equal than "2019-05-05".
rquadling commented 5 years ago

The stringify function is a bit simplistic. Would need to be a major change for this as backward compatibility could be an issue for anyone working with the current pattern. Though, you could extend the Assertion class and present your own stringify method.

pfazzi commented 5 years ago

That is true, it can cause backward compatibility. I would not like to extend the class so i'll look for another solution. Thanks anyway.