/** * 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); }
/** * 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; }
/** * @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; }
/** * Formats currency number according to locale settings. * * Options format: * array( * 'currency' => <currency>, * 'attributes' => array( * <attribute> => <value>, * ... * ), * 'textAttributes' => array( * <attribute> => <value>, * ... * ), * 'symbols' => array( * <symbol> => <value>, * ... * ), * 'locale' => <locale> * ) * * @param float $value * @param array $options * @return string */ public function formatCurrency($value, array $options = array()) { $currency = $this->getOption($options, 'currency'); $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->formatCurrency($value, $currency, $attributes, $textAttributes, $symbols, $locale); }
/** * @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; }
/** * @dataProvider formatCurrencyDataProvider */ public function testFormatCurrency($expected, $value, $currency, $attributes, $textAttributes, $symbols, $locale) { $currencySymbolMap = [['USD', '$'], ['RUB', 'руб.']]; $this->localeSettings->expects($this->any())->method('getCurrencySymbolByCurrency')->will($this->returnValueMap($currencySymbolMap)); $this->assertEquals($expected, $this->formatter->formatCurrency($value, $currency, $attributes, $textAttributes, $symbols, $locale)); }
/** * @param ProductPrice $productPrice * @return string */ protected function formatProductPrice(ProductPrice $productPrice) { $price = $productPrice->getPrice(); return $this->numberFormatter->formatCurrency($price->getValue(), $price->getCurrency()); }