Completed
Push — master ( 8539d8...12a313 )
by ARCANEDEV
17s
created

Rate::reverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
1
<?php namespace Arcanedev\Currencies\Entities;
2
3
use Arcanedev\Currencies\Contracts\Entities\Rate as RateContract;
4
use Illuminate\Contracts\Support\Arrayable;
5
use Illuminate\Contracts\Support\Jsonable;
6
7
/**
8
 * Class     Rate
9
 *
10
 * @package  Arcanedev\Currencies\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Rate implements RateContract, Arrayable, Jsonable
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * @var string
21
     */
22
    protected $from;
23
24
    /**
25
     * @var string
26
     */
27
    protected $to;
28
29
    /**
30
     * @var double
31
     */
32
    protected $ratio;
33
34
    /* ------------------------------------------------------------------------------------------------
35
     |  Constructor
36
     | ------------------------------------------------------------------------------------------------
37
     */
38
    /**
39
     * Rate constructor.
40
     *
41
     * @param  string  $from
42
     * @param  string  $to
43
     * @param  double  $ratio
44
     */
45 120
    public function __construct($from, $to, $ratio)
46
    {
47 120
        $this->from  = $from;
48 120
        $this->to    = $to;
49 120
        $this->ratio = $ratio;
50 120
    }
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Getters & Setters
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Get the `from` currency iso.
58
     *
59
     * @return string
60
     */
61 84
    public function from()
62
    {
63 84
        return $this->from;
64
    }
65
66
    /**
67
     * Get the `from` currency.
68
     *
69
     * @return \Arcanedev\Currencies\Entities\Currency
70
     */
71 12
    public function getFromCurrency()
72
    {
73 12
        return currency_manager()->findOrFail($this->from());
74
    }
75
76
    /**
77
     * Get the `to` currency iso.
78
     *
79
     * @return string
80
     */
81 96
    public function to()
82
    {
83 96
        return $this->to;
84
    }
85
86
    /**
87
     * Get the `to` currency.
88
     *
89
     * @return \Arcanedev\Currencies\Entities\Currency
90
     */
91 12
    public function getToCurrency()
92
    {
93 12
        return currency_manager()->findOrFail($this->to());
94
    }
95
96
    /**
97
     * Get the rate ratio.
98
     *
99
     * @return float
100
     */
101 84
    public function ratio()
102
    {
103 84
        return $this->ratio;
104
    }
105
106
    /* ------------------------------------------------------------------------------------------------
107
     |  Main Functions
108
     | ------------------------------------------------------------------------------------------------
109
     */
110
    /**
111
     * Make a rate instance.
112
     *
113
     * @param  string  $from
114
     * @param  string  $to
115
     * @param  double  $ratio
116
     *
117
     * @return self
118
     */
119 108
    public static function make($from, $to, $ratio)
120
    {
121 108
        return new self($from, $to, $ratio);
122
    }
123
124
    /**
125
     * Convert the amount.
126
     *
127
     * @param  double|int  $amount
128
     *
129
     * @return double|int
130
     */
131 12
    public function convert($amount)
132
    {
133 12
        return $amount * $this->ratio();
134
    }
135
136
    /**
137
     * Reverse the rate.
138
     *
139
     * @return self
140
     */
141 12
    public function reverse()
142
    {
143 12
        return self::make(
144 12
            $this->to(),
145 12
            $this->from(),
146 12
            round(1 / $this->ratio(), 6)
147 9
        );
148
    }
149
150
    /**
151
     * Get the instance as an array.
152
     *
153
     * @return array
154
     */
155 24
    public function toArray()
156
    {
157
        return [
158 24
            'from'  => $this->from(),
159 24
            'to'    => $this->to(),
160 24
            'ratio' => $this->ratio(),
161 18
        ];
162
    }
163
164
    /**
165
     * Convert the object to its JSON representation.
166
     *
167
     * @param  int $options
168
     * @return string
169
     */
170 12
    public function toJson($options = 0)
171
    {
172 12
        return json_encode($this->toArray(), $options);
173
    }
174
}
175