Completed
Push — master ( 0f5e88...aa8f76 )
by Propa
04:13
created

Number::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 10
loc 10
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Propaganistas\LaravelIntl;
2
3
use CommerceGuys\Intl\Formatter\NumberFormatter;
4
use CommerceGuys\Intl\NumberFormat\NumberFormat;
5
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
6
use Propaganistas\LaravelIntl\Base\Intl;
7
use ReflectionClass;
8
9
class Number extends Intl
10
{
11
    /**
12
     * @var \CommerceGuys\Intl\NumberFormat\NumberFormatRepository
13
     */
14
    protected $data;
15
16
    /**
17
     * Number constructor.
18
     *
19
     * @param \CommerceGuys\Intl\NumberFormat\NumberFormatRepository $data
20
     */
21 27
    public function __construct(NumberFormatRepository $data)
22
    {
23 27
        $this->data = $data;
24 27
    }
25
26
    /**
27
     * Format an value in the given locale (or app locale if not specified).
28
     *
29
     * @param int|float $value
30
     * @return string
31
     */
32 12
    public function format($value)
33
    {
34 12
        $format = $this->get();
35 12
        $formatter = new NumberFormatter($format);
36
37 12
        return $formatter->format($value);
38
    }
39
40
    /**
41
     * Format an value as percents in the given locale (or app locale if not specified).
42
     *
43
     * @param int|float $value
44
     * @return string
45
     */
46 3
    public function percent($value)
47
    {
48 3
        $format = $this->get();
49 3
        $formatter = new NumberFormatter($format, NumberFormatter::PERCENT);
50
51 3
        return $formatter->format($value);
52
    }
53
54
    /**
55
     * Parse a localized number into native PHP format.
56
     *
57
     * @param string|int|float $value
58
     * @return int|float
59
     */
60 3 View Code Duplication
    public function parse($value)
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...
61
    {
62 3
        $format = $this->get(null);
63 3
        $formatter = new NumberFormatter($format);
64
65
        // At time of writing, commerceguys/intl has number parsing still coupled to a currency. Parsing does
66
        // succeed however,even though a value is provided without any currency. So let's just pass in
67
        // a very rare currency to avoid unwanted formatting behavior. Sorry Cape Verdean Escudo!
68 3
        return $formatter->parseCurrency($value, $currency = currency()->get('CVE'));
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<CommerceGuys\Intl\Currency\Currency>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
    }
70
71
    /**
72
     * Get a localized entry.
73
     *
74
     * @param string|null $code
75
     * @return mixed
76
     */
77 21
    public function get($code = null)
78
    {
79 21
        return $this->data->get(null);
80
    }
81
82
    /**
83
     * Get a localized list of entries, keyed by their code.
84
     *
85
     * @return array
86
     */
87 3
    public function all()
88
    {
89
        // Unsupported.
90 3
        return [];
91
    }
92
93
    /**
94
     * Get the 'digits' property of a given NumberFormat.
95
     *
96
     * @param \CommerceGuys\Intl\NumberFormat\NumberFormat $format
97
     * @return array
98
     */
99
    private function getDigitsOfFormat(NumberFormat $format)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
100
    {
101
        $formatter = new NumberFormatter($format);
102
103
        $reflection = new ReflectionClass($formatter);
104
        $property = $reflection->getProperty('digits');
105
        $property->setAccessible(true);
106
107
        return $property->getValue($formatter);
108
    }
109
}