/** * {@inheritdoc} */ public function format($value, array $options = []) { if (!$value instanceof \DateTimeInterface) { throw new InvalidTypeException(sprintf('The number formatter expects a numeric value, got "%s".', is_object($value) ? get_class($value) : gettype($value))); } $formatter = new \IntlDateFormatter($this->localeContext->getLocale(), isset($options['date_format']) ? $options['date_format'] : \IntlDateFormatter::MEDIUM, isset($options['time_format']) ? $options['time_format'] : \IntlDateFormatter::MEDIUM, isset($options['timezone']) ? $options['timezone'] : $value->getTimezone(), isset($options['calendar']) ? $options['calendar'] : null, isset($options['pattern']) ? $options['pattern'] : null); $formatter->setLenient(isset($options['lenient']) ? $options['lenient'] : false); return $formatter->format($value); }
/** * {@inheritdoc} */ public function format($value, array $options = []) { if (!is_numeric($value)) { throw new InvalidTypeException(sprintf('The number formatter expects a numeric value, got "%s".', is_object($value) ? get_class($value) : gettype($value))); } $scale = isset($options['scale']) ? $options['scale'] : 2; $rounding = isset($options['rounding']) ? $options['rounding'] : \NumberFormatter::ROUND_HALFUP; $grouping = isset($options['grouping']) ? $options['grouping'] : false; $formatter = new \NumberFormatter($this->localeContext->getLocale(), \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $scale); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $rounding); $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $grouping); return str_replace(" ", ' ', $formatter->format($value)); }
/** * @dataProvider formatProvider */ public function testFormat($number, array $options, $expected) { $this->localeContext->expects($this->once())->method('getLocale')->will($this->returnValue('en')); $this->assertSame($expected, $this->formatter->format($number, $options)); }