/** * 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 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 decimal 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 formatDecimal($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->formatDecimal($value, $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 formatDecimalDataProvider */ public function testFormatDecimal($expected, $value, $attributes, $textAttributes, $symbols, $locale) { $this->assertEquals($expected, $this->formatter->formatDecimal($value, $attributes, $textAttributes, $symbols, $locale)); }