|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Item\Modifier\Data\Transformer; |
|
4
|
|
|
|
|
5
|
|
|
use TreeHouse\Feeder\Exception\TransformationFailedException; |
|
6
|
|
|
use TreeHouse\Feeder\Modifier\Data\Transformer\LocalizedStringToNumberTransformer as BaseTransformer; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Transforms a string into a number. This one is a bit more lenient than the |
|
10
|
|
|
* base number transformer, as it tries to match a number in a string first, and |
|
11
|
|
|
* then transforms that into a number. |
|
12
|
|
|
*/ |
|
13
|
|
|
class LocalizedStringToNumberTransformer extends BaseTransformer |
|
14
|
|
|
{ |
|
15
|
10 |
|
public function transform($value) |
|
16
|
|
|
{ |
|
17
|
10 |
|
if (is_null($value)) { |
|
18
|
|
|
return $value; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
10 |
|
if (is_scalar($value)) { |
|
22
|
10 |
|
$value = (string) $value; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
10 |
|
if (!is_string($value)) { |
|
26
|
|
|
throw new TransformationFailedException( |
|
27
|
|
|
sprintf('Expected a string to transform, got %s instead', json_encode($value)) |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// make sure grouping is not used |
|
32
|
10 |
|
$this->grouping = false; |
|
33
|
|
|
|
|
34
|
10 |
|
$formatter = $this->getNumberFormatter(); |
|
35
|
10 |
|
$groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL); |
|
36
|
10 |
|
$decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); |
|
37
|
|
|
|
|
38
|
|
|
// remove grouping separator |
|
39
|
10 |
|
$value = str_replace($groupSep, '', $value); |
|
40
|
|
|
|
|
41
|
|
|
// try to match something like 1234 or 1234<dec>45 |
|
42
|
|
|
// discard alphanumeric characters altogether |
|
43
|
10 |
|
if (!preg_match('/(\-?\d+(' . preg_quote($decSep) . '\d+)?)/', $value, $matches)) { |
|
44
|
|
|
// could not find any digit |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// use the matched numbers as value |
|
49
|
10 |
|
$value = $matches[1]; |
|
50
|
|
|
|
|
51
|
10 |
|
return parent::transform($value); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|