function testPadding() { $formatter = new NumberFormat(); $number = '5'; $pattern = '0000'; $wanted = '0005'; //this should fail!!! $this->assertEqual($wanted, $formatter->format($number, $pattern)); }
public function setProfil($options) { if ($options instanceof NumberFormat) { $this->other["profil"] = $options; } else { $profil = new NumberFormat(); if (is_array($options) && $options !== array()) { foreach ($options as $key => $value) { $f = 'set' . ucfirst($key); $profil->{$f}($value); } } else { $profil->setSymbol($this->code); } $this->other["profil"] = $profil; } return $this; }
/** * Constructor * * @param string f default NULL format string */ public function __construct($f = NULL) { // Add some default formatters $this->setFormatter('printf', PrintfFormat::getInstance()); $this->setFormatter('date', DateFormat::getInstance()); $this->setFormatter('choice', ChoiceFormat::getInstance()); $this->setFormatter('number', NumberFormat::getInstance()); $this->setFormatter('array', ArrayFormat::getInstance()); $this->setFormatter('hash', HashFormat::getInstance()); parent::__construct($f); }
/** * Formats a phone number in the specified format using client-defined formatting rules. Note that * if the phone number has a country calling code of zero or an otherwise invalid country calling * code, we cannot work out things like whether there should be a national prefix applied, or how * to format extensions, so we return the national significant number with no formatting applied. * * @param PhoneNumber $number the phone number to be formatted * @param int $numberFormat the format the phone number should be formatted into * @param array $userDefinedFormats formatting rules specified by clients * @return String the formatted phone number */ public function formatByPattern(PhoneNumber $number, $numberFormat, array $userDefinedFormats) { $countryCallingCode = $number->getCountryCode(); $nationalSignificantNumber = $this->getNationalSignificantNumber($number); if (!$this->hasValidCountryCallingCode($countryCallingCode)) { return $nationalSignificantNumber; } // Note getRegionCodeForCountryCode() is used because formatting information for regions which // share a country calling code is contained by only one region for performance reasons. For // example, for NANPA regions it will be contained in the metadata for US. $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode); // Metadata cannot be null because the country calling code is valid $metadata = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode); $formattedNumber = ""; $formattingPattern = $this->chooseFormattingPatternForNumber($userDefinedFormats, $nationalSignificantNumber); if ($formattingPattern === null) { // If no pattern above is matched, we format the number as a whole. $formattedNumber .= $nationalSignificantNumber; } else { $numFormatCopy = new NumberFormat(); // Before we do a replacement of the national prefix pattern $NP with the national prefix, we // need to copy the rule so that subsequent replacements for different numbers have the // appropriate national prefix. $numFormatCopy->mergeFrom($formattingPattern); $nationalPrefixFormattingRule = $formattingPattern->getNationalPrefixFormattingRule(); if (mb_strlen($nationalPrefixFormattingRule) > 0) { $nationalPrefix = $metadata->getNationalPrefix(); if (mb_strlen($nationalPrefix) > 0) { // Replace $NP with national prefix and $FG with the first group ($1). $npPatternMatcher = new Matcher(self::NP_PATTERN, $nationalPrefixFormattingRule); $nationalPrefixFormattingRule = $npPatternMatcher->replaceFirst($nationalPrefix); $fgPatternMatcher = new Matcher(self::FG_PATTERN, $nationalPrefixFormattingRule); $nationalPrefixFormattingRule = $fgPatternMatcher->replaceFirst("\\\$1"); $numFormatCopy->setNationalPrefixFormattingRule($nationalPrefixFormattingRule); } else { // We don't want to have a rule for how to format the national prefix if there isn't one. $numFormatCopy->clearNationalPrefixFormattingRule(); } } $formattedNumber .= $this->formatNsnUsingPattern($nationalSignificantNumber, $numFormatCopy, $numberFormat); } $this->maybeAppendFormattedExtension($number, $metadata, $numberFormat, $formattedNumber); $this->prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, $formattedNumber); return $formattedNumber; }
public function testFormatByPattern() { $newNumFormat = new NumberFormat(); $newNumFormat->setPattern("(\\d{3})(\\d{3})(\\d{4})"); $newNumFormat->setFormat("(\$1) \$2-\$3"); $newNumberFormats = array(); $newNumberFormats[] = $newNumFormat; $this->assertEquals("(650) 253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats)); $this->assertEquals("+1 (650) 253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats)); $this->assertEquals("+1-650-253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::RFC3966, $newNumberFormats)); // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are // followed. $newNumFormat->setNationalPrefixFormattingRule('$NP ($FG)'); $newNumFormat->setFormat("\$1 \$2-\$3"); $this->assertEquals("1 (242) 365-1234", $this->phoneUtil->formatByPattern(self::$bsNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats)); $this->assertEquals("+1 242 365-1234", $this->phoneUtil->formatByPattern(self::$bsNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats)); $newNumFormat->setPattern("(\\d{2})(\\d{5})(\\d{3})"); $newNumFormat->setFormat("\$1-\$2 \$3"); $newNumberFormats[0] = $newNumFormat; $this->assertEquals("02-36618 300", $this->phoneUtil->formatByPattern(self::$itNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats)); $this->assertEquals("+39 02-36618 300", $this->phoneUtil->formatByPattern(self::$itNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats)); $newNumFormat->setNationalPrefixFormattingRule('$NP$FG'); $newNumFormat->setPattern("(\\d{2})(\\d{4})(\\d{4})"); $newNumFormat->setFormat("\$1 \$2 \$3"); $newNumberFormats[0] = $newNumFormat; $this->assertEquals("020 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats)); $newNumFormat->setNationalPrefixFormattingRule('($NP$FG)'); $this->assertEquals("(020) 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats)); $newNumFormat->setNationalPrefixFormattingRule(""); $this->assertEquals("20 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats)); $this->assertEquals("+44 20 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats)); }
/** * @param NumberFormat $other * @return NumberFormat */ public function mergeFrom(NumberFormat $other) { if ($other->hasPattern()) { $this->setPattern($other->getPattern()); } if ($other->hasFormat()) { $this->setFormat($other->getFormat()); } $leadingDigitsPatternSize = $other->leadingDigitsPatternSize(); for ($i = 0; $i < $leadingDigitsPatternSize; $i++) { $this->addLeadingDigitsPattern($other->getLeadingDigitsPattern($i)); } if ($other->hasNationalPrefixFormattingRule()) { $this->setNationalPrefixFormattingRule($other->getNationalPrefixFormattingRule()); } if ($other->hasDomesticCarrierCodeFormattingRule()) { $this->setDomesticCarrierCodeFormattingRule($other->getDomesticCarrierCodeFormattingRule()); } $this->setNationalPrefixOptionalWhenFormatting($other->isNationalPrefixOptionalWhenFormatting()); return $this; }
/** * Renders the localized number, be it currency or decimal, or percentage. * If the culture is not specified, the default application * culture will be used. * This method overrides parent's implementation. */ protected function renderBody() { $app = $this->Application->getGlobalization(); //initialized the default class wide formatter if (is_null(self::$formatter)) { self::$formatter = new NumberFormat($app->Culture); } $culture = $this->getCulture(); $pattern = $this->getPattern(); $type = $this->getType(); if (empty($pattern)) { $pattern = $type; } $value = $this->getValue(); if (strlen($value) == 0) { $value = parent::renderBody(); } //return the specific cultural formatted number if (!empty($culture) && $app->Culture != $culture) { $formatter = new NumberFormat($culture); return $formatter->format($value, $pattern, $this->getCurrency()); } //return the application wide culture formatted number. return self::$formatter->format($value, $pattern, $this->getCurrency()); }
public function fromArray(array $input) { if (isset($input['generalDesc'])) { $desc = new PhoneNumberDesc(); $this->setGeneralDesc($desc->fromArray($input['generalDesc'])); } if (isset($input['fixedLine'])) { $desc = new PhoneNumberDesc(); $this->setFixedLine($desc->fromArray($input['fixedLine'])); } if (isset($input['mobile'])) { $desc = new PhoneNumberDesc(); $this->setMobile($desc->fromArray($input['mobile'])); } if (isset($input['tollFree'])) { $desc = new PhoneNumberDesc(); $this->setTollFree($desc->fromArray($input['tollFree'])); } if (isset($input['premiumRate'])) { $desc = new PhoneNumberDesc(); $this->setPremiumRate($desc->fromArray($input['premiumRate'])); } if (isset($input['sharedCost'])) { $desc = new PhoneNumberDesc(); $this->setSharedCost($desc->fromArray($input['sharedCost'])); } /* hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setPersonalNumber(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setVoip(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setPager(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setUan(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setVoicemail(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setEmergency(desc); } */ if (isset($input['noInternationalDialling'])) { $desc = new PhoneNumberDesc(); $this->setNoInternationalDialling($desc->fromArray($input['noInternationalDialling'])); } $this->setId($input['id']); $this->setCountryCode($input['countryCode']); $this->setInternationalPrefix($input['internationalPrefix']); if (isset($input['preferredInternationalPrefix'])) { $this->setPreferredInternationalPrefix($input['preferredInternationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['preferredExtnPrefix'])) { $this->setPreferredExtnPrefix($input['preferredExtnPrefix']); } if (isset($input['nationalPrefixForParsing'])) { $this->setNationalPrefixForParsing($input['nationalPrefixForParsing']); } if (isset($input['nationalPrefixTransformRule'])) { $this->setNationalPrefixTransformRule($input['nationalPrefixTransformRule']); } /* setSameMobileAndFixedLinePattern(objectInput.readBoolean()); */ foreach ($input['numberFormat'] as $numberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($numberFormatElt); $this->addNumberFormat($numberFormat); } foreach ($input['intlNumberFormat'] as $intlNumberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($intlNumberFormatElt); $this->addIntlNumberFormat($numberFormat); } /* setMainCountryForCode(objectInput.readBoolean()); * */ $this->setMainCountryForCode($input['mainCountryForCode']); if (isset($input['leadingDigits'])) { $this->setLeadingDigits($input['leadingDigits']); } $this->setLeadingZeroPossible($input['leadingZeroPossible']); return $this; }
public function formatNsnUsingPattern($nationalNumber, NumberFormat $formattingPattern, $numberFormat, $carrierCode = NULL) { $numberFormatRule = $formattingPattern->getFormat(); $m = new Matcher($formattingPattern->getPattern(), $nationalNumber); if ($numberFormat == PhoneNumberFormat::NATIONAL && $carrierCode !== NULL && strlen($carrierCode) > 0 && strlen($formattingPattern->getDomesticCarrierCodeFormattingRule()) > 0) { // Replace the $CC in the formatting rule with the desired carrier code. $carrierCodeFormattingRule = $formattingPattern->getDomesticCarrierCodeFormattingRule(); $ccPatternMatcher = new Matcher(self::CC_PATTERN, $carrierCodeFormattingRule); $carrierCodeFormattingRule = $ccPatternMatcher->replaceFirst($carrierCode); // Now replace the $FG in the formatting rule with the first group and the carrier code // combined in the appropriate way. $firstGroupMatcher = new Matcher(self::FIRST_GROUP_PATTERN, $numberFormatRule); $numberFormatRule = $firstGroupMatcher->replaceFirst($carrierCodeFormattingRule); $formattedNationalNumber = $m->replaceAll($numberFormatRule); } else { // Use the national prefix formatting rule instead. $nationalPrefixFormattingRule = $formattingPattern->getNationalPrefixFormattingRule(); if ($numberFormat == PhoneNumberFormat::NATIONAL && $nationalPrefixFormattingRule !== NULL && strlen($nationalPrefixFormattingRule) > 0) { $firstGroupMatcher = new Matcher(self::FIRST_GROUP_PATTERN, $numberFormatRule); $formattedNationalNumber = $m->replaceAll($firstGroupMatcher->replaceFirst($nationalPrefixFormattingRule)); } else { $formattedNationalNumber = $m->replaceAll($numberFormatRule); } } if ($numberFormat == PhoneNumberFormat::RFC3966) { // Strip any leading punctuation. $matcher = new Matcher(self::$SEPARATOR_PATTERN, $formattedNationalNumber); if ($matcher->lookingAt()) { $formattedNationalNumber = $matcher->replaceFirst(""); } // Replace the rest with a dash between each number group. $formattedNationalNumber = $matcher->reset($formattedNationalNumber)->replaceAll("-"); } return $formattedNationalNumber; }
/** * @param array $input * @return PhoneMetadata */ public function fromArray(array $input) { if (isset($input['generalDesc'])) { $desc = new PhoneNumberDesc(); $this->setGeneralDesc($desc->fromArray($input['generalDesc'])); } if (isset($input['fixedLine'])) { $desc = new PhoneNumberDesc(); $this->setFixedLine($desc->fromArray($input['fixedLine'])); } if (isset($input['mobile'])) { $desc = new PhoneNumberDesc(); $this->setMobile($desc->fromArray($input['mobile'])); } if (isset($input['tollFree'])) { $desc = new PhoneNumberDesc(); $this->setTollFree($desc->fromArray($input['tollFree'])); } if (isset($input['premiumRate'])) { $desc = new PhoneNumberDesc(); $this->setPremiumRate($desc->fromArray($input['premiumRate'])); } if (isset($input['sharedCost'])) { $desc = new PhoneNumberDesc(); $this->setSharedCost($desc->fromArray($input['sharedCost'])); } if (isset($input['personalNumber'])) { $desc = new PhoneNumberDesc(); $this->setPersonalNumber($desc->fromArray($input['personalNumber'])); } if (isset($input['voip'])) { $desc = new PhoneNumberDesc(); $this->setVoip($desc->fromArray($input['voip'])); } if (isset($input['pager'])) { $desc = new PhoneNumberDesc(); $this->setPager($desc->fromArray($input['pager'])); } if (isset($input['uan'])) { $desc = new PhoneNumberDesc(); $this->setUan($desc->fromArray($input['uan'])); } if (isset($input['emergency'])) { $desc = new PhoneNumberDesc(); $this->setEmergency($desc->fromArray($input['emergency'])); } if (isset($input['voicemail'])) { $desc = new PhoneNumberDesc(); $this->setVoicemail($desc->fromArray($input['voicemail'])); } if (isset($input['shortCode'])) { $desc = new PhoneNumberDesc(); $this->setShortCode($desc->fromArray($input['shortCode'])); } if (isset($input['standardRate'])) { $desc = new PhoneNumberDesc(); $this->setStandardRate($desc->fromArray($input['standardRate'])); } if (isset($input['carrierSpecific'])) { $desc = new PhoneNumberDesc(); $this->setCarrierSpecific($desc->fromArray($input['carrierSpecific'])); } if (isset($input['noInternationalDialling'])) { $desc = new PhoneNumberDesc(); $this->setNoInternationalDialling($desc->fromArray($input['noInternationalDialling'])); } $this->setId($input['id']); $this->setCountryCode($input['countryCode']); $this->setInternationalPrefix($input['internationalPrefix']); if (isset($input['preferredInternationalPrefix'])) { $this->setPreferredInternationalPrefix($input['preferredInternationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['preferredExtnPrefix'])) { $this->setPreferredExtnPrefix($input['preferredExtnPrefix']); } if (isset($input['nationalPrefixForParsing'])) { $this->setNationalPrefixForParsing($input['nationalPrefixForParsing']); } if (isset($input['nationalPrefixTransformRule'])) { $this->setNationalPrefixTransformRule($input['nationalPrefixTransformRule']); } foreach ($input['numberFormat'] as $numberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($numberFormatElt); $this->addNumberFormat($numberFormat); } foreach ($input['intlNumberFormat'] as $intlNumberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($intlNumberFormatElt); $this->addIntlNumberFormat($numberFormat); } $this->setMainCountryForCode($input['mainCountryForCode']); if (isset($input['leadingDigits'])) { $this->setLeadingDigits($input['leadingDigits']); } if (isset($input['leadingZeroPossible'])) { $this->setLeadingZeroPossible($input['leadingZeroPossible']); } if (isset($input['mobileNumberPortableRegion'])) { $this->setMobileNumberPortableRegion($input['mobileNumberPortableRegion']); } return $this; }
function testLocalizedCurrencyFormats2() { $it = new NumberFormat('it_IT'); $number = 12.41; $wanted = '12,41'; $this->assertEquals($wanted, $it->format($number, 'd')); $number = 10.23; $wanted = '10,23'; $this->assertEquals($wanted, $it->format($number, 'd')); $number = 10010.23; $wanted = '10.010,23'; $this->assertEquals($wanted, $it->format($number, 'd')); $old = setlocale(LC_ALL, "0"); setlocale(LC_ALL, "it_IT"); $number = 12.41; $wanted = '12,41'; $this->assertEquals($wanted, $it->format($number, 'd')); $number = 10.23; $wanted = '10,23'; $this->assertEquals($wanted, $it->format($number, 'd')); $number = 10010.23; $wanted = '10.010,23'; $this->assertEquals($wanted, $it->format($number, 'd')); setlocale(LC_ALL, $old); }
/** * Formats the localized number, be it currency or decimal, or percentage. * If the culture is not specified, the default application * culture will be used. * @return string formatted number */ protected function getFormattedValue() { $value = $this->getValue(); $defaultText = $this->getDefaultText(); if (empty($value) && !empty($defaultText)) { return $this->getDefaultText(); } $app = $this->getApplication()->getGlobalization(); //initialized the default class wide formatter if (self::$formatter === null) { self::$formatter = new NumberFormat($app->getCulture()); } $pattern = strlen($this->getPattern()) > 0 ? $this->getPattern() : $this->getType(); $culture = $this->getCulture(); //return the specific cultural formatted number if (!empty($culture) && $app->getCulture() != $culture) { $formatter = new NumberFormat($culture); return $formatter->format($this->getValue(), $pattern, $this->getCurrency(), $this->getCharset()); } //return the application wide culture formatted number. return self::$formatter->format($this->getValue(), $pattern, $this->getCurrency(), $this->getCharset()); }
public static function setLeadingDigitsPatterns(\DOMElement $numberFormatElement, NumberFormat $format) { $leadingDigitsPatternNodes = $numberFormatElement->getElementsByTagName(self::LEADING_DIGITS); $numOfLeadingDigitsPatterns = $leadingDigitsPatternNodes->length; if ($numOfLeadingDigitsPatterns > 0) { for ($i = 0; $i < $numOfLeadingDigitsPatterns; $i++) { $elt = $leadingDigitsPatternNodes->item($i); $format->addLeadingDigitsPattern($elt->firstChild->nodeValue, true); } } }