<?php
namespace Engine\Smarty\Modifiers;

class NumberFormat
{
	/**
	 * Format a number. If called without params, it will use the app-default settings
	 */
	static public function create($number, $decimals=null, $dec_point=null, $thousands_sep=null)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		if ($decimals===null && $dec_point===null && $thousands_sep===null) {
			if ($CFG['localization']['numberFormat']=='eu') {
				return number_format((Double)$number, 2, ',', '.');
			} elseif ($CFG['localization']['numberFormat']=='us') {
				return number_format((Double)$number, 2, '.', ',');
			} else { // includes 'plain'
				return number_format((Double)$number, 2, '.', '');
			}
		}

		if ($CFG['localization']['numberFormat']=='eu') {
			$decimals = 		($decimals===null?       2:$decimals);
			$dec_point = 		($dec_point===null?    ',':$dec_point);
			$thousands_sep = 	($thousands_sep===null?'.':$thousands_sep);
		}
		elseif ($CFG['localization']['numberFormat']=='us') {
			$decimals = 		($decimals===null?       2:$decimals);
			$dec_point = 		($dec_point===null?    '.':$dec_point);
			$thousands_sep = 	($thousands_sep===null?',':$thousands_sep);
		}
		else {
			$decimals = 		($decimals===null?       2:$decimals);
			$dec_point = 		($dec_point===null?    '.':$dec_point);
			$thousands_sep = 	($thousands_sep===null? '':$thousands_sep);
		}

		return number_format((float)$number, $decimals, $dec_point, $thousands_sep);
	}
}
