本文整理汇总了PHP中intl_is_failure函数的典型用法代码示例。如果您正苦于以下问题:PHP intl_is_failure函数的具体用法?PHP intl_is_failure怎么用?PHP intl_is_failure使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了intl_is_failure函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: format
/**
* Currency format a number
*
* @throws Exception\RuntimeException
* @param float|string|int $number
* @return string
*/
public function format($number, ArrayObject $row = null)
{
$locale = $this->params['locale'];
//$formatterId = md5($locale);
$formatterId = $locale . (string) $this->params['pattern'];
if (!array_key_exists($formatterId, $this->formatters)) {
$this->loadFormatterId($formatterId);
}
if ($number !== null && !is_numeric($number)) {
$this->throwNumberFormatterException($this->formatters[$formatterId], $number);
}
if ($this->unit_column !== null) {
if (!isset($row[$this->unit_column])) {
throw new Exception\RuntimeException(__METHOD__ . " Cannot determine unit code based on column '{$this->unit_column}'.");
}
$value = $this->formatters[$formatterId]->format($number) . ' ' . $row[$this->unit_column];
} elseif ($this->params['unit'] != '') {
$value = $this->formatters[$formatterId]->format($number) . ' ' . $this->params['unit'];
} else {
throw new Exception\RuntimeException(__METHOD__ . " Unit code must be set prior to use the UnitFormatter");
}
if (intl_is_failure($this->formatters[$formatterId]->getErrorCode())) {
$this->throwNumberFormatterException($this->formatters[$formatterId], $number);
}
return $value;
}
示例2: load
/**
*
* {@inheritdoc}
*
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!is_dir($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
try {
$rb = new \ResourceBundle($locale, $resource);
} catch (\Exception $e) {
// HHVM compatibility: constructor throws on invalid resource
$rb = null;
}
if (!$rb) {
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
if (class_exists('Symfony\\Component\\Config\\Resource\\DirectoryResource')) {
$catalogue->addResource(new DirectoryResource($resource));
}
return $catalogue;
}
示例3: reverseTransform
/**
* Transforms a localized number into an integer or float
*
* @param string $value
*/
public function reverseTransform($value)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
}
$formatter = $this->getNumberFormatter();
$value = $formatter->parse($value);
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
return $value;
}
示例4: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
$rb = new \ResourceBundle($locale, $resource);
if (!$rb) {
throw new \RuntimeException("cannot load this resource : {$resource}");
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
$catalogue->addResource(new FileResource($resource . '.dat'));
return $catalogue;
}
示例5: reverseTransform
/**
* Transforms a localized number into an integer or float
*
* @param string $value
*/
public function reverseTransform($value)
{
if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}
if ('' === $value) {
return null;
}
$formatter = $this->getNumberFormatter();
$value = $formatter->parse($value);
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
return $value;
}
示例6: 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)]);
}
}
示例7: reverseTransform
/**
* Transforms between a percentage value into a normalized format (integer or float).
*
* @param number $value Percentage value.
* @return number Normalized value.
*/
public function reverseTransform($value)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
}
$formatter = $this->getNumberFormatter();
// replace normal spaces so that the formatter can read them
$value = $formatter->parse(str_replace(' ', ' ', $value));
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
if (self::FRACTIONAL == $this->getOption('type')) {
$value /= 100;
}
return $value;
}
示例8: reverseTransform
/**
* {@inheritdoc}
*/
public function reverseTransform($value)
{
if (!is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}
if ('' === $value) {
return;
}
if ('NaN' === $value) {
throw new TransformationFailedException('"NaN" is not a valid integer');
}
$formatter = $this->getNumberFormatter();
$value = $formatter->parse($value, PHP_INT_SIZE == 8 ? $formatter::TYPE_INT64 : $formatter::TYPE_INT32);
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
return $value;
}
示例9: reverseTransform
/**
* Transforms between a percentage value into a normalized format (integer or float).
*
* @param number $value Percentage value.
* @return number Normalized value.
*/
public function reverseTransform($value)
{
if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}
if ('' === $value) {
return null;
}
$formatter = $this->getNumberFormatter();
// replace normal spaces so that the formatter can read them
$value = $formatter->parse(str_replace(' ', ' ', $value));
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
if (self::FRACTIONAL == $this->getOption('type')) {
$value /= 100;
}
return $value;
}
示例10: transform
/**
* @inheritdoc
*/
public function transform($value)
{
if (!is_string($value) && !is_numeric($value)) {
throw new TransformationFailedException(sprintf('Expected a string to transform, got "%s" instead.', json_encode($value)));
}
if ('' === $value) {
return null;
}
if ('NaN' === $value) {
throw new TransformationFailedException('"NaN" is not a valid number');
}
$position = 0;
$formatter = $this->getNumberFormatter();
$groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
$decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
if ('.' !== $decSep && (!$this->grouping || '.' !== $groupSep)) {
$value = str_replace('.', $decSep, $value);
}
if (',' !== $decSep && (!$this->grouping || ',' !== $groupSep)) {
$value = str_replace(',', $decSep, $value);
}
$result = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE, $position);
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
if ($result >= PHP_INT_MAX || $result <= -PHP_INT_MAX) {
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
}
$encoding = mb_detect_encoding($value);
$length = mb_strlen($value, $encoding);
// After parsing, position holds the index of the character where the parsing stopped
if ($position < $length) {
// Check if there are unrecognized characters at the end of the
// number (excluding whitespace characters)
$remainder = trim(mb_substr($value, $position, $length, $encoding), " \t\n\r\v ");
if ('' !== $remainder) {
throw new TransformationFailedException(sprintf('The number contains unrecognized characters: "%s"', $remainder));
}
}
// Only the format() method in the NumberFormatter rounds, whereas parse() does not
return $this->round($result);
}
示例11: 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;
}
示例12: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource . '.dat')) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource . '.dat')) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$rb = new \ResourceBundle($locale, $resource);
if (!$rb) {
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
$catalogue->addResource(new FileResource($resource . '.dat'));
return $catalogue;
}
示例13: format
/**
* {@inheritdoc}
*/
public function format($value, array $options = array())
{
if (null === $value) {
return $options['null_value'];
}
if (!$value instanceof \DateTime) {
throw FormatterException::invalidType($this, $value, 'DateTime instance');
}
$dateTime = clone $value;
if ('UTC' !== $options['time_zone']) {
$dateTime->setTimezone(new \DateTimeZone('UTC'));
}
$formatter = new \IntlDateFormatter(\Locale::getDefault(), $options['date_format'], $options['time_format'], $options['time_zone'], $options['calendar'], $options['pattern']);
$formatter->setLenient(false);
$value = $formatter->format((int) $dateTime->format('U'));
if (intl_is_failure(intl_get_error_code())) {
throw FormatterException::intlError($this, intl_get_error_message());
}
$value = preg_replace('~GMT\\+00:00$~', 'GMT', $value);
return $value;
}
示例14: 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)]);
}
}
示例15: transform
/**
* Transforms a normalized format into a localized money string.
*
* @param MoneyValue $value Normalized number
*
* @throws TransformationFailedException If the given value is not numeric or
* if the value can not be transformed
*
* @return string Localized money string
*/
public function transform($value)
{
if (null === $value) {
return '';
}
if (!$value instanceof MoneyValue) {
throw new TransformationFailedException('Expected a MoneyValue object.');
}
if (!is_numeric($value->value)) {
throw new TransformationFailedException('Expected a numeric value.');
}
$amountValue = $value->value;
$amountValue /= $this->divisor;
$formatter = $this->getNumberFormatter();
$value = $formatter->formatCurrency($amountValue, $value->currency);
if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
}
// Convert fixed spaces to normal ones
$value = str_replace(" ", ' ', $value);
return $value;
}