-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpeed.php
85 lines (73 loc) · 1.79 KB
/
Speed.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types = 1);
namespace PCF\ValueObject\Speed;
use PCF\ValueObject\Distance\AbstractDistance;
use PCF\ValueObject\Time\AbstractTime;
use PCF\ValueObject\ValueObjectInterface;
class Speed implements ValueObjectInterface
{
/**
* @var AbstractDistance
*/
private $distance;
/**
* @var AbstractTime
*/
private $time;
/**
* @var float
*/
private $quality = null;
/**
* @var string
*/
private $unit = null;
/**
* Speed constructor.
* @param AbstractDistance $distance
* @param AbstractTime $time
*/
public function __construct(AbstractDistance $distance, AbstractTime $time)
{
$this->distance = $distance;
$this->time = $time;
}
/**
* @inheritdoc
*/
public function isEqualTo(ValueObjectInterface $compare): bool
{
if (!$compare instanceof Speed || $compare->getUnit() != $this->getUnit()) {
$errorMsg = 'you are able to compare only speed with same unit';
throw new \InvalidArgumentException($errorMsg);
}
return $compare->getQuality() == $this->getQuality();
}
/**
* @inheritdoc
*/
public function __toString(): string
{
return $this->getQuality() . ' ' . $this->getUnit();
}
/**
* @return float
*/
public function getQuality(): float
{
if (empty($this->quantity)) {
$this->quality = $this->distance->getQuality() / $this->time->getQuality();
}
return $this->quality;
}
/**
* @return string
*/
public function getUnit(): string
{
if (empty($this->unit)) {
$this->unit = $this->distance->getUnit() . '/' . $this->time->getUnit();
}
return $this->unit;
}
}