forked from smartemailing/types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart.php
64 lines (50 loc) · 1.08 KB
/
Part.php
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
<?php
declare(strict_types = 1);
namespace SmartEmailing\Types;
use SmartEmailing\Types\Comparable\ComparableInterface;
use SmartEmailing\Types\Comparable\StringComparableTrait;
use SmartEmailing\Types\ExtractableTraits\FloatExtractableTrait;
final class Part implements ToStringInterface, ComparableInterface
{
use FloatExtractableTrait;
use ToStringTrait;
use StringComparableTrait;
/**
* @var float
*/
private $value;
public function __construct(
float $value
) {
if ($value < 0 || $value > 1) {
throw new InvalidTypeException('Invalid part: ' . $value);
}
$this->value = $value;
}
public static function fromRatio(
float $value,
float $whole
): self {
if ($value > $whole) {
throw new InvalidTypeException(
'Value cannot be higher than whole: but '
. $value
. ' / '
. $whole
. ' given.'
);
}
if ($whole === 0.0) {
return new static(0.0);
}
return new static($value / $whole);
}
public function getValue(): float
{
return $this->value;
}
public function getPercent(): float
{
return $this->getValue() * 100;
}
}