Completed
Push — master ( 7383a6...06d9f7 )
by ARCANEDEV
19s
created

Rate::getFromCurrency()   A

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
crap 1
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 96
    public function __construct($from, $to, $ratio)
46
    {
47 96
        $this->from  = $from;
48 96
        $this->to    = $to;
49 96
        $this->ratio = $ratio;
50 96
    }
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Getters & Setters
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Get the `from` currency iso.
58
     *
59
     * @return string
60
     */
61 60
    public function from()
62
    {
63 60
        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 72
    public function to()
82
    {
83 72
        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 72
    public function ratio()
102
    {
103 72
        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 84
    public static function make($from, $to, $ratio)
120
    {
121 84
        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
     * Get the instance as an array.
138
     *
139
     * @return array
140
     */
141 24
    public function toArray()
142
    {
143
        return [
144 24
            'from'  => $this->from(),
145 24
            'to'    => $this->to(),
146 24
            'ratio' => $this->ratio(),
147 18
        ];
148
    }
149
150
    /**
151
     * Convert the object to its JSON representation.
152
     *
153
     * @param  int $options
154
     * @return string
155
     */
156 12
    public function toJson($options = 0)
157
    {
158 12
        return json_encode($this->toArray(), $options);
159
    }
160
}
161