Completed
Push — master ( 8fd5ec...7383a6 )
by ARCANEDEV
7s
created

Currency::toArray()   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
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Support\Arr;
8
9
/**
10
 * Class     Currency
11
 *
12
 * @package  Arcanedev\Currencies\Entities
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @property  string  key
16
 * @property  bool    is_iso
17
 * @property  string  iso_numeric
18
 * @property  string  name
19
 * @property  string  symbol
20
 * @property  array   alternate_symbols
21
 * @property  string  subunit
22
 * @property  int     subunit_to_unit
23
 * @property  bool    symbol_first
24
 * @property  string  html_entity
25
 * @property  string  decimal_separator
26
 * @property  string  thousands_separator
27
 */
28
class Currency implements CurrencyContract, Arrayable, Jsonable
29
{
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Properties
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * Currency attributes.
36
     *
37
     * @var array
38
     */
39
    protected $attributes = [];
40
41
    /* ------------------------------------------------------------------------------------------------
42
     |  Constructor
43
     | ------------------------------------------------------------------------------------------------
44
     */
45
    /**
46
     * Currency constructor.
47
     *
48
     * @param  string  $key
49
     * @param  array   $attributes
50
     */
51 180
    public function __construct($key, array $attributes)
52
    {
53 180
        $attributes = compact('key') + $attributes;
54 180
        $this->checkRequiredAttributes($attributes);
55 180
        $this->attributes = $attributes;
56 180
    }
57
58
    /* ------------------------------------------------------------------------------------------------
59
     |  Getters & Setters
60
     | ------------------------------------------------------------------------------------------------
61
     */
62
    /**
63
     * Get an attribute.
64
     *
65
     * @param  string  $name
66
     *
67
     * @return mixed
68
     */
69 180
    public function __get($name)
70
    {
71 180
        return Arr::get($this->attributes, $name);
72
    }
73
74
    /**
75
     * Get required attributes.
76
     *
77
     * @return array
78
     */
79 180
    protected function getRequiredAttributes()
80
    {
81
        return [
82 180
            'key', 'iso_numeric', 'name', 'symbol', 'alternate_symbols', 'subunit', 'subunit_to_unit',
83 135
            'symbol_first', 'html_entity', 'decimal_separator', 'thousands_separator',
84 135
        ];
85
    }
86
87
    /* ------------------------------------------------------------------------------------------------
88
     |  Main Functions
89
     | ------------------------------------------------------------------------------------------------
90
     */
91
    /**
92
     * Make a currency instance.
93
     *
94
     * @param  string  $key
95
     * @param  array   $attributes
96
     *
97
     * @return self
98
     */
99 180
    public static function make($key, array $attributes)
100
    {
101 180
        return new self($key, $attributes);
102
    }
103
104
    /**
105
     * Format the currency amount.
106
     *
107
     * @param  int  $amount    -  Amount in cents
108
     * @param  int  $decimals
109
     *
110
     * @return string
111
     */
112 12
    public function format($amount, $decimals = 2)
113
    {
114 12
        $formatted = number_format(
115 12
            $amount / $this->subunit_to_unit,
116 9
            $decimals,
117 12
            $this->decimal_separator,
118 12
            $this->thousands_separator
119 9
        );
120
121 12
        return $this->symbol_first
122 12
            ? $this->symbol.' '.$formatted
123 12
            : $formatted.' '.$this->symbol;
124
    }
125
126
    /**
127
     * Get the instance as an array.
128
     *
129
     * @return array
130
     */
131 24
    public function toArray()
132
    {
133 24
        return $this->attributes;
134
    }
135
136
    /**
137
     * Convert the object to its JSON representation.
138
     *
139
     * @param  int  $options
140
     *
141
     * @return string
142
     */
143 12
    public function toJson($options = 0)
144
    {
145 12
        return json_encode($this->toArray(), $options);
146
    }
147
148
    /* ------------------------------------------------------------------------------------------------
149
     |  Check Functions
150
     | ------------------------------------------------------------------------------------------------
151
     */
152
    /**
153
     * Check the required attributes.
154
     *
155
     * @param  array  $attributes
156
     *
157
     * @throws \Arcanedev\Currencies\Exceptions\InvalidCurrencyArgumentException
158
     */
159 180
    private function checkRequiredAttributes(array $attributes)
160
    {
161 180
        $missing = array_diff($this->getRequiredAttributes(), array_keys($attributes));
162
163 180
        if ( ! empty($missing)) {
164 12
            throw new InvalidCurrencyArgumentException(
165 12
                'The Currency attributes are missing: ['.implode(', ', $missing).']'
166 9
            );
167
        }
168 180
    }
169
}
170