本文整理匯總了PHP中NumberFormatter::getErrorCode方法的典型用法代碼示例。如果您正苦於以下問題:PHP NumberFormatter::getErrorCode方法的具體用法?PHP NumberFormatter::getErrorCode怎麽用?PHP NumberFormatter::getErrorCode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類NumberFormatter
的用法示例。
在下文中一共展示了NumberFormatter::getErrorCode方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value)) {
throw new UnexpectedTypeException($value, 'string');
}
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
$position = 0;
$formatter->parse($value, \NumberFormatter::TYPE_DOUBLE, $position);
if (intl_is_failure($formatter->getErrorCode()) || $position < strlen($value)) {
/** @var Decimal $constraint */
$this->context->addViolation($constraint->message, ['{{ value }}' => $this->formatValue($value)]);
}
}
示例2: format
/**
* {@inheritdoc}
*/
public function format($value, array $options = array())
{
if (null === $value) {
return $options['null_value'];
}
if (!is_numeric($value)) {
throw FormatterException::invalidType($this, $value, 'numeric value');
}
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
if (null !== $options['precision']) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $options['rounding_mode']);
}
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $options['grouping']);
$value = $formatter->format($value);
if (intl_is_failure($formatter->getErrorCode())) {
throw FormatterException::intlError($this, $formatter->getErrorMessage());
}
return $value;
}
示例3: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value)) {
throw new UnexpectedTypeException($value, 'string');
}
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0);
$formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0);
$decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$position = 0;
$formatter->parse($value, PHP_INT_SIZE == 8 ? $formatter::TYPE_INT64 : $formatter::TYPE_INT32, $position);
if (intl_is_failure($formatter->getErrorCode()) || strpos($value, $decimalSeparator) !== false || $position < strlen($value)) {
/** @var Integer $constraint */
$this->context->addViolation($constraint->message, ['{{ value }}' => $this->formatValue($value)]);
}
}
示例4: getDisplayedValue
public function getDisplayedValue($value)
{
if ($value !== null && $value !== '') {
$formatter = new \NumberFormatter($this->locale, $this->style);
if ($this->precision !== null) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
}
if ($this->ruleSet !== null) {
$formatter->setTextAttribute(\NumberFormatter::DEFAULT_RULESET, $this->ruleSet);
}
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping);
if ($this->style === \NumberFormatter::PERCENT && !$this->fractional) {
$value /= 100;
}
if ($this->style === \NumberFormatter::CURRENCY) {
if ($this->currencyCode === null) {
$this->currencyCode = $formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
}
if (strlen($this->currencyCode) !== 3) {
throw new TransformationFailedException('Your locale definition is not complete, you have to define a language and a country. (.e.g en_US, fr_FR)');
}
$value = $formatter->formatCurrency($value, $this->currencyCode);
} else {
$value = $formatter->format($value);
}
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
if (key_exists((string) $value, $this->values)) {
$value = $this->values[$value];
}
return $value;
}
return '';
}
示例5: throwNumberFormatterException
/**
* Throws an Exception when number cannot be formatted
* @param IntlNumberFormatter $intlFormatter
* @param int|string|float $number
* @throws Exception\RuntimeException
*/
protected function throwNumberFormatterException(IntlNumberFormatter $intlFormatter, $number)
{
$error_code = $intlFormatter->getErrorCode();
if (is_scalar($number)) {
$val = (string) $number;
} else {
$val = 'type: ' . gettype($number);
}
throw new Exception\RuntimeException(__METHOD__ . " Cannot format value '{$val}', Intl/NumberFormatter error code: {$error_code}.");
}