Completed
Pull Request — master (#7)
by ARCANEDEV
07:42
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 312
    public function __construct($value = 0, $unit = self::L, array $options = [])
33
    {
34 312
        $this->init($value, $unit, $options);
35 312
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Getters & Setters
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * Get the default names.
43
     *
44
     * @return array
45
     */
46 216
    public function defaultNames()
47
    {
48 216
        return array_combine(static::units(), [
49 216
            'kilolitre',
50 162
            'hectolitre',
51 162
            'decalitre',
52 162
            'litre',
53 162
            'decilitre',
54 162
            'centilitre',
55 162
            'millilitre',
56 162
        ]);
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 128
    public static function make($value = 0, $unit = self::L, array $options = [])
73
    {
74 128
        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 32
    public function addVolume($value, $unit = self::L)
90
    {
91 32
        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 32
    public function subVolume($value, $unit = self::L)
103
    {
104 32
        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 40
    protected static function getRatio($to, $from)
120
    {
121 40
        static::checkUnit($from);
122 40
        static::checkUnit($to);
123
124 40
        if ($to === $from) return 1;
125
126 40
        $ratios = static::getRatios();
127
128 40
        return $ratios[$to] / $ratios[$from];
129
    }
130
131
    /**
132
     * Get all the volume ratios.
133
     *
134
     * @return array
135
     */
136 40
    protected static function getRatios()
137
    {
138 40
        $rate   = 10;
139
        $ratios = [
140 40
            static::KL  => 0,
141 40
            static::HL  => 1,
142 40
            static::DAL => 2,
143 40
            static::L   => 3,
144 40
            static::DL  => 4,
145 40
            static::CL  => 5,
146 40
            static::ML  => 6,
147 30
        ];
148
149 40
        return array_map(function ($ratio) use ($rate) {
150 40
            return static::calculate($rate, '^', $ratio);
151 40
        }, $ratios);
152
    }
153
}
154