-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.php
More file actions
97 lines (85 loc) · 3.11 KB
/
Time.php
File metadata and controls
97 lines (85 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
declare(strict_types=1);
namespace Andante\Measurement\Quantity\Time;
use Andante\Measurement\Contract\AutoScalableInterface;
use Andante\Measurement\Contract\CalculableInterface;
use Andante\Measurement\Contract\ComparableInterface;
use Andante\Measurement\Contract\ConvertibleInterface;
use Andante\Measurement\Contract\Math\NumberInterface;
use Andante\Measurement\Contract\Quantity\Time\TimeInterface;
use Andante\Measurement\Contract\QuantityFactoryInterface;
use Andante\Measurement\Contract\UnitInterface;
use Andante\Measurement\Dimension\Time as TimeDimension;
use Andante\Measurement\Exception\InvalidUnitException;
use Andante\Measurement\Math\NumberFactory;
use Andante\Measurement\Quantity\Trait\AutoScalableTrait;
use Andante\Measurement\Quantity\Trait\CalculableTrait;
use Andante\Measurement\Quantity\Trait\ComparableTrait;
use Andante\Measurement\Quantity\Trait\ConvertibleTrait;
use Andante\Measurement\Quantity\Trait\DateIntervalConvertibleTrait;
use Andante\Measurement\Unit\Time\TimeUnit;
/**
* Generic time quantity that can hold any time unit.
*
* Use this class when you need to work with time values where the specific
* unit may vary or is determined at runtime.
*
* Example:
* ```php
* $duration = Time::of(NumberFactory::create('30'), TimeUnit::Minute);
* $interval = $duration->toPhpDateInterval(); // 30 minutes
* ```
*/
final class Time implements TimeInterface, QuantityFactoryInterface, ConvertibleInterface, ComparableInterface, CalculableInterface, AutoScalableInterface
{
use ConvertibleTrait;
use ComparableTrait;
use CalculableTrait;
use AutoScalableTrait;
use DateIntervalConvertibleTrait;
private function __construct(
private readonly NumberInterface $value,
private readonly UnitInterface $unit,
) {
}
/**
* Create a Time quantity with a specific unit.
*
* @throws InvalidUnitException If unit is not a time unit
*/
public static function of(NumberInterface $value, UnitInterface $unit): self
{
if (!$unit->dimension()->isCompatibleWith(TimeDimension::instance())) {
throw InvalidUnitException::forInvalidDimension($unit, TimeDimension::instance(), self::class);
}
return new self($value, $unit);
}
/**
* Create a Time quantity from a PHP DateInterval.
*
* Returns a Time in seconds.
* Note: Years and months are ignored as they don't have fixed durations.
*/
public static function ofPhpDateInterval(\DateInterval $interval): self
{
$totalSeconds = self::dateIntervalToSeconds($interval);
return self::of(NumberFactory::create($totalSeconds), TimeUnit::Second);
}
/**
* @internal Used by the library for conversions and calculations
*
* @throws InvalidUnitException If unit is not a time unit
*/
public static function from(NumberInterface $value, UnitInterface $unit): self
{
return self::of($value, $unit);
}
public function getValue(): NumberInterface
{
return $this->value;
}
public function getUnit(): UnitInterface
{
return $this->unit;
}
}