Conversation
Implements a DateInterval wrapper using the decorator pattern as discussed. This addresses the need for a proper interval class with: - ISO 8601 duration string formatting via __toString() - Factory methods: create(), createFromValues(), instance() - toNative() for compatibility with code expecting DateInterval - Convenience methods: totalSeconds(), totalDays(), isNegative(), isZero() - Property access proxy to underlying DateInterval Related to #444
Fixes PHPStan level 8 'Unsafe usage of new static()' errors.
|
markstory
left a comment
There was a problem hiding this comment.
Looks good to me. The decorator based design avoids all the problems we've had with extending datetime classes in the past.
I think these are useful for combining intervals together.
Wouldn't this be a breaking change? I don't think we can make that switch outside of a major release.
Perhaps a method to convert the interval in to a |
- add() and sub() methods for combining intervals - createFromDateString() factory wrapping DateInterval method - toDateString() for strtotime-compatible output format
|
@LordSimal Could you clarify what you meant by In Some possible interpretations:
Would appreciate an example of the use case you had in mind. |
Document the new ChronosInterval wrapper class including: - Factory methods (create, createFromValues, createFromDateString, instance) - Property access for DateInterval fields - Formatting methods (toIso8601String, format, toDateString) - Calculation methods (totalSeconds, totalDays) - State checking (isZero, isNegative) - Arithmetic (add, sub) - Native interop (toNative)
|
Docs added. |
Summary
This PR adds a
ChronosIntervalclass that wrapsDateIntervalusing the decorator pattern, as suggested by @markstory in #444.Features
__toString()returns proper ISO 8601 format (e.g.,P1Y2M3DT4H5M6S)create(string $spec)- Create from ISO 8601 speccreateFromValues(...)- Create from individual componentsinstance(DateInterval $interval)- Wrap an existing DateIntervaltoNative()returns the underlyingDateIntervalfor compatibilitytotalSeconds()- Approximate total secondstotalDays()- Total days (exact if from diff())isNegative()- Check if interval is invertedisZero()- Check if interval has no durationy,m,d,h,i,s,f,invert,daysExample Usage
Discussion Points
This is a draft PR to gather feedback on the API design:
add(),sub())?ChronosIntervalbe returned fromChronos::diff()instead ofDateInterval?Related to #444