diff options
author | friendica <info@friendica.com> | 2014-12-30 15:25:06 -0800 |
---|---|---|
committer | friendica <info@friendica.com> | 2014-12-30 15:25:06 -0800 |
commit | abd345f80f5074d325fd137e48a23925eaa66802 (patch) | |
tree | c5a469a001b2eb9ae5482b93b557a42e3181208f /library/intl/src/NumberFormat | |
parent | 2f650b74d35335815c48537cd03c81cc92a3c4f8 (diff) | |
parent | ae9d08267c632cae36a4ebd34c2077fd0051e0e7 (diff) | |
download | volse-hubzilla-abd345f80f5074d325fd137e48a23925eaa66802.tar.gz volse-hubzilla-abd345f80f5074d325fd137e48a23925eaa66802.tar.bz2 volse-hubzilla-abd345f80f5074d325fd137e48a23925eaa66802.zip |
Merge branch 'master' into trinidad
Diffstat (limited to 'library/intl/src/NumberFormat')
4 files changed, 560 insertions, 0 deletions
diff --git a/library/intl/src/NumberFormat/NumberFormat.php b/library/intl/src/NumberFormat/NumberFormat.php new file mode 100644 index 000000000..0c512b7ab --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormat.php @@ -0,0 +1,269 @@ +<?php + +namespace CommerceGuys\Intl\NumberFormat; + +class NumberFormat implements NumberFormatInterface +{ + /** + * The locale (i.e. "en_US"). + * + * @var string + */ + protected $locale; + + /** + * The numbering system. + * + * @var string + */ + protected $numberingSystem = array(); + + /** + * The decimal separator. + * + * @var string + */ + protected $decimalSeparator = array(); + + /** + * The grouping separator. + * + * @var string + */ + protected $groupingSeparator = array(); + + /** + * The plus sign. + * + * @var string + */ + protected $plusSign = array(); + + /** + * The number symbols. + * + * @var string + */ + protected $minusSign = array(); + + /** + * The percent sign. + * + * @var string + */ + protected $percentSign = array(); + + /** + * The number pattern used to format decimal numbers. + * + * @var string + */ + protected $decimalPattern; + + /** + * The number pattern used to format percentages. + * + * @var string + */ + protected $percentPattern; + + /** + * The number pattern used to format currency amounts. + * + * @var string + */ + protected $currencyPattern; + + /** + * The number pattern used to format accounting currency amounts. + * + * @var string + */ + protected $accountingCurrencyPattern; + + /** + * {@inheritdoc} + */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritdoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNumberingSystem() + { + return $this->numberingSystem; + } + + /** + * {@inheritdoc} + */ + public function setNumberingSystem($numberingSystem) + { + $this->numberingSystem = $numberingSystem; + } + + /** + * {@inheritdoc} + */ + public function getDecimalSeparator() + { + return $this->decimalSeparator; + } + + /** + * {@inheritdoc} + */ + public function setDecimalSeparator($decimalSeparator) + { + $this->decimalSeparator = $decimalSeparator; + } + + /** + * {@inheritdoc} + */ + public function getGroupingSeparator() + { + return $this->groupingSeparator; + } + + /** + * {@inheritdoc} + */ + public function setGroupingSeparator($groupingSeparator) + { + $this->groupingSeparator = $groupingSeparator; + } + + /** + * {@inheritdoc} + */ + public function getPlusSign() + { + return $this->plusSign; + } + + /** + * {@inheritdoc} + */ + public function setPlusSign($plusSign) + { + $this->plusSign = $plusSign; + } + + /** + * {@inheritdoc} + */ + public function getMinusSign() + { + return $this->minusSign; + } + + /** + * {@inheritdoc} + */ + public function setMinusSign($minusSign) + { + $this->minusSign = $minusSign; + } + + /** + * {@inheritdoc} + */ + public function getPercentSign() + { + return $this->percentSign; + } + + /** + * {@inheritdoc} + */ + public function setPercentSign($percentSign) + { + $this->percentSign = $percentSign; + } + + /** + * {@inheritdoc} + */ + public function getDecimalPattern() + { + return $this->decimalPattern; + } + + /** + * {@inheritdoc} + */ + public function setDecimalPattern($decimalPattern) + { + $this->decimalPattern = $decimalPattern; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPercentPattern() + { + return $this->percentPattern; + } + + /** + * {@inheritdoc} + */ + public function setPercentPattern($percentPattern) + { + $this->percentPattern = $percentPattern; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCurrencyPattern() + { + return $this->currencyPattern; + } + + /** + * {@inheritdoc} + */ + public function setCurrencyPattern($currencyPattern) + { + $this->currencyPattern = $currencyPattern; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getAccountingCurrencyPattern() + { + return $this->accountingCurrencyPattern; + } + + /** + * {@inheritdoc} + */ + public function setAccountingCurrencyPattern($accountingCurrencyPattern) + { + $this->accountingCurrencyPattern = $accountingCurrencyPattern; + + return $this; + } +} diff --git a/library/intl/src/NumberFormat/NumberFormatInterface.php b/library/intl/src/NumberFormat/NumberFormatInterface.php new file mode 100644 index 000000000..fa382df70 --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormatInterface.php @@ -0,0 +1,185 @@ +<?php + +namespace CommerceGuys\Intl\NumberFormat; + +interface NumberFormatInterface +{ + // Arabic-Indic digits. + const NUMBERING_SYSTEM_ARABIC = 'arab'; + // Extended Arabic-Indic digits. + const NUMBERING_SYSTEM_ARABIC_EXTENDED = 'arabext'; + // Bengali digits. + const NUMBERING_SYSTEM_BENGALI = 'beng'; + // Devanagari digits. + const NUMBERING_SYSTEM_DEVANAGARI = 'deva'; + // Latin digits + const NUMBERING_SYSTEM_LATIN = 'latn'; + + /** + * Gets the locale. + * + * @return string + */ + public function getLocale(); + + /** + * Sets the locale. + * + * @param string $locale The locale (i.e. "en_US"). + */ + public function setLocale($locale); + + /** + * Gets the numbering system. + * + * The value is one of the NUMBERING_SYSTEM_ constants. + * + * @return string + */ + public function getNumberingSystem(); + + /** + * Sets the numbering system. + * + * @param string $numberingSystem One of the NUMBERING_SYSTEM_ constants. + */ + public function setNumberingSystem($numberingSystem); + + /** + * Gets the decimal separator. + * + * @return string + */ + public function getDecimalSeparator(); + + /** + * Sets the decimal separator. + * + * @var string $decimalSeparator + */ + public function setDecimalSeparator($decimalSeparator); + + /** + * Gets the grouping separator. + * + * @return string + */ + public function getGroupingSeparator(); + + /** + * Sets the grouping separator. + * + * @var string $groupingSeparator + */ + public function setGroupingSeparator($groupingSeparator); + + /** + * Gets the plus sign. + * + * @return string + */ + public function getPlusSign(); + + /** + * Sets the plus sign. + * + * @var string $plusSign + */ + public function setPlusSign($plusSign); + + /** + * Gets the minus sign. + * + * @return string + */ + public function getMinusSign(); + + /** + * Sets the minus sign. + * + * @var string $minusSign + */ + public function setMinusSign($minusSign); + + /** + * Gets the percent sign. + * + * @return string + */ + public function getPercentSign(); + + /** + * Sets the percent sign. + * + * @var string $percentSign + */ + public function setPercentSign($percentSign); + + /** + * Gets the number pattern used to format decimal numbers. + * + * @return string + * + * @see http://cldr.unicode.org/translation/number-patterns + */ + public function getDecimalPattern(); + + /** + * Sets the number pattern used to format decimal numbers. + * + * @param string $decimalPattern The decimal pattern. + */ + public function setDecimalPattern($decimalPattern); + + /** + * Gets the number pattern used to format percentages. + * + * @return string + * + * @see http://cldr.unicode.org/translation/number-patterns + */ + public function getPercentPattern(); + + /** + * Sets the number pattern used to format percentages. + * + * @param string $percentPattern The percent pattern. + */ + public function setPercentPattern($percentPattern); + + /** + * Gets the number pattern used to format currency amounts. + * + * @return string + * + * @see http://cldr.unicode.org/translation/number-patterns + */ + public function getCurrencyPattern(); + + /** + * Sets the number pattern used to format currency amounts. + * + * @param string $currencyPattern The currency pattern. + */ + public function setCurrencyPattern($currencyPattern); + + /** + * Gets the number pattern used to format accounting currency amounts. + * + * Most commonly used when formatting amounts on invoices. + * + * @return string + * + * @see http://cldr.unicode.org/translation/number-patterns + */ + public function getAccountingCurrencyPattern(); + + /** + * Sets the number pattern used to format accounting currency amounts. + * + * Most commonly used when formatting amounts on invoices. + * + * @param string $accountingCurrencyPattern The accounting currency pattern. + */ + public function setAccountingCurrencyPattern($accountingCurrencyPattern); +} diff --git a/library/intl/src/NumberFormat/NumberFormatRepository.php b/library/intl/src/NumberFormat/NumberFormatRepository.php new file mode 100644 index 000000000..29a597906 --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormatRepository.php @@ -0,0 +1,87 @@ +<?php + +namespace CommerceGuys\Intl\NumberFormat; + +use CommerceGuys\Intl\LocaleResolverTrait; + +/** + * Repository for number formats based on JSON definitions. + */ +class NumberFormatRepository implements NumberFormatRepositoryInterface +{ + use LocaleResolverTrait; + + /** + * Number format definitions. + * + * @var array + */ + protected $definitions = array(); + + /** + * Creates a NumberFormatRepository instance. + * + * @param string $definitionPath The path to the number format definitions. + * Defaults to 'resources/number_format'. + */ + public function __construct($definitionPath = null) + { + $this->definitionPath = $definitionPath ? $definitionPath : __DIR__ . '/../../resources/number_format/'; + } + + /** + * {@inheritdoc} + */ + public function get($locale, $fallbackLocale = null) + { + $locale = $this->resolveLocale($locale, $fallbackLocale); + if (!isset($this->definitions[$locale])) { + $filename = $this->definitionPath . $locale . '.json'; + $this->definitions[$locale] = json_decode(file_get_contents($filename), true); + } + + return $this->createNumberFormatFromDefinition($this->definitions[$locale], $locale); + } + + /** + * Creates a number format object from the provided definition. + * + * @param array $definition The number format definition. + * @param string $locale The locale of the number format definition. + * + * @return NumberFormat + */ + protected function createNumberFormatFromDefinition(array $definition, $locale) + { + if (!isset($definition['decimal_separator'])) { + $definition['decimal_separator'] = '.'; + } + if (!isset($definition['grouping_separator'])) { + $definition['grouping_separator'] = ','; + } + if (!isset($definition['plus_sign'])) { + $definition['plus_sign'] = '+'; + } + if (!isset($definition['minus_sign'])) { + $definition['minus_sign'] = '-'; + } + if (!isset($definition['percent_sign'])) { + $definition['percent_sign'] = '%'; + } + + $numberFormat = new NumberFormat(); + $numberFormat->setLocale($locale); + $numberFormat->setNumberingSystem($definition['numbering_system']); + $numberFormat->setDecimalSeparator($definition['decimal_separator']); + $numberFormat->setGroupingSeparator($definition['grouping_separator']); + $numberFormat->setPlusSign($definition['plus_sign']); + $numberFormat->setMinusSign($definition['minus_sign']); + $numberFormat->setPercentSign($definition['percent_sign']); + $numberFormat->setDecimalPattern($definition['decimal_pattern']); + $numberFormat->setPercentPattern($definition['percent_pattern']); + $numberFormat->setCurrencyPattern($definition['currency_pattern']); + $numberFormat->setAccountingCurrencyPattern($definition['accounting_currency_pattern']); + + return $numberFormat; + } +} diff --git a/library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php b/library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php new file mode 100644 index 000000000..ff162b522 --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php @@ -0,0 +1,19 @@ +<?php + +namespace CommerceGuys\Intl\NumberFormat; + +/** + * Number format repository interface. + */ +interface NumberFormatRepositoryInterface +{ + /** + * Returns a number format instance for the provided locale. + * + * @param string $locale The locale (i.e. fr-FR). + * @param string $fallbackLocale A fallback locale (i.e "en"). + * + * @return NumberFormatInterface + */ + public function get($locale, $fallbackLocale = null); +} |