Completed
Pull Request — master (#7)
by ARCANEDEV
12:42
created

Weight::getSymbolName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\Units\Measures;
2
3
use Arcanedev\Units\Bases\UnitMeasure;
4
use Arcanedev\Units\Contracts\Measures\Weight as WeightContract;
5
use Arcanedev\Units\Traits\Calculatable;
6
7
/**
8
 * Class     Weight
9
 *
10
 * @package  Arcanedev\Units
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Weight extends UnitMeasure implements WeightContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Traits
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    use Calculatable;
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Constructor
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * Weight constructor.
27
     *
28
     * @param  float|int  $value
29
     * @param  string     $unit
30
     * @param  array      $options
31
     */
32
    public function __construct($value = 0, $unit = self::KG, array $options = [])
33 176
    {
34
        $this->init($value, $unit, $options);
35 176
    }
36 176
37 176
    /* ------------------------------------------------------------------------------------------------
38 176
     |  Getters & Setters
39 176
     | ------------------------------------------------------------------------------------------------
40 176
     */
41 176
    /**
42 132
     * Get the default names.
43 176
     *
44
     * @return array
45
     */
46
    public function defaultNames()
47
    {
48
        return array_combine(static::units(), [
49
            'ton',
50
            'kilogram',
51
            'gram',
52
            'milligram',
53
        ]);
54 8
    }
55
56 8
    /* ------------------------------------------------------------------------------------------------
57 8
     |  Main Functions
58 6
     | ------------------------------------------------------------------------------------------------
59 6
     */
60 6
    /**
61 6
     * Make a weight instance.
62
     *
63
     * @param  float|int  $value
64
     * @param  string     $unit
65
     * @param  array      $options
66
     *
67
     * @return self
68
     */
69
    public static function make($value = 0, $unit = self::KG, array $options = [])
70
    {
71 8
        return new static($value, $unit, $options);
72
    }
73 8
74
    /**
75 8
     * Convert the weight to the given unit.
76
     *
77
     * @param  string  $to
78
     *
79
     * @return self
80
     */
81
    public function to($to)
82
    {
83
        if ($to === $this->unit()) return $this;
84
85
        $value = static::convert($this->unit(), $to, $this->value());
86
87
        return static::make($value, $to);
88
    }
89
90
    /**
91 64
     * Convert the weight.
92
     *
93 64
     * @param  string     $from
94
     * @param  string     $to
95
     * @param  float|int  $value
96
     *
97
     * @return float|int
98
     */
99
    public static function convert($from, $to, $value)
100
    {
101
        return $value * static::getRatio($to, $from);
102
    }
103 64
104
    /* ------------------------------------------------------------------------------------------------
105 64
     |  Calculation Functions
106
     | ------------------------------------------------------------------------------------------------
107 16
     */
108
    /**
109 16
     * Add the weight.
110
     *
111
     * @param  float|int  $value
112
     * @param  string     $unit
113
     *
114
     * @return self
115
     */
116
    public function addWeight($value, $unit = self::KG)
117
    {
118
        return $this->add(self::make($value, $unit));
119
    }
120
121 16
    /**
122
     * Sub the weight.
123 16
     *
124
     * @param  float|int  $value
125
     * @param  string     $unit
126
     *
127
     * @return self
128
     */
129
    public function subWeight($value, $unit = self::KG)
130
    {
131
        return $this->sub(static::make($value, $unit));
132
    }
133
134
    /* ------------------------------------------------------------------------------------------------
135
     |  Other Functions
136
     | ------------------------------------------------------------------------------------------------
137
     */
138 16
    /**
139
     * Get the weight convert ratio.
140 16
     *
141
     * @param  string  $to
142
     * @param  string  $from
143
     *
144
     * @return double|float|integer
145
     */
146
    protected static function getRatio($to, $from)
147
    {
148
        static::checkUnit($from);
149
        static::checkUnit($to);
150
151 16
        if ($to === $from) return 1;
152
153 16
        $ratios = static::getRatios();
154
155
        return $ratios[$to] / $ratios[$from];
156
    }
157
158
    /**
159
     * Get all the weight ratios.
160
     *
161
     * @return array
162
     */
163
    protected static function getRatios()
164
    {
165
        $rate   = 1000;
166
        $ratios = [
167
            static::TON => 0,
168 16
            static::KG  => 1,
169
            static::G   => 2,
170 16
            static::MG  => 3,
171 16
        ];
172
173 16
        return array_map(function ($ratio) use ($rate) {
174
            return static::calculate($rate, '^', $ratio);
175 16
        }, $ratios);
176
    }
177
}
178