Weight::defaultNames()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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
20
    use Calculatable;
21
22
    /* -----------------------------------------------------------------
23
     |  Constructor
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * Weight constructor.
29
     *
30
     * @param  float|int  $value
31
     * @param  string     $unit
32
     * @param  array      $options
33
     */
34 66
    public function __construct($value = 0, $unit = self::KG, array $options = [])
35
    {
36 66
        parent::__construct($value, $unit, $options);
37 66
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Getters & Setters
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Get the default names.
46
     *
47
     * @return array
48
     */
49 63
    public function defaultNames()
50
    {
51 63
        return array_combine(static::units(), [
52 63
            'ton',
53
            'kilogram',
54
            'gram',
55
            'milligram',
56
        ]);
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Main Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Make a weight instance.
66
     *
67
     * @param  float|int  $value
68
     * @param  string     $unit
69
     * @param  array      $options
70
     *
71
     * @return \Arcanedev\Units\Contracts\Measures\Weight|\Arcanedev\Units\Contracts\UnitMeasure
72
     */
73 24
    public static function make($value = 0, $unit = self::KG, array $options = [])
74
    {
75 24
        return parent::make($value, $unit, $options);
76
    }
77
78
    /* -----------------------------------------------------------------
79
     |  Calculation Methods
80
     | -----------------------------------------------------------------
81
     */
82
83
    /**
84
     * Add the weight.
85
     *
86
     * @param  float|int  $value
87
     * @param  string     $unit
88
     *
89
     * @return \Arcanedev\Units\Contracts\Measures\Weight|\Arcanedev\Units\Contracts\UnitMeasure
90
     */
91 6
    public function addWeight($value, $unit = self::KG)
92
    {
93 6
        return $this->add(static::make($value, $unit));
94
    }
95
96
    /**
97
     * Sub the weight.
98
     *
99
     * @param  float|int  $value
100
     * @param  string     $unit
101
     *
102
     * @return \Arcanedev\Units\Contracts\Measures\Weight|\Arcanedev\Units\Contracts\UnitMeasure
103
     */
104 6
    public function subWeight($value, $unit = self::KG)
105
    {
106 6
        return $this->sub(static::make($value, $unit));
107
    }
108
109
    /* -----------------------------------------------------------------
110
     |  Other Methods
111
     | -----------------------------------------------------------------
112
     */
113
114
    /**
115
     * Get the weight convert ratio.
116
     *
117
     * @param  string  $to
118
     * @param  string  $from
119
     *
120
     * @return double|float|integer
121
     */
122 6
    protected static function getRatio($to, $from)
123
    {
124 6
        static::checkUnit($from);
125 6
        static::checkUnit($to);
126
127 6
        if ($to === $from) return 1;
128
129 6
        $ratios = static::getRatios();
130
131 6
        return $ratios[$to] / $ratios[$from];
132
    }
133
134
    /**
135
     * Get all the weight ratios.
136
     *
137
     * @return array
138
     */
139 6
    protected static function getRatios()
140
    {
141 6
        $rate   = 1000;
142
        $ratios = [
143 6
            static::TON => 0,
144 6
            static::KG  => 1,
145 6
            static::G   => 2,
146 6
            static::MG  => 3,
147
        ];
148
149 6
        return array_map(function ($ratio) use ($rate) {
150 6
            return static::calculate($rate, '^', $ratio);
151 6
        }, $ratios);
152
    }
153
}
154