Completed
Push — 13.x ( dc1925 )
by Tim
03:49
created

SimpleNumberConverter::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\I18n\NumberConverter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Subjects\I18n;
22
23
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
24
25
/**
26
 * Simple number converter implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class SimpleNumberConverter implements NumberConverterInterface
35
{
36
37
    /**
38
     * The subject configuraiton instance.
39
     *
40
     * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface
41
     */
42
    private $subjectConfiguration;
43
44
    /**
45
     * The target number formatter.
46
     *
47
     * @var \NumberFormatter
48
     */
49
    private $formatter;
50
51
    /**
52
     * Initialize the number converter instance.
53
     */
54 9
    public function __construct()
55
    {
56 9
        $this->formatter = \NumberFormatter::create('en_US', \NumberFormatter::DECIMAL);
57 9
    }
58
59
    /**
60
     * Sets the subject configuration instance.
61
     *
62
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration
63
     *
64
     * @return void
65
     */
66
    public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration)
67
    {
68
        $this->subjectConfiguration = $subjectConfiguration;
69
    }
70
71
    /**
72
     * Returns the subject configuration instance.
73
     *
74
     * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration
75
     */
76
    public function getSubjectConfiguration()
77
    {
78
        return $this->subjectConfiguration;
79
    }
80
81
    /**
82
     * Returns the number converter configuration instance.
83
     *
84
     * @return \TechDivision\Import\Configuration\Subject\NumberConverterConfigurationInterface The number converter configuration
85
     */
86 2
    protected function getNumberConverterConfiguration()
87
    {
88 2
        return $this->getSubjectConfiguration()->getNumberConverter();
89
    }
90
91
    /**
92
     * Returns the target number formatter instance.
93
     *
94
     * @return \NumberFormatter The target number formatter instance
95
     */
96
    protected function getFormatter()
97
    {
98
        return $this->formatter;
99
    }
100
101
    /**
102
     * Converts the passed number into a float value.
103
     *
104
     * @param string $number The number to parse
105
     *
106
     * @return float The float value of the number
107
     */
108
    public function convert($number)
109
    {
110
        return $this->getFormatter()->format($this->parse($number));
111
    }
112
113
    /**
114
     * Parse a string into a number using the current formatter rules.
115
     *
116
     * @param string  $value    The value to be converted
117
     * @param integer $type     The formatting type to use, by default NumberFormatter::TYPE_DOUBLE is used
118
     * @param integer $position The offset in the string at which to begin parsing, on return this value will hold the offset at which parsing ended
119
     *
120
     * @return float The value of the parsed number or FALSE on error
121
     */
122 2
    public function parse($value, $type = \NumberFormatter::TYPE_DOUBLE, &$position = null)
123
    {
124 2
        return \NumberFormatter::create($this->getNumberConverterConfiguration()->getLocale(), \NumberFormatter::DECIMAL)->parse($value, $type, $position);
125
    }
126
}
127