Completed
Push — master ( b1b048...d29c62 )
by ARCANEDEV
12:09
created

Weight::to()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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 240
    public function __construct($value = 0, $unit = self::KG, array $options = [])
33
    {
34 240
        $this->init($value, $unit, $options);
35 240
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Getters & Setters
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * Get the default names.
43
     *
44
     * @return array
45
     */
46 168
    public function defaultNames()
47
    {
48 168
        return array_combine(static::units(), [
49 168
            'ton',
50 84
            'kilogram',
51 84
            'gram',
52 84
            'milligram',
53 84
        ]);
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 static
68
     */
69 90
    public static function make($value = 0, $unit = self::KG, array $options = [])
70
    {
71 90
        return parent::make($value, $unit, $options);
72
    }
73
74
    /* ------------------------------------------------------------------------------------------------
75
     |  Calculation Functions
76
     | ------------------------------------------------------------------------------------------------
77
     */
78
    /**
79
     * Add the weight.
80
     *
81
     * @param  float|int  $value
82
     * @param  string     $unit
83
     *
84
     * @return self
85
     */
86 24
    public function addWeight($value, $unit = self::KG)
87
    {
88 24
        return $this->add(static::make($value, $unit));
89
    }
90
91
    /**
92
     * Sub the weight.
93
     *
94
     * @param  float|int  $value
95
     * @param  string     $unit
96
     *
97
     * @return self
98
     */
99 24
    public function subWeight($value, $unit = self::KG)
100
    {
101 24
        return $this->sub(static::make($value, $unit));
102
    }
103
104
    /* ------------------------------------------------------------------------------------------------
105
     |  Other Functions
106
     | ------------------------------------------------------------------------------------------------
107
     */
108
    /**
109
     * Get the weight convert ratio.
110
     *
111
     * @param  string  $to
112
     * @param  string  $from
113
     *
114
     * @return double|float|integer
115
     */
116 24
    protected static function getRatio($to, $from)
117
    {
118 24
        static::checkUnit($from);
119 24
        static::checkUnit($to);
120
121 24
        if ($to === $from) return 1;
122
123 24
        $ratios = static::getRatios();
124
125 24
        return $ratios[$to] / $ratios[$from];
126
    }
127
128
    /**
129
     * Get all the weight ratios.
130
     *
131
     * @return array
132
     */
133 24
    protected static function getRatios()
134
    {
135 24
        $rate   = 1000;
136
        $ratios = [
137 24
            static::TON => 0,
138 24
            static::KG  => 1,
139 24
            static::G   => 2,
140 24
            static::MG  => 3,
141 12
        ];
142
143 24
        return array_map(function ($ratio) use ($rate) {
144 24
            return static::calculate($rate, '^', $ratio);
145 24
        }, $ratios);
146
    }
147
}
148