/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $currency = $options['currency']; $isPrepend = $this->numberFormatter->isCurrencySymbolPrepend($currency); $view->vars['money_pattern'] = $this->getPattern($currency, $isPrepend); $view->vars['currency_symbol'] = $options['currency_symbol']; $view->vars['currency_symbol_prepend'] = $isPrepend; }
/** * Formats currency number according to locale settings. * * Options format: * array( * 'attributes' => array( * <attribute> => <value>, * ... * ), * 'textAttributes' => array( * <attribute> => <value>, * ... * ), * 'symbols' => array( * <symbol> => <value>, * ... * ), * 'locale' => <locale> * ) * * @param Price $price * @param array $options * @return string */ public function formatPrice(Price $price, array $options = []) { $value = $price->getValue(); $currency = $price->getCurrency(); $attributes = (array) $this->getOption($options, 'attributes', []); $textAttributes = (array) $this->getOption($options, 'textAttributes', []); $symbols = (array) $this->getOption($options, 'symbols', []); $locale = $this->getOption($options, 'locale'); return $this->formatter->formatCurrency($value, $currency, $attributes, $textAttributes, $symbols, $locale); }
/** * @param string $value * @param array $options * @return string */ protected function applyFrontendFormatting($value, array $options) { $frontendType = isset($options['frontend_type']) ? $options['frontend_type'] : null; switch ($frontendType) { case PropertyInterface::TYPE_DATE: $value = $this->dateTimeFormatter->formatDate($value); break; case PropertyInterface::TYPE_DATETIME: $value = $this->dateTimeFormatter->format($value); break; case PropertyInterface::TYPE_DECIMAL: $value = $this->numberFormatter->formatDecimal($value); break; case PropertyInterface::TYPE_INTEGER: $value = $this->numberFormatter->formatDecimal($value); break; case PropertyInterface::TYPE_BOOLEAN: $value = $this->translator->trans((bool) $value ? self::YES_LABEL_KEY : self::NO_LABEL_KEY); break; case PropertyInterface::TYPE_PERCENT: $value = $this->numberFormatter->formatPercent($value); break; case PropertyInterface::TYPE_CURRENCY: $value = $this->numberFormatter->formatCurrency($value); break; case PropertyInterface::TYPE_SELECT: if (isset($options['choices'][$value])) { $value = $this->translator->trans($options['choices'][$value]); } break; } return $value; }
/** * Apply formatting to totals values * * @param mixed|null $val * @param string|null $formatter * @return string|null */ protected function applyFrontendFormatting($val = null, $formatter = null) { if (null != $formatter) { switch ($formatter) { case PropertyInterface::TYPE_DATE: $val = $this->dateTimeFormatter->formatDate($val); break; case PropertyInterface::TYPE_DATETIME: $val = $this->dateTimeFormatter->format($val); break; case PropertyInterface::TYPE_TIME: $val = $this->dateTimeFormatter->formatTime($val); break; case PropertyInterface::TYPE_DECIMAL: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_INTEGER: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_PERCENT: $val = $this->numberFormatter->formatPercent($val); break; case PropertyInterface::TYPE_CURRENCY: $val = $this->numberFormatter->formatCurrency($val); break; } } return $val; }
/** * @param BaseQuoteProductItem $item * @param string $default * @return string */ protected function formatPrice(BaseQuoteProductItem $item, $default = '') { if ($item->getPrice()) { return $this->numberFormatter->formatCurrency($item->getPrice()->getValue(), $item->getPrice()->getCurrency()); } return $default; }
/** * Formats ordinal number according to locale settings. * * Options format: * array( * 'attributes' => array( * <attribute> => <value>, * ... * ), * 'textAttributes' => array( * <attribute> => <value>, * ... * ), * 'symbols' => array( * <symbol> => <value>, * ... * ), * 'locale' => <locale> * ) * * @param float $value * @param array $options * @return string */ public function formatOrdinal($value, array $options = array()) { $attributes = (array) $this->getOption($options, 'attributes', array()); $textAttributes = (array) $this->getOption($options, 'textAttributes', array()); $symbols = (array) $this->getOption($options, 'symbols', array()); $locale = $this->getOption($options, 'locale'); return $this->formatter->formatOrdinal($value, $attributes, $textAttributes, $symbols, $locale); }
/** * @param bool $expected * @param string $currency * @param string|null $locale * @param string|null $defaultLocale * @dataProvider isCurrencySymbolPrependDataProvider */ public function testIsCurrencySymbolPrepend($expected, $currency, $locale, $defaultLocale = null) { if ($defaultLocale) { $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue($defaultLocale)); } else { $this->localeSettings->expects($this->never())->method('getLocale'); } $this->assertEquals($expected, $this->formatter->isCurrencySymbolPrepend($currency, $locale)); }
/** * @param array $inputData * @param array $expectedData * * @dataProvider formatOfferProvider */ public function testFormatOffer(array $inputData, array $expectedData) { /* @var $item QuoteProductOffer */ $item = $inputData['item']; $item->setQuantity($inputData['quantity'])->setProductUnit($inputData['unit'])->setProductUnitCode($inputData['unitCode'])->setPrice($inputData['price']); $this->productUnitValueFormatter->expects($inputData['unit'] ? $this->once() : $this->never())->method('format')->with($inputData['quantity'], $inputData['unitCode'])->will($this->returnValue($expectedData['formattedUnits'])); $price = $inputData['price'] ?: new Price(); $this->numberFormatter->expects($price ? $this->once() : $this->never())->method('formatCurrency')->with($price->getValue(), $price->getCurrency())->will($this->returnValue($expectedData['formattedPrice'])); $this->productUnitLabelFormatter->expects($this->once())->method('format')->with($inputData['unitCode'])->will($this->returnValue($expectedData['formattedUnit'])); $this->translator->expects($this->once())->method('transChoice')->with($expectedData['transConstant'], $expectedData['transIndex'], ['{units}' => $expectedData['formattedUnits'], '{price}' => $expectedData['formattedPrice'], '{unit}' => $expectedData['formattedUnit']]); $this->formatter->formatOffer($inputData['item']); }
/** * @param ValueRenderEvent $fieldValueEvent */ public function beforeValueRender(ValueRenderEvent $fieldValueEvent) { $originalValue = $fieldValueEvent->getOriginalValue(); $metadata = $fieldValueEvent->getMetadata(); if ($originalValue instanceof AddressInterface) { $fieldValueEvent->setConvertedValue($this->addressFormatter->format($originalValue)); } elseif ($originalValue instanceof NamePrefixInterface || $originalValue instanceof FirstNameInterface || $originalValue instanceof MiddleNameInterface || $originalValue instanceof LastNameInterface || $originalValue instanceof NameSuffixInterface) { $fieldValueEvent->setConvertedValue($this->nameFormatter->format($originalValue)); } elseif ($originalValue instanceof \DateTime) { $dateType = $metadata->get('render_date_type'); $timeType = $metadata->get('render_time_type'); $dateTimePattern = $metadata->get('render_datetime_pattern'); $fieldValueEvent->setConvertedValue($this->dateTimeFormatter->format($originalValue, $dateType, $timeType, null, null, $dateTimePattern)); } elseif (is_numeric($originalValue)) { $numberStyle = $metadata->get('render_number_style'); if (!$numberStyle) { $numberStyle = 'default_style'; } $fieldValueEvent->setConvertedValue($this->numberFormatter->format($originalValue, $numberStyle)); } }
/** * @param float $value * @param string $type * @param bool $isDeviant * * @return string */ public function formatValue($value, $type = '', $isDeviant = false) { $sign = null; if ($isDeviant && $value !== 0) { $sign = $value > 0 ? '+' : '−'; $value = abs($value); } switch ($type) { case 'currency': $value = $this->numberFormatter->formatCurrency($value); break; case 'percent': $precision = 4; if ($isDeviant) { $precision = 2; } $value = round($value, $precision); $value = $this->numberFormatter->formatPercent($value); break; default: $value = $this->numberFormatter->formatDecimal($value); } return !is_null($sign) ? sprintf('%s%s', $sign, $value) : $value; }
/** * @param mixed $val * @param array $options * * @return string|null * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function applyFrontendFormatting($val, $options) { if (null !== $val) { $frontendType = isset($options['frontend_type']) ? $options['frontend_type'] : null; switch ($frontendType) { case PropertyInterface::TYPE_DATE: $val = $this->dateTimeFormatter->formatDate($val); break; case PropertyInterface::TYPE_DATETIME: $val = $this->dateTimeFormatter->format($val); break; case PropertyInterface::TYPE_TIME: $val = $this->dateTimeFormatter->formatTime($val); break; case PropertyInterface::TYPE_DECIMAL: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_INTEGER: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_BOOLEAN: $val = $this->translator->trans((bool) $val ? 'Yes' : 'No', [], 'jsmessages'); break; case PropertyInterface::TYPE_PERCENT: $val = $this->numberFormatter->formatPercent($val); break; case PropertyInterface::TYPE_CURRENCY: $val = $this->numberFormatter->formatCurrency($val); break; case PropertyInterface::TYPE_SELECT: if (isset($options['choices'][$val])) { $val = $this->translator->trans($options['choices'][$val]); } break; case PropertyInterface::TYPE_HTML: $val = $this->formatHtmlFrontendType($val, isset($options['export_type']) ? $options['export_type'] : null); break; } } return $val; }
/** * @param mixed $value * @param string $type * @param bool $isDeviant * * @return string */ protected function formatValue($value, $type = '', $isDeviant = false) { $sign = null; if ($isDeviant && $value !== 0) { $sign = $value > 0 ? '+' : '−'; $value = abs($value); } switch ($type) { case 'currency': $value = $this->numberFormatter->formatCurrency($value); break; case 'percent': if ($isDeviant) { $value = round($value * 100, 0) / 100; } else { $value = round($value * 100, 2) / 100; } $value = $this->numberFormatter->formatPercent($value); break; default: $value = $this->numberFormatter->formatDecimal($value); } return $isDeviant && !is_null($sign) ? sprintf('%s%s', $sign, $value) : $value; }
/** * @param mixed $value * @param string $type * @param bool $isDeviant * * @return string */ protected function formatValue($value, $type = '', $isDeviant = false) { $sign = null; $precision = 2; if ($isDeviant) { if ($value !== 0) { $sign = $value > 0 ? '+' : '−'; $value = abs($value); } $precision = 0; } if ($type === 'currency') { $formattedValue = $this->numberFormatter->formatCurrency($value); } elseif ($type === 'percent') { $value = round($value * 100, $precision) / 100; $formattedValue = $this->numberFormatter->formatPercent($value); } else { $formattedValue = $this->numberFormatter->formatDecimal($value); } if ($sign) { $formattedValue = sprintf('%s%s', $sign, $formattedValue); } return $formattedValue; }
/** * Creates instance of NumberFormatter class of intl extension * * @param string $locale * @param int $style * @param array $attributes * @param array $textAttributes * @param array $symbols * @throws \InvalidArgumentException * @return IntlNumberFormatter */ protected function getFormatter($locale, $style, array $attributes = array(), array $textAttributes = array(), array $symbols = array()) { $formatter = new IntlNumberFormatter($locale ?: $this->localeSettings->getLocale(), $this->parseStyle($style)); foreach ($this->parseAttributes($attributes) as $attribute => $value) { $formatter->setAttribute($attribute, $value); } foreach ($this->parseAttributes($textAttributes) as $attribute => $value) { $formatter->setTextAttribute($attribute, $value); } foreach ($this->parseAttributes($symbols) as $symbol => $value) { $formatter->setSymbol($symbol, $value); } return $formatter; }
/** * @param IntlNumberFormatter $formatter * @param string $locale * @param int $style * @param array $attributes */ protected function adjustFormatter(IntlNumberFormatter $formatter, $locale, $style, array $attributes = array()) { // need to manually set percent fraction same to decimal if ($style === \NumberFormatter::PERCENT) { $overriddenDecimalAttributes = array(\NumberFormatter::FRACTION_DIGITS, \NumberFormatter::MIN_FRACTION_DIGITS, \NumberFormatter::MAX_FRACTION_DIGITS); $decimalFormatter = $this->getFormatter($locale, \NumberFormatter::DECIMAL); foreach ($overriddenDecimalAttributes as $decimalAttribute) { if (!array_key_exists($decimalAttribute, $attributes)) { $formatter->setAttribute($decimalAttribute, $decimalFormatter->getAttribute($decimalAttribute)); } } } }
public function testIsCurrencySymbolPrependWithoutLocale() { $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue('en')); $this->localeSettings->expects($this->once())->method('getCurrency')->will($this->returnValue('RUR')); $this->assertEquals(true, $this->formatter->isCurrencySymbolPrepend()); }
/** * @param Price $price * @param array $options * @param string $expected * @dataProvider formatCurrencyDataProvider */ public function testFormatCurrency(Price $price, array $options, $expected) { $this->formatter->expects($this->once())->method('formatCurrency')->with($price->getValue(), $price->getCurrency(), $options['attributes'], $options['textAttributes'], $options['symbols'], $options['locale'])->will($this->returnValue($expected)); $this->assertEquals($expected, $this->extension->formatPrice($price, $options)); }
/** * @param ProductPrice $productPrice * @return string */ protected function formatProductPrice(ProductPrice $productPrice) { $price = $productPrice->getPrice(); return $this->numberFormatter->formatCurrency($price->getValue(), $price->getCurrency()); }