LiquidVolume::addVolume()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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