/** * @param float $value Money amount. * * @param string $currency This can be any 3 letter ISO 4217 code. You * can also set this to false to hide the currency symbol. * * @param integer $precision For some reason, if you need some precision * other than 2 decimal places, you can modify this value. You probably * won't need to do this unless, for example, you want to round to the * nearest dollar (set the precision to 0). * * @param string $grouping This value is used internally as the * NumberFormatter::GROUPING_USED value when using PHP's NumberFormatter * class. Its documentation is non-existent, but it appears that if you set * this to true, numbers will be grouped with a comma or period (depending * on your locale): 12345.123 would display as 12,345.123. * * @param integer $divisor If, for some reason, you need to divide your * starting value by a number before rendering it to the user, you can use * the divisor option. * * @return string Localized money */ public function getLocalizedMoney($value, $currency = 'EUR', $precision = 2, $grouping = true, $divisor = 1) { $locale = \Locale::getDefault(); $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $pattern = $format->formatCurrency('123', $currency); $dt = new MoneyToLocalizedStringTransformer($precision, $grouping, null, $divisor); $transformed_value = $dt->transform($value); preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123(?:[,.]0+)?[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/u', $pattern, $matches); if (!empty($matches[1])) { $localized_money = $matches[1] . ' ' . $transformed_value; } elseif (!empty($matches[2])) { $localized_money = $transformed_value . ' ' . $matches[2]; } else { $localized_money = $transformed_value; } return $localized_money; }
/** * {@inheritdoc} */ public function reverseTransform($value) { if (null === $value) { return null; } if (!is_array($value)) { throw new UnexpectedTypeException($value, 'array'); } if (!isset($value['tbbc_amount']) || !isset($value['tbbc_currency'])) { return null; } $amount = (string) $value['tbbc_amount']; $amount = str_replace(" ", "", $amount); $amount = $this->sfTransformer->reverseTransform($amount); $amount = round($amount); $amount = (int) $amount; return new Money($amount, $value['tbbc_currency']); }
public function transform($value) { if ($value === null || is_string($value) && trim($value) === '') { return null; } if ($value instanceof Money) { $value = (string) $value; } return parent::transform($value); }
public function testReverseTransform_empty() { $transformer = new MoneyToLocalizedStringTransformer(); $this->assertSame(null, $transformer->reverseTransform('', null)); }
/** * {@inheritdoc} */ public function reverseTransform($value) { $value = parent::reverseTransform($value); return null === $value ? $value : (int) round($value); }
public function testReverseTransformEmpty() { $transformer = new MoneyToLocalizedStringTransformer(); $this->assertNull($transformer->reverseTransform('')); }
protected function getNumberFormatter() { $formatter = parent::getNumberFormatter(); $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100); return $formatter; }