-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferRate.php
More file actions
83 lines (73 loc) · 2.76 KB
/
TransferRate.php
File metadata and controls
83 lines (73 loc) · 2.76 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
<?php
declare(strict_types=1);
namespace Andante\Measurement\Quantity\Digital\DataTransferRate\SI;
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\Digital\SITransferRateInterface;
use Andante\Measurement\Contract\QuantityFactoryInterface;
use Andante\Measurement\Contract\UnitInterface;
use Andante\Measurement\Exception\InvalidUnitException;
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\Unit\Digital\SI\SITransferRateUnit;
/**
* SI (decimal) data transfer rate quantity that can hold any SI transfer rate unit.
*
* This is the "mid-level" class for SI transfer rates. Use this when you need
* to work with any SI transfer rate unit (bit or byte based) without knowing
* the specific unit at compile time.
*
* For more specific mid-level classes:
* - BitTransferRate - only SI bit-based rates
* - ByteTransferRate - only SI byte-based rates
*
* Example:
* ```php
* $speed = TransferRate::of($number, SITransferRateUnit::MegabitPerSecond);
* $speed = TransferRate::of($number, SITransferRateUnit::MegabytePerSecond);
* ```
*/
final class TransferRate implements SITransferRateInterface, QuantityFactoryInterface, ConvertibleInterface, ComparableInterface, CalculableInterface, AutoScalableInterface
{
use ConvertibleTrait;
use ComparableTrait;
use CalculableTrait;
use AutoScalableTrait;
private function __construct(
private readonly NumberInterface $value,
private readonly UnitInterface $unit,
) {
}
/**
* Create an SI transfer rate quantity with the specified value and unit.
*/
public static function of(NumberInterface $value, SITransferRateUnit $unit): self
{
return new self($value, $unit);
}
/**
* @internal Used by the library for conversions and calculations
*
* @throws InvalidUnitException If unit is not an SITransferRateUnit
*/
public static function from(NumberInterface $value, UnitInterface $unit): self
{
if (!$unit instanceof SITransferRateUnit) {
throw InvalidUnitException::forInvalidUnitType($unit, SITransferRateUnit::class, self::class);
}
return new self($value, $unit);
}
public function getValue(): NumberInterface
{
return $this->value;
}
public function getUnit(): UnitInterface
{
return $this->unit;
}
}