1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\CLDR;
/**
* Formats currencies using locale conventions.
*
* @property-read CurrencyFormatter $target
*/
class LocalizedCurrencyFormatter extends LocalizedObject implements Formatter
{
const PATTERN_STANDARD = 'standard';
const PATTERN_ACCOUNTING = 'accounting';
/**
* Formats currency using localized conventions.
*
* @param number $number
* @param Currency|string $currency
* @param string $pattern
* @param array $symbols
*
* @return string
*/
public function __invoke($number, $currency, $pattern = self::PATTERN_STANDARD, array $symbols = [])
{
return $this->format($number, $currency, $pattern, $symbols);
}
/**
* Formats currency using localized conventions.
*
* @param number $number
* @param Currency|string $currency
* @param string $pattern
* @param array $symbols
*
* @return string
*/
public function format($number, $currency, $pattern = self::PATTERN_STANDARD, array $symbols = [])
{
$symbols += $this->locale->numbers->symbols + [
'currencySymbol' => $this->resolve_currency_symbol($currency)
];
return $this->target->format($number, $this->resolve_pattern($pattern), $symbols);
}
/**
* @param Currency|string $currency
*
* @return string
*/
private function resolve_currency_symbol($currency)
{
return $this->locale['currencies'][(string) $currency]['symbol'];
}
/**
* Resolves a pattern.
*
* The special patterns {@link PATTERN_STANDARD} and {@link PATTERN_ACCOUNTING} are resolved
* from the currency formats.
*
* @param string $pattern
*
* @return string
*/
private function resolve_pattern($pattern)
{
switch ($pattern)
{
case self::PATTERN_STANDARD:
return $this->locale->numbers->currency_formats['standard'];
case self::PATTERN_ACCOUNTING:
return $this->locale->numbers->currency_formats['accounting'];
}
return $pattern;
}
}