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

Currency::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\Currencies\Entities;
2
3
use Arcanedev\Currencies\Contracts\Entities\Currency as CurrencyContract;
4
use Arcanedev\Currencies\Exceptions\InvalidCurrencyArgumentException;
5
use ArrayAccess;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use Illuminate\Support\Arr;
9
10
/**
11
 * Class     Currency
12
 *
13
 * @package  Arcanedev\Currencies\Entities
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @property  string  key
17
 * @property  bool    is_iso
18
 * @property  string  iso_numeric
19
 * @property  string  name
20
 * @property  string  symbol
21
 * @property  array   alternate_symbols
22
 * @property  string  subunit
23
 * @property  int     subunit_to_unit
24
 * @property  bool    symbol_first
25
 * @property  string  html_entity
26
 * @property  string  decimal_separator
27
 * @property  string  thousands_separator
28
 */
29
class Currency implements CurrencyContract, Arrayable, ArrayAccess, Jsonable
30
{
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Properties
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * Currency attributes.
37
     *
38
     * @var array
39
     */
40
    protected $attributes = [];
41
42
    /* ------------------------------------------------------------------------------------------------
43
     |  Constructor
44
     | ------------------------------------------------------------------------------------------------
45
     */
46
    /**
47
     * Currency constructor.
48
     *
49
     * @param  string  $key
50
     * @param  array   $attributes
51
     */
52 384
    public function __construct($key, array $attributes)
53
    {
54 384
        $attributes = compact('key') + $attributes;
55 384
        $this->checkRequiredAttributes($attributes);
56 384
        $this->attributes = $attributes;
57 384
    }
58
59
    /* ------------------------------------------------------------------------------------------------
60
     |  Getters & Setters
61
     | ------------------------------------------------------------------------------------------------
62
     */
63
    /**
64
     * Get an attribute.
65
     *
66
     * @param  string  $name
67
     *
68
     * @return mixed
69
     */
70 384
    public function __get($name)
71
    {
72 384
        return Arr::get($this->attributes, $name);
73
    }
74
75
    /**
76
     * Get required attributes.
77
     *
78
     * @return array
79
     */
80 384
    protected function getRequiredAttributes()
81
    {
82
        return [
83 384
            'key', 'iso_numeric', 'name', 'symbol', 'alternate_symbols', 'subunit', 'subunit_to_unit',
84 288
            'symbol_first', 'html_entity', 'decimal_separator', 'thousands_separator',
85 288
        ];
86
    }
87
88
    /* ------------------------------------------------------------------------------------------------
89
     |  Main Functions
90
     | ------------------------------------------------------------------------------------------------
91
     */
92
    /**
93
     * Make a currency instance.
94
     *
95
     * @param  string  $key
96
     * @param  array   $attributes
97
     *
98
     * @return self
99
     */
100 384
    public static function make($key, array $attributes)
101
    {
102 384
        return new self($key, $attributes);
103
    }
104
105
    /**
106
     * Format the currency amount.
107
     *
108
     * @param  int  $amount    -  Amount in cents
109
     * @param  int  $decimals
110
     *
111
     * @return string
112
     */
113 12
    public function format($amount, $decimals = 2)
114
    {
115 12
        $formatted = number_format(
116 12
            $amount / $this->subunit_to_unit,
117 9
            $decimals,
118 12
            $this->decimal_separator,
119 12
            $this->thousands_separator
120 9
        );
121
122 12
        return $this->symbol_first
123 12
            ? $this->symbol.' '.$formatted
124 12
            : $formatted.' '.$this->symbol;
125
    }
126
127
    /**
128
     * Get the instance as an array.
129
     *
130
     * @return array
131
     */
132 24
    public function toArray()
133
    {
134 24
        return $this->attributes;
135
    }
136
137
    /**
138
     * Convert the object to its JSON representation.
139
     *
140
     * @param  int  $options
141
     *
142
     * @return string
143
     */
144 12
    public function toJson($options = 0)
145
    {
146 12
        return json_encode($this->toArray(), $options);
147
    }
148
149
    /* ------------------------------------------------------------------------------------------------
150
     |  Check Functions
151
     | ------------------------------------------------------------------------------------------------
152
     */
153
    /**
154
     * Check the required attributes.
155
     *
156
     * @param  array  $attributes
157
     *
158
     * @throws \Arcanedev\Currencies\Exceptions\InvalidCurrencyArgumentException
159
     */
160 384
    private function checkRequiredAttributes(array $attributes)
161
    {
162 384
        $missing = array_diff($this->getRequiredAttributes(), array_keys($attributes));
163
164 384
        if ( ! empty($missing)) {
165 12
            throw new InvalidCurrencyArgumentException(
166 12
                'The Currency attributes are missing: ['.implode(', ', $missing).']'
167 9
            );
168
        }
169 384
    }
170
171
    /**
172
     * Whether a offset exists
173
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
174
     *
175
     * @param  string  $offset
176
     *
177
     * @return bool
178
     */
179 12
    public function offsetExists($offset)
180
    {
181 12
        return Arr::has($this->attributes, $offset);
182
    }
183
184
    /**
185
     * Offset to retrieve
186
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
187
     *
188
     * @param  string  $offset
189
     *
190
     * @return mixed
191
     */
192 12
    public function offsetGet($offset)
193
    {
194 12
        return Arr::get($this->attributes, $offset, null);
195
    }
196
197
    /**
198
     * Offset to set
199
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
200
     *
201
     * @param  string  $offset
202
     * @param  mixed   $value
203
     */
204
    public function offsetSet($offset, $value) { /** DO NOTHING **/ }
205
206
    /**
207
     * Offset to unset
208
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
209
     *
210
     * @param string $offset
211
     */
212
    public function offsetUnset($offset) { /** DO NOTHING **/ }
213
}
214