Angle::getUnits()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2020 WEBEWEB
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 WBW\Library\Core\Geometry;
13
14
use WBW\Library\Core\Model\Attribute\FloatValueTrait;
15
16
/**
17
 * Angle.
18
 *
19
 * @author webeweb <https://github.com/webeweb>
20
 * @package WBW\Library\Core\Geometry
21
 */
22
class Angle {
23
24
    use FloatValueTrait;
25
26
    /**
27
     * Unit "degree".
28
     *
29
     * @var string
30
     */
31
    const UNIT_DEGREE = "degree";
32
33
    /**
34
     * Unit "radian".
35
     *
36
     * @var string
37
     */
38
    const UNIT_RADIAN = "radian";
39
40
    /**
41
     * Units.
42
     *
43
     * @var string
44
     */
45
    private $units;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param float $value The value.
51
     * @param string $units The units.
52
     */
53
    public function __construct(float $value, string $units = self::UNIT_RADIAN) {
54
        $this->setUnits($units);
55
        $this->setValue($value);
56
    }
57
58
    /**
59
     * Degree.
60
     *
61
     * @return float Returns the degree.
62
     */
63
    public function deg(): float {
64
65
        if (true === $this->isDegree()) {
66
            return $this->getValue();
67
        }
68
69
        return $this->getValue() * 180 / pi();
70
    }
71
72
    /**
73
     * Get the unit.
74
     *
75
     * @return string Returns the unit.
76
     */
77
    public function getUnits(): string {
78
        return $this->units;
79
    }
80
81
    /**
82
     * Determines if this angle is in degree.
83
     *
84
     * @return bool Returns true in case of success, false otherwise.
85
     */
86
    public function isDegree(): bool {
87
        return self::UNIT_DEGREE === $this->getUnits();
88
    }
89
90
    /**
91
     * Determines if this angle is in radian.
92
     *
93
     * @return bool Returns true in case of success, false otherwise.
94
     */
95
    public function isRadian(): bool {
96
        return self::UNIT_RADIAN === $this->getUnits();
97
    }
98
99
    /**
100
     * Radian.
101
     *
102
     * @return float Returns the radian.
103
     */
104
    public function rad(): float {
105
106
        if (true === $this->isRadian()) {
107
            return $this->getValue();
108
        }
109
110
        return $this->getValue() * pi() / 180;
111
    }
112
113
    /**
114
     * Set the unit.
115
     *
116
     * @param string $units The unit.
117
     * @return Angle Returns this angle.
118
     */
119
    protected function setUnits(string $units): Angle {
120
        $this->units = $units;
121
        return $this;
122
    }
123
}
124