public function transform($value) { if (is_null($value)) { return $value; } if (is_scalar($value)) { $value = (string) $value; } if (!is_string($value)) { throw new TransformationFailedException(sprintf('Expected a string to transform, got %s instead', json_encode($value))); } // make sure grouping is not used $this->grouping = false; $formatter = $this->getNumberFormatter(); $groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL); $decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); // remove grouping separator $value = str_replace($groupSep, '', $value); // try to match something like 1234 or 1234<dec>45 // discard alphanumeric characters altogether if (!preg_match('/(\\-?\\d+(' . preg_quote($decSep) . '\\d+)?)/', $value, $matches)) { // could not find any digit return null; } // use the matched numbers as value $value = $matches[1]; return parent::transform($value); }
/** * @dataProvider getInvalidTransformationData * @expectedException \TreeHouse\Feeder\Exception\TransformationFailedException */ public function testTransformationFailure($value) { $transformer = new LocalizedStringToNumberTransformer(null, null, true); $transformer->transform($value); }