/** * Creates a new number format instance. The constructor can be instantiated * with a string that represent a culture/locale. Similarly, passing * a sfCultureInfo or sfNumberFormatInfo instance will instantiated a instance * for that particular culture. * * @param mixed $formatInfo either null, a sfCultureInfo, a sfNumberFormatInfo, or string * @return sfNumberFormat */ function __construct($formatInfo = null) { if (null === $formatInfo) { $this->formatInfo = sfNumberFormatInfo::getInvariantInfo(); } else { if ($formatInfo instanceof sfCultureInfo) { $this->formatInfo = $formatInfo->sfNumberFormat; } else { if ($formatInfo instanceof sfNumberFormatInfo) { $this->formatInfo = $formatInfo; } else { $this->formatInfo = sfNumberFormatInfo::getInstance($formatInfo); } } } }
<?php require_once dirname(__FILE__) . '/../bootstrap.php'; require_once dirname(__FILE__) . '/../../lib/widget/knpWidgetLocaleAwareNumber.class.php'; $t = new lime_test(1); // ->render() $t->diag('->render()'); $format = clone sfNumberFormatInfo::getInstance(); $format->setDecimalSeparator(','); $widget = new knpWidgetLocaleAwareNumber(array('format' => $format)); $t->is($widget->render('id', '9.3'), '<input type="text" name="id" value="9,3" id="id" />', '9.3 is rendered as 9,3');
// setters/getters foreach (array('DecimalDigits', 'DecimalSeparator', 'GroupSeparator', 'CurrencySymbol', 'NegativeInfinitySymbol', 'PositiveInfinitySymbol', 'NegativeSign', 'PositiveSign', 'NaNSymbol', 'PercentSymbol', 'PerMilleSymbol') as $method) { $t->diag(sprintf('->get%s() ->set%s()', $method, $method)); $n = sfNumberFormatInfo::getInstance(); $setter = 'set' . $method; $getter = 'get' . $method; $n->{$setter}('foo'); $t->is($n->{$getter}(), 'foo', sprintf('->%s() sets the current decimal digits', $setter)); $t->is($n->{$method}, $n->{$getter}(), sprintf('->%s() is equivalent to ->%s', $getter, $method)); $n->{$method} = 'bar'; $t->is($n->{$getter}(), 'bar', sprintf('->%s() is equivalent to ->%s = ', $setter, $method)); } foreach (array('GroupSizes', 'NegativePattern', 'PositivePattern') as $method) { $t->diag(sprintf('->get%s() ->set%s()', $method, $method)); $n = sfNumberFormatInfo::getInstance(); $setter = 'set' . $method; $getter = 'get' . $method; $n->{$setter}(array('foo', 'foo')); $t->is($n->{$getter}(), array('foo', 'foo'), sprintf('->%s() sets the current decimal digits', $setter)); $t->is($n->{$method}, $n->{$getter}(), sprintf('->%s() is equivalent to ->%s', $getter, $method)); $n->{$method} = array('bar', 'bar'); $t->is($n->{$getter}(), array('bar', 'bar'), sprintf('->%s() is equivalent to ->%s = ', $setter, $method)); } $tests = array('fr' => array('DecimalDigits' => -1, 'DecimalSeparator' => ',', 'GroupSeparator' => ' ', 'CurrencySymbol' => '$', 'NegativeInfinitySymbol' => '-∞', 'PositiveInfinitySymbol' => '+∞', 'NegativeSign' => '-', 'PositiveSign' => '+', 'NaNSymbol' => '�', 'PercentSymbol' => '%', 'PerMilleSymbol' => '‰'), 'en' => array('DecimalDigits' => -1, 'DecimalSeparator' => '.', 'GroupSeparator' => ',', 'CurrencySymbol' => 'US$', 'NegativeInfinitySymbol' => '-∞', 'PositiveInfinitySymbol' => '+∞', 'NegativeSign' => '-', 'PositiveSign' => '+', 'NaNSymbol' => '�', 'PercentSymbol' => '%', 'PerMilleSymbol' => '‰')); foreach ($tests as $culture => $fixtures) { $n = sfNumberFormatInfo::getInstance($culture); foreach ($fixtures as $method => $result) { $getter = 'get' . $method; $t->is($n->{$getter}(), $result, sprintf('->%s() returns "%s" for culture "%s"', $getter, $result, $culture)); } }
$tests = array('fr' => array('DecimalDigits' => -1, 'DecimalSeparator' => ',', 'GroupSeparator' => ' ', 'CurrencySymbol' => '$US', 'NegativeInfinitySymbol' => '-∞', 'PositiveInfinitySymbol' => '+∞', 'NegativeSign' => '-', 'PositiveSign' => '+', 'NaNSymbol' => 'NaN', 'PercentSymbol' => '%', 'PerMilleSymbol' => '‰'), 'en' => array('DecimalDigits' => -1, 'DecimalSeparator' => '.', 'GroupSeparator' => ',', 'CurrencySymbol' => '$', 'NegativeInfinitySymbol' => '-∞', 'PositiveInfinitySymbol' => '+∞', 'NegativeSign' => '-', 'PositiveSign' => '+', 'NaNSymbol' => 'NaN', 'PercentSymbol' => '%', 'PerMilleSymbol' => '‰')); foreach ($tests as $culture => $fixtures) { $n = sfNumberFormatInfo::getInstance($culture); foreach ($fixtures as $method => $result) { $getter = 'get' . $method; $t->is($n->{$getter}(), $result, sprintf('->%s() returns "%s" for culture "%s"', $getter, $result, $culture)); } } // setters/getters foreach (array('DecimalDigits', 'DecimalSeparator', 'GroupSeparator', 'CurrencySymbol', 'NegativeInfinitySymbol', 'PositiveInfinitySymbol', 'NegativeSign', 'PositiveSign', 'NaNSymbol', 'PercentSymbol', 'PerMilleSymbol') as $method) { $t->diag(sprintf('->get%s() ->set%s()', $method, $method)); $n = sfNumberFormatInfo::getInstance(); $setter = 'set' . $method; $getter = 'get' . $method; $n->{$setter}('foo'); $t->is($n->{$getter}(), 'foo', sprintf('->%s() sets the current decimal digits', $setter)); $t->is($n->{$method}, $n->{$getter}(), sprintf('->%s() is equivalent to ->%s', $getter, $method)); $n->{$method} = 'bar'; $t->is($n->{$getter}(), 'bar', sprintf('->%s() is equivalent to ->%s = ', $setter, $method)); } foreach (array('GroupSizes', 'NegativePattern', 'PositivePattern') as $method) { $t->diag(sprintf('->get%s() ->set%s()', $method, $method)); $n = sfNumberFormatInfo::getInstance(); $setter = 'set' . $method; $getter = 'get' . $method; $n->{$setter}(array('foo', 'foo')); $t->is($n->{$getter}(), array('foo', 'foo'), sprintf('->%s() sets the current decimal digits', $setter)); $t->is($n->{$method}, $n->{$getter}(), sprintf('->%s() is equivalent to ->%s', $getter, $method)); $n->{$method} = array('bar', 'bar'); $t->is($n->{$getter}(), array('bar', 'bar'), sprintf('->%s() is equivalent to ->%s = ', $setter, $method)); }
/** * Internal method to convert cldr number syntax into regex * * @param string $type * @return string */ private static function _getRegexForType($type, $culture) { $num_format = sfNumberFormatInfo::getInstance($culture, $type); $pos_pattern = $num_format->getPattern(); $decimal = $pos_pattern['positive']; $decimal = preg_replace('/[^#0,;\\.\\-Ee]/', '', $decimal); $patterns = explode(';', $decimal); if (count($patterns) == 1) { $patterns[1] = '-' . $patterns[0]; } $num_format = sfCultureInfo::getInstance($culture)->getNumberFormat(); foreach ($patterns as $pkey => $pattern) { $regex[$pkey] = '/^'; $rest = 0; $end = null; if (strpos($pattern, '.') !== false) { $end = substr($pattern, strpos($pattern, '.') + 1); $pattern = substr($pattern, 0, -strlen($end) - 1); } if (strpos($pattern, ',') !== false) { $parts = explode(',', $pattern); $count = count($parts); foreach ($parts as $key => $part) { switch ($part) { case '#': case '-#': if ($part[0] == '-') { $regex[$pkey] .= '[' . $num_format->getNegativeSign() . '-]{0,1}'; } else { $regex[$pkey] .= '[' . $num_format->getPositiveSign() . '+]{0,1}'; } if ($parts[$key + 1] == '##0') { $regex[$pkey] .= '[0-9]{1,3}'; } else { if ($parts[$key + 1] == '##') { $regex[$pkey] .= '[0-9]{1,2}'; } else { throw new sfException('Unsupported token for numberformat (Pos 1):"' . $pattern . '"'); } } break; case '##': if ($parts[$key + 1] == '##0') { $regex[$pkey] .= '(\\' . $num_format->getGroupSeparator() . '{0,1}[0-9]{2})*'; } else { throw new sfException('Unsupported token for numberformat (Pos 2):"' . $pattern . '"'); } break; case '##0': if ($parts[$key - 1] == '##') { $regex[$pkey] .= '[0-9]'; } else { if ($parts[$key - 1] == '#' || $parts[$key - 1] == '-#') { $regex[$pkey] .= '(\\' . $num_format->getGroupSeparator() . '{0,1}[0-9]{3})*'; } else { throw new sfException('Unsupported token for numberformat (Pos 3):"' . $pattern . '"'); } } break; case '#0': if ($key == 0) { $regex[$pkey] .= '[0-9]*'; } else { throw new sfException('Unsupported token for numberformat (Pos 4):"' . $pattern . '"'); } break; } } } if (strpos($pattern, 'E') !== false) { if ($pattern == '#E0' || $pattern == '#E00') { $regex[$pkey] .= '[' . $num_format->getPositiveSign() . '+]{0,1}[0-9]{1,}(\\' . $num_format->getDecimalSeparator() . '[0-9]{1,})*[eE][' . $num_format->getPositiveSign() . '+]{0,1}[0-9]{1,}'; } else { if ($pattern == '-#E0' || $pattern == '-#E00') { $regex[$pkey] .= '[' . $num_format->getNegativeSign() . '-]{0,1}[0-9]{1,}(\\' . $num_format->getDecimalSeparator() . '[0-9]{1,})*[eE][' . $num_format->getNegativeSign() . '-]{0,1}[0-9]{1,}'; } else { throw new sfException('Unsupported token for numberformat (Pos 5):"' . $pattern . '"'); } } } if (!empty($end)) { if ($end == '###') { $regex[$pkey] .= '(\\' . $num_format->getDecimalSeparator() . '{1}[0-9]{1,}){0,1}'; } else { if ($end == '###-') { $regex[$pkey] .= '(\\' . $num_format->getDecimalSeparator() . '{1}[0-9]{1,}){0,1}[' . $num_format->getNegativeSign() . '-]'; } else { throw new sfException('Unsupported token for numberformat (Pos 6):"' . $pattern . '"'); } } } $regex[$pkey] .= '$/'; } return $regex; }