Number::getLocalesKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Propaganistas\LaravelIntl;
4
5
use CommerceGuys\Intl\Formatter\NumberFormatter;
6
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
7
use Illuminate\Support\Arr;
8
use Propaganistas\LaravelIntl\Concerns\WithLocales;
9
use Propaganistas\LaravelIntl\Contracts\Intl;
10
11
class Number extends Intl
12
{
13
    use WithLocales;
14
15
    /**
16
     * Array of localized number formatters.
17
     *
18
     * @var array
19
     */
20
    protected $formatters;
21
22
    /**
23
     * Format a number.
24
     *
25
     * @param string|int|float $number
26
     * @param array $options
27
     * @return string
28
     */
29 15
    public function format($number, $options = [])
30
    {
31 15
        return $this->formatter()->format($number,
32 15
            $this->mergeOptions($options)
33
        );
34
    }
35
36
    /**
37
     * Format as percentage.
38
     *
39
     * @param string|int|float $number
40
     * @param array $options
41
     * @return string
42
     */
43 3
    public function percent($number, $options = [])
44
    {
45 3
        return $this->formatter()->format($number,
46 3
            $this->mergeOptions($options, ['style' => 'percent'])
47
        );
48
    }
49
50
    /**
51
     * Parse a localized number into native PHP format.
52
     *
53
     * @param string|int|float $number
54
     * @param array $options
55
     * @return string|false
56
     */
57 3
    public function parse($number, $options = [])
58
    {
59 3
        return $this->formatter()->parse($number,
60 3
            $this->mergeOptions($options)
61
        );
62
    }
63
64
    /**
65
     * Get the formatter's key.
66
     *
67
     * @param string $locale
68
     * @param string $fallbackLocale
69
     * @return string
70
     */
71 21
    protected function getLocalesKey($locale, $fallbackLocale)
72
    {
73 21
        return implode('|', [
74 21
            $locale,
75 21
            $fallbackLocale,
76
        ]);
77
    }
78
79
    /**
80
     * The current number formatter.
81
     *
82
     * @return \CommerceGuys\Intl\Formatter\NumberFormatter
83
     */
84 21 View Code Duplication
    protected function formatter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86 21
        $key = $this->getLocalesKey(
87 21
            $locale = $this->getLocale(),
88 21
            $fallbackLocale = $this->getFallbackLocale()
89
        );
90
91 21
        if (! isset($this->formatters[$key])) {
92 21
            $this->formatters[$key] = new NumberFormatter(new NumberFormatRepository($fallbackLocale), ['locale' => $locale]);
93
        }
94
95 21
        return $this->formatters[$key];
96
    }
97
98
    /**
99
     * Merges the options array.
100
     *
101
     * @param array $options
102
     * @param array $defaults
103
     * @return array
104
     */
105 21
    protected function mergeOptions(array $options, array $defaults = [])
106
    {
107 21
        Arr::forget($options, 'locale');
108
109 21
        return $defaults + $options;
110
    }
111
}