Completed
Push — master ( e30611...9a9190 )
by Ivannis Suárez
06:48
created

Distance.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\Geolocation;
13
14
use Cubiche\Core\Comparable\ComparableInterface;
15
use Cubiche\Domain\Model\ValueObjectInterface;
16
use Cubiche\Domain\System\Real;
17
18
/**
19
 * Distance Class.
20
 *
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
class Distance implements ValueObjectInterface, ComparableInterface
0 ignored issues
show
There is one abstract method hashCode in this class; you could implement it, or declare this class as abstract.
Loading history...
24
{
25
    /**
26
     * @var DistanceUnit
27
     */
28
    protected $unit;
29
30
    /**
31
     * @var \Cubiche\Domain\System\Real
32
     */
33
    protected $value;
34
35
    /**
36
     * @param Coordinate   $from
37
     * @param Coordinate   $to
38
     * @param DistanceUnit $unit
39
     *
40
     * @return Distance
41
     */
42
    public static function fromTo(
43
        Coordinate $from,
44
        Coordinate $to,
45
        DistanceUnit $unit = null
46
    ) {
47
        return $from->distance($to, $unit);
48
    }
49
50
    /**
51
     * @param \Cubiche\Domain\System\Real $value
52
     * @param DistanceUnit                $unit
53
     */
54
    public function __construct(Real $value, DistanceUnit $unit)
55
    {
56
        $this->value = $value;
57
        $this->unit = $unit;
58
    }
59
60
    /**
61
     * @return DistanceUnit
62
     */
63
    public function unit()
64
    {
65
        return $this->unit;
66
    }
67
68
    /**
69
     * @return \Cubiche\Domain\System\Real
70
     */
71
    public function value()
72
    {
73
        return $this->value;
74
    }
75
76
    /**
77
     * @param DistanceUnit $unit
78
     *
79
     * @return Distance
80
     */
81
    public function in(DistanceUnit $unit)
82
    {
83
        if ($this->unit()->equals($unit)) {
84
            return $this;
85
        }
86
87
        return new self($this->value()->multReal(Real::fromNative($this->unit()->conversionRate($unit))), $unit);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function equals($other)
94
    {
95
        return $other instanceof self &&
96
            $this->unit()->equals($other->unit()) &&
97
            $this->value()->equals($other->value());
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function compareTo($other)
104
    {
105
        if (!$other instanceof self) {
106
            throw new \InvalidArgumentException(sprintf(
107
                'Argument "%s" is invalid. Allowed types for argument are "%s".',
108
                $other,
109
                self::class
110
            ));
111
        }
112
113
        return $this->value()->compareTo($other->in($this->unit())->value());
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function __toString()
120
    {
121
        return\sprintf('%F %s', $this->value()->toNative(), $this->unit()->toNative());
122
    }
123
}
124