Completed
Pull Request — master (#7)
by ARCANEDEV
10:19
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 176
    public function __construct($value = 0, $unit = self::KG, array $options = [])
33
    {
34 176
        $this->init($value, $unit, $options);
35 176
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Getters & Setters
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * Get the default names.
43
     *
44
     * @return array
45
     */
46 176
    public function defaultNames()
47
    {
48 176
        return array_combine(static::units(), [
49 176
            'ton',
50 132
            'kilogram',
51 132
            'gram',
52 132
            'milligram',
53 132
        ]);
54
    }
55
56
    /* ------------------------------------------------------------------------------------------------
57
     |  Main Functions
58
     | ------------------------------------------------------------------------------------------------
59
     */
60
    /**
61
     * Make a weight instance.
62
     *
63
     * @param  float|int  $value
64
     * @param  string     $unit
65
     * @param  array      $options
66
     *
67
     * @return self
68
     */
69 64
    public static function make($value = 0, $unit = self::KG, array $options = [])
70
    {
71 64
        return new static($value, $unit, $options);
72
    }
73
74
    /**
75
     * Convert the weight to the given unit.
76
     *
77
     * @param  string  $to
78
     *
79
     * @return self
80
     */
81 64
    public function to($to)
82
    {
83 64
        if ($to === $this->unit()) return $this;
84
85 16
        $value = static::convert($this->unit(), $to, $this->value());
86
87 16
        return static::make($value, $to);
88
    }
89
90
    /**
91
     * Convert the weight.
92
     *
93
     * @param  string     $from
94
     * @param  string     $to
95
     * @param  float|int  $value
96
     *
97
     * @return float|int
98
     */
99 16
    public static function convert($from, $to, $value)
100
    {
101 16
        return $value * static::getRatio($to, $from);
102
    }
103
104
    /* ------------------------------------------------------------------------------------------------
105
     |  Calculation Functions
106
     | ------------------------------------------------------------------------------------------------
107
     */
108
    /**
109
     * Add the weight.
110
     *
111
     * @param  float|int  $value
112
     * @param  string     $unit
113
     *
114
     * @return self
115
     */
116 16
    public function addWeight($value, $unit = self::KG)
117
    {
118 16
        return $this->add(self::make($value, $unit));
119
    }
120
121
    /**
122
     * Sub the weight.
123
     *
124
     * @param  float|int  $value
125
     * @param  string     $unit
126
     *
127
     * @return self
128
     */
129 16
    public function subWeight($value, $unit = self::KG)
130
    {
131 16
        return $this->sub(static::make($value, $unit));
132
    }
133
134
    /* ------------------------------------------------------------------------------------------------
135
     |  Other Functions
136
     | ------------------------------------------------------------------------------------------------
137
     */
138
    /**
139
     * Get the weight convert ratio.
140
     *
141
     * @param  string  $to
142
     * @param  string  $from
143
     *
144
     * @return double|float|integer
145
     */
146 16
    protected static function getRatio($to, $from)
147
    {
148 16
        static::checkUnit($from);
149 16
        static::checkUnit($to);
150
151 16
        if ($to === $from) return 1;
152
153 16
        $ratios = static::getRatios();
154
155 16
        return $ratios[$to] / $ratios[$from];
156
    }
157
158
    /**
159
     * Get all the weight ratios.
160
     *
161
     * @return array
162
     */
163 16
    protected static function getRatios()
164
    {
165 16
        $rate   = 1000;
166
        $ratios = [
167 16
            static::TON => 0,
168 16
            static::KG  => 1,
169 16
            static::G   => 2,
170 16
            static::MG  => 3,
171 12
        ];
172
173 16
        return array_map(function ($ratio) use ($rate) {
174 16
            return static::calculate($rate, '^', $ratio);
175 16
        }, $ratios);
176
    }
177
}
178