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

Weight::subWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
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 320
    public function __construct($value = 0, $unit = self::KG, array $options = [])
33
    {
34 320
        $this->init($value, $unit, $options);
35 320
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Getters & Setters
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * Get the default names.
43
     *
44
     * @return array
45
     */
46 224
    public function defaultNames()
47
    {
48 224
        return array_combine(static::units(), [
49 224
            'ton',
50 168
            'kilogram',
51 168
            'gram',
52 168
            'milligram',
53 168
        ]);
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 120
    public static function make($value = 0, $unit = self::KG, array $options = [])
70
    {
71 120
        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 32
    public function addWeight($value, $unit = self::KG)
87
    {
88 32
        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 32
    public function subWeight($value, $unit = self::KG)
100
    {
101 32
        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 32
    protected static function getRatio($to, $from)
117
    {
118 32
        static::checkUnit($from);
119 32
        static::checkUnit($to);
120
121 32
        if ($to === $from) return 1;
122
123 32
        $ratios = static::getRatios();
124
125 32
        return $ratios[$to] / $ratios[$from];
126
    }
127
128
    /**
129
     * Get all the weight ratios.
130
     *
131
     * @return array
132
     */
133 32
    protected static function getRatios()
134
    {
135 32
        $rate   = 1000;
136
        $ratios = [
137 32
            static::TON => 0,
138 32
            static::KG  => 1,
139 32
            static::G   => 2,
140 32
            static::MG  => 3,
141 24
        ];
142
143 32
        return array_map(function ($ratio) use ($rate) {
144 32
            return static::calculate($rate, '^', $ratio);
145 32
        }, $ratios);
146
    }
147
}
148