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

LiquidVolume::getRatios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.4285
cc 1
eloc 13
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\LiquidVolume as LiquidVolumeContract;
5
use Arcanedev\Units\Traits\Calculatable;
6
7
/**
8
 * Class     LiquidVolume
9
 *
10
 * @package  Arcanedev\Units\Measures
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class LiquidVolume extends UnitMeasure implements LiquidVolumeContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Traits
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    use Calculatable;
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Constructor
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * Volume constructor.
27
     *
28
     * @param  float|int  $value
29
     * @param  string     $unit
30
     * @param  array      $options
31
     */
32 234
    public function __construct($value = 0, $unit = self::L, array $options = [])
33
    {
34 234
        $this->init($value, $unit, $options);
35 234
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Getters & Setters
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * Get the default names.
43
     *
44
     * @return array
45
     */
46 162
    public function defaultNames()
47
    {
48 162
        return array_combine(static::units(), [
49 162
            'kilolitre',
50 81
            'hectolitre',
51 81
            'decalitre',
52 81
            'litre',
53 81
            'decilitre',
54 81
            'centilitre',
55 81
            'millilitre',
56 81
        ]);
57
    }
58
59
    /* ------------------------------------------------------------------------------------------------
60
     |  Main Functions
61
     | ------------------------------------------------------------------------------------------------
62
     */
63
    /**
64
     * Make a volume instance.
65
     *
66
     * @param  float|int  $value
67
     * @param  string     $unit
68
     * @param  array      $options
69
     *
70
     * @return static
71
     */
72 96
    public static function make($value = 0, $unit = self::L, array $options = [])
73
    {
74 96
        return parent::make($value, $unit, $options);
75
    }
76
77
    /* ------------------------------------------------------------------------------------------------
78
     |  Calculation Functions
79
     | ------------------------------------------------------------------------------------------------
80
     */
81
    /**
82
     * Add the volume.
83
     *
84
     * @param  float|int  $value
85
     * @param  string     $unit
86
     *
87
     * @return \Arcanedev\Units\Contracts\Measures\LiquidVolume
88
     */
89 24
    public function addVolume($value, $unit = self::L)
90
    {
91 24
        return $this->add(static::make($value, $unit));
92
    }
93
94
    /**
95
     * Sub the volume.
96
     *
97
     * @param  float|int  $value
98
     * @param  string     $unit
99
     *
100
     * @return \Arcanedev\Units\Contracts\Measures\LiquidVolume
101
     */
102 24
    public function subVolume($value, $unit = self::L)
103
    {
104 24
        return $this->sub(static::make($value, $unit));
105
    }
106
107
    /* ------------------------------------------------------------------------------------------------
108
     |  Other Functions
109
     | ------------------------------------------------------------------------------------------------
110
     */
111
    /**
112
     * Get the volume convert ratio.
113
     *
114
     * @param  string  $to
115
     * @param  string  $from
116
     *
117
     * @return float|int
118
     */
119 30
    protected static function getRatio($to, $from)
120
    {
121 30
        static::checkUnit($from);
122 30
        static::checkUnit($to);
123
124 30
        if ($to === $from) return 1;
125
126 30
        $ratios = static::getRatios();
127
128 30
        return $ratios[$to] / $ratios[$from];
129
    }
130
131
    /**
132
     * Get all the volume ratios.
133
     *
134
     * @return array
135
     */
136 30
    protected static function getRatios()
137
    {
138 30
        $rate   = 10;
139
        $ratios = [
140 30
            static::KL  => 0,
141 30
            static::HL  => 1,
142 30
            static::DAL => 2,
143 30
            static::L   => 3,
144 30
            static::DL  => 4,
145 30
            static::CL  => 5,
146 30
            static::ML  => 6,
147 15
        ];
148
149 30
        return array_map(function ($ratio) use ($rate) {
150 30
            return static::calculate($rate, '^', $ratio);
151 30
        }, $ratios);
152
    }
153
}
154