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

LiquidVolume::defaultNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 9
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
    public function __construct($value = 0, $unit = self::L, 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
            'kilolitre',
50
            'hectolitre',
51
            'decalitre',
52
            'litre',
53
            'decilitre',
54 8
            'centilitre',
55
            'millilitre',
56 8
        ]);
57 8
    }
58 6
59 6
    /* ------------------------------------------------------------------------------------------------
60 6
     |  Main Functions
61 6
     | ------------------------------------------------------------------------------------------------
62 6
     */
63 6
    /**
64 6
     * Make a volume instance.
65
     *
66
     * @param  float|int  $value
67
     * @param  string     $unit
68
     * @param  array      $options
69
     *
70
     * @return static
71
     */
72
    public static function make($value = 0, $unit = self::L, array $options = [])
73
    {
74 8
        return parent::make($value, $unit, $options);
75
    }
76 8
77
    /* ------------------------------------------------------------------------------------------------
78 8
     |  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
    public function addVolume($value, $unit = self::L)
90
    {
91
        return $this->add(static::make($value, $unit));
92
    }
93
94 72
    /**
95
     * Sub the volume.
96 72
     *
97
     * @param  float|int  $value
98
     * @param  string     $unit
99
     *
100
     * @return \Arcanedev\Units\Contracts\Measures\LiquidVolume
101
     */
102
    public function subVolume($value, $unit = self::L)
103
    {
104
        return $this->sub(static::make($value, $unit));
105
    }
106 72
107
    /* ------------------------------------------------------------------------------------------------
108 72
     |  Other Functions
109
     | ------------------------------------------------------------------------------------------------
110 24
     */
111
    /**
112 24
     * Get the volume convert ratio.
113
     *
114
     * @param  string  $to
115
     * @param  string  $from
116
     *
117
     * @return float|int
118
     */
119
    protected static function getRatio($to, $from)
120
    {
121
        static::checkUnit($from);
122
        static::checkUnit($to);
123
124 24
        if ($to === $from) return 1;
125
126 24
        $ratios = static::getRatios();
127
128
        return $ratios[$to] / $ratios[$from];
129
    }
130
131
    /**
132
     * Get all the volume ratios.
133
     *
134
     * @return array
135
     */
136
    protected static function getRatios()
137
    {
138
        $rate   = 10;
139
        $ratios = [
140
            static::KL  => 0,
141 16
            static::HL  => 1,
142
            static::DAL => 2,
143 16
            static::L   => 3,
144 16
            static::DL  => 4,
145 12
            static::CL  => 5,
146
            static::ML  => 6,
147
        ];
148
149
        return array_map(function ($ratio) use ($rate) {
150
            return static::calculate($rate, '^', $ratio);
151
        }, $ratios);
152
    }
153
}
154