Completed
Pull Request — master (#7)
by ARCANEDEV
15:23
created

Weight::defaultNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 1
cts 1
cp 1
rs 9.6666
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
    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 static
68
     */
69
    public static function make($value = 0, $unit = self::KG, array $options = [])
70
    {
71 8
        return parent::make($value, $unit, $options);
72
    }
73 8
74
    /* ------------------------------------------------------------------------------------------------
75 8
     |  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
    public function addWeight($value, $unit = self::KG)
87
    {
88
        return $this->add(static::make($value, $unit));
89
    }
90
91 64
    /**
92
     * Sub the weight.
93 64
     *
94
     * @param  float|int  $value
95
     * @param  string     $unit
96
     *
97
     * @return self
98
     */
99
    public function subWeight($value, $unit = self::KG)
100
    {
101
        return $this->sub(static::make($value, $unit));
102
    }
103 64
104
    /* ------------------------------------------------------------------------------------------------
105 64
     |  Other Functions
106
     | ------------------------------------------------------------------------------------------------
107 16
     */
108
    /**
109 16
     * Get the weight convert ratio.
110
     *
111
     * @param  string  $to
112
     * @param  string  $from
113
     *
114
     * @return double|float|integer
115
     */
116
    protected static function getRatio($to, $from)
117
    {
118
        static::checkUnit($from);
119
        static::checkUnit($to);
120
121 16
        if ($to === $from) return 1;
122
123 16
        $ratios = static::getRatios();
124
125
        return $ratios[$to] / $ratios[$from];
126
    }
127
128
    /**
129
     * Get all the weight ratios.
130
     *
131
     * @return array
132
     */
133
    protected static function getRatios()
134
    {
135
        $rate   = 1000;
136
        $ratios = [
137
            static::TON => 0,
138 16
            static::KG  => 1,
139
            static::G   => 2,
140 16
            static::MG  => 3,
141
        ];
142
143
        return array_map(function ($ratio) use ($rate) {
144
            return static::calculate($rate, '^', $ratio);
145
        }, $ratios);
146
    }
147
}
148