2121
2222final readonly class Duration implements JsonSerializable
2323{
24- public int $ value ;
24+ public int $ microseconds ;
2525 public int $ sign ;
2626
2727 /**
28- * @param int $value expressed in microseconds
28+ * @param int $microseconds expressed in microseconds
2929 *
3030 * @throws InvalidDuration
3131 */
32- private function __construct (int $ value )
32+ private function __construct (int $ microseconds )
3333 {
34- ($ value > PHP_INT_MIN + 1 && $ value < PHP_INT_MAX ) || throw InvalidDuration::dueToOverflow ();
34+ ($ microseconds > PHP_INT_MIN + 1 && $ microseconds < PHP_INT_MAX ) || throw InvalidDuration::dueToOverflow ();
3535
36- $ this ->value = $ value ;
37- $ this ->sign = $ this ->value <=> 0 ;
36+ $ this ->microseconds = $ microseconds ;
37+ $ this ->sign = $ this ->microseconds <=> 0 ;
3838 }
3939
4040 /**
@@ -172,27 +172,16 @@ public static function maxOf(self ...$durations): self
172172 return null !== $ value ? $ value : throw new ValueError ('maxOf() expects at least one duration ' );
173173 }
174174
175- private static function extractDuration (self |Interval |Task |NativeInterval |NativeTask $ that ): self
176- {
177- return match (true ) {
178- $ that instanceof NativeInterval => Interval::fromNative ($ that )->duration ,
179- $ that instanceof NativeTask => Task::fromNative ($ that )->interval ->duration ,
180- $ that instanceof Task => $ that ->interval ->duration ,
181- $ that instanceof Interval => $ that ->duration ,
182- $ that instanceof self => $ that ,
183- };
184- }
185-
186175 /**
187176 * Compare this instance with another.
188177 *
189178 * @return int<-1, 1> If this duration is shorter, equal, or longer than the given duration.
190179 */
191180 public static function compare (
192- self |Interval |Task |NativeInterval |NativeTask $ that ,
193- self |Interval |Task |NativeInterval |NativeTask $ other
181+ Duration | DateInterval |Interval |Task |NativeInterval |NativeTask $ that ,
182+ Duration | DateInterval |Interval |Task |NativeInterval |NativeTask $ other
194183 ): int {
195- return self :: extractDuration ($ that )->value <=> self :: extractDuration ($ other )->value ;
184+ return AcceptTypes:: duration ($ that )->microseconds <=> AcceptTypes:: duration ($ other )->microseconds ;
196185 }
197186
198187 /**
@@ -210,7 +199,7 @@ public function format(DurationFormat $format = DurationFormat::Iso8601): string
210199 */
211200 public function toDateInterval (?DateTimeInterface $ relativeTo = null ): DateInterval
212201 {
213- $ parsed = UnitTransformer::decompose ($ this ->value );
202+ $ parsed = UnitTransformer::decompose ($ this ->microseconds );
214203 $ interval = new DateInterval ('PT0S ' );
215204 $ interval ->d = $ parsed ->daysCount ;
216205 $ interval ->h = $ parsed ->hours % 24 ;
@@ -234,9 +223,9 @@ public function toDateInterval(?DateTimeInterface $relativeTo = null): DateInter
234223 /**
235224 * Returns the Duration as expressed in the specified Unit of time.
236225 */
237- public function total (Unit $ unit ): int |float
226+ public function in (Unit $ unit ): int |float
238227 {
239- return UnitTransformer::fromMicroseconds ($ this ->value , $ unit );
228+ return UnitTransformer::fromMicroseconds ($ this ->microseconds , $ unit );
240229 }
241230
242231 /**
@@ -254,7 +243,7 @@ public function jsonSerialize(): string
254243 */
255244 public function isZero (): bool
256245 {
257- return 0 === $ this ->value ;
246+ return 0 === $ this ->microseconds ;
258247 }
259248
260249 /**
@@ -264,38 +253,38 @@ public function isZero(): bool
264253 */
265254 public function negated (): self
266255 {
267- return new self (-$ this ->value );
256+ return new self (-$ this ->microseconds );
268257 }
269258
270259 /**
271260 * @throws InvalidDuration
272261 */
273262 public function abs (): self
274263 {
275- return $ this ->value < 0 ? $ this ->negated () : $ this ;
264+ return $ this ->microseconds < 0 ? $ this ->negated () : $ this ;
276265 }
277266
278267 /**
279268 * Returns a new instance rounded to the specified unit using a rounding mode.
280269 */
281270 public function roundTo (Unit $ unit , SnapMode $ mode = SnapMode::Nearest): self
282271 {
283- $ rounded = UnitTransformer::round ($ this ->value , $ unit , $ mode );
272+ $ rounded = UnitTransformer::round ($ this ->microseconds , $ unit , $ mode );
284273
285- return $ this ->value === $ rounded ? $ this : new self ($ rounded );
274+ return $ this ->microseconds === $ rounded ? $ this : new self ($ rounded );
286275 }
287276
288277 /**
289278 * @throws InvalidDuration
290279 */
291- public function sum (self |Interval |Task |NativeInterval |NativeTask ...$ other ): self
280+ public function sum (Duration | DateInterval |Interval |Task |NativeInterval |NativeTask ...$ other ): self
292281 {
293- $ other = array_map (self :: extractDuration (...), $ other );
282+ $ other = array_map (AcceptTypes:: duration (...), $ other );
294283 $ other [] = $ this ;
295- $ value = array_sum (array_column ($ other , 'value ' ));
296- is_int ($ value ) || throw InvalidDuration::dueToOverflow (); /* @phpstan-ignore-line */
284+ $ microseconds = array_sum (array_column ($ other , 'microseconds ' ));
285+ is_int ($ microseconds ) || throw InvalidDuration::dueToOverflow (); /* @phpstan-ignore-line */
297286
298- return $ this ->value === $ value ? $ this : new self ($ value );
287+ return $ this ->microseconds === $ microseconds ? $ this : new self ($ microseconds );
299288 }
300289
301290 /**
@@ -363,26 +352,26 @@ public function decrease(
363352 /**
364353 * Tells whether this instance is equal to the specified duration.
365354 */
366- public function equals (self $ other ): bool
355+ public function equals (Duration | DateInterval | Interval | Task | NativeInterval | NativeTask $ other ): bool
367356 {
368357 return 0 === self ::compare ($ this , $ other );
369358 }
370359
371- public function isLongerThan (self $ other ): bool
360+ public function isLongerThan (Duration | DateInterval | Interval | Task | NativeInterval | NativeTask $ other ): bool
372361 {
373362 return 0 < self ::compare ($ this , $ other );
374363 }
375364
376- public function isLongerThanOrEqual (self $ other ): bool
365+ public function isLongerThanOrEqual (Duration | DateInterval | Interval | Task | NativeInterval | NativeTask $ other ): bool
377366 {
378367 return 0 <= self ::compare ($ this , $ other );
379368 }
380- public function isShorterThan (self $ other ): bool
369+ public function isShorterThan (Duration | DateInterval | Interval | Task | NativeInterval | NativeTask $ other ): bool
381370 {
382371 return 0 > self ::compare ($ this , $ other );
383372 }
384373
385- public function isShorterThanOrEqual (self $ other ): bool
374+ public function isShorterThanOrEqual (Duration | DateInterval | Interval | Task | NativeInterval | NativeTask $ other ): bool
386375 {
387376 return 0 >= self ::compare ($ this , $ other );
388377 }
@@ -395,11 +384,11 @@ public function isShorterThanOrEqual(self $other): bool
395384 * @throws InvalidDuration
396385 */
397386 public function clamp (
398- self |Interval |Task |NativeInterval |NativeTask $ min ,
399- self |Interval |Task |NativeInterval |NativeTask $ max
387+ Duration | DateInterval |Interval |Task |NativeInterval |NativeTask $ min ,
388+ Duration | DateInterval |Interval |Task |NativeInterval |NativeTask $ max
400389 ): self {
401- $ max = self :: extractDuration ($ max );
402- $ min = self :: extractDuration ($ min );
390+ $ max = AcceptTypes:: duration ($ max );
391+ $ min = AcceptTypes:: duration ($ min );
403392
404393 $ max ->isLongerThanOrEqual ($ min ) || throw new InvalidDuration ('The maximum duration must be longer or equal to the minimum duration. ' );
405394
@@ -419,7 +408,7 @@ public function multipliedBy(int $factor): self
419408 {
420409 0 <= $ factor || throw new InvalidDuration ('factor must be a non negative integer. ' ); /* @phpstan-ignore-line */
421410
422- $ result = $ this ->value * $ factor ;
411+ $ result = $ this ->microseconds * $ factor ;
423412
424413 is_int ($ result ) || throw InvalidDuration::dueToOverflow (); /* @phpstan-ignore-line */
425414
@@ -439,15 +428,15 @@ public function dividedBy(int $factor): self
439428 {
440429 0 < $ factor || throw new InvalidDuration ('factor must be a positive integer. ' ); /* @phpstan-ignore-line */
441430
442- return new self (intdiv ($ this ->value , $ factor ));
431+ return new self (intdiv ($ this ->microseconds , $ factor ));
443432 }
444433
445- public function times ( self |Interval |Task |NativeInterval |NativeTask $ other ): int
434+ public function countOf ( Duration | DateInterval |Interval |Task |NativeInterval |NativeTask $ other ): int
446435 {
447- $ other = self :: extractDuration ($ other );
436+ $ other = AcceptTypes:: duration ($ other );
448437
449438 return !$ other ->isZero ()
450- ? intdiv ($ this ->value , $ other ->value )
439+ ? intdiv ($ this ->microseconds , $ other ->microseconds )
451440 : throw new InvalidDuration ('Cannot divide by zero duration. ' );
452441 }
453442
@@ -456,7 +445,7 @@ public function times(self|Interval|Task|NativeInterval|NativeTask $other): int
456445 */
457446 public function __serialize (): array
458447 {
459- return [['microseconds ' => $ this ->value ], []];
448+ return [['microseconds ' => $ this ->microseconds ], []];
460449 }
461450
462451 /**
@@ -468,7 +457,7 @@ public function __unserialize(array $data): void
468457 {
469458 [$ properties ] = $ data ;
470459 $ duration = new self ($ properties ['microseconds ' ]);
471- $ this ->value = $ duration ->value ;
460+ $ this ->microseconds = $ duration ->microseconds ;
472461 $ this ->sign = $ duration ->sign ;
473462 }
474463}
0 commit comments