-
-
Notifications
You must be signed in to change notification settings - Fork 272
Added Number extension #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Twig. | ||
* | ||
* (c) 2009 Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Tim Nagel <tim@nagel.com.au> | ||
* @package Twig | ||
* @subpackage Twig-extensions | ||
*/ | ||
class Twig_Extensions_Extension_Number extends Twig_Extension | ||
{ | ||
/** | ||
* Returns a list of filters. | ||
* | ||
* @return array | ||
*/ | ||
public function getFilters() | ||
{ | ||
return array( | ||
'currency' => new Twig_Filter_Function('twig_format_currency'), | ||
); | ||
} | ||
|
||
/** | ||
* Name of this extension | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'Number'; | ||
} | ||
} | ||
|
||
if (class_exists('NumberFormatter')) { | ||
function twig_format_currency($value, $currency, $locale = null) | ||
{ | ||
static $formatter; | ||
|
||
if (null === $locale) { | ||
$locale = Locale::getDefault(); | ||
} | ||
|
||
if (null === $formatter) { | ||
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should not use a static formatter as it will fail when using the method several times with different locales. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually you can use a static but compare the locale of the object with the given one (that is how I do it).
|
||
} | ||
|
||
return $formatter->formatCurrency($value, $currency); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about making the locale configurable with a third parameter (and using the default when it is not given) ? |
||
} | ||
} else { | ||
function twig_format_currency($value, $currency = null, $locale = null) | ||
{ | ||
if (null !== $currency) { | ||
throw new \LogicException("You must have Intl enabled to specify a currency. Pass null if you\'re trying to set the locale."); | ||
} | ||
|
||
if (null !== $locale) { | ||
setlocale(LC_MONETARY, $locale); | ||
} | ||
|
||
return money_format("%i", $value); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part should be removed in favor of #55 IMO