/**
* The required files
*/
require_once(HTML_BASE_COMMON_PATH.'/Object.php');
if (defined('HTML_LANGUAGE_UTIL_PATH')) {
require_once(HTML_LANGUAGE_UTIL_PATH.'/Locale.php');
require_once(HTML_LANGUAGE_UTIL_PATH.'/Currency.php');
}
/**
* Format the text accordingly to predefined styles
* <code>
* Usage:
* $text = '20081103142103';
* $style = FORMAT_STYLE_DATE_LONG;
* $html = new Format($text,$style,$pattern,$locale);
* print $html->getHtml(); // 2008-11-03 14:21:03
* OR
* Format::display($text,$style,$pattern,$locale);
* OR
* $newtext = Format::get($text,$style,$pattern,$locale);
* </code>
* @package util
* @todo use number_format() with locale
*/
class Format extends Object {
/**
* @var String $text The text to format
*/
var $text = '';
/**
* @see FORMAT_STYLE_TEXT
* @var String $style The style to use
*/
var $style = '';
/**
* @see FORMAT_PATTERN_HEX
* @var String $pattern The pattern to use
*/
var $pattern = '';
/**
* @var Locale $locale The locale object to use
*/
var $locale = '';
/**
* Constructor
* @param String $text The text to format accordingly
* @param String $style The format style
* @param String $pattern The pattern to use
* @param Locale $locale The Locale object to use
*/
function Format($text,$style='',$pattern='',$locale='') {
$this->text = $text;
$this->style = $style!=''?$style:FORMAT_STYLE_TEXT;
$this->pattern = $pattern!=''?$pattern:'';
if ($this->pattern=='') {
switch ($this->style) {
case FORMAT_STYLE_HEX:
$this->pattern = FORMAT_PATTERN_HEX;
break;
case FORMAT_STYLE_BIN:
$this->pattern = FORMAT_PATTERN_BIN;
break;
default:
// Ignore
break;
}
}
if ($locale!='') {
if (is_object($locale) && is_a($locale,CLASS_NAME_LOCALE)) {
$this->locale = $locale;
} else {
die($this->getClassName()." Unknown class name, expected Locale, found=".$locale);
}
}
if ($this->locale=='') {
if (defined('HTML_LANGUAGE_UTIL_PATH')) {
$this->locale = new Locale();
}
}
}
/**
* Returns the text as formatted html.
* The Locale object is used to decide how the decimal and thousand seperators lool like
* @return String The text formatted as specified by style
*/
function convert() {
$html = '';
/**
* Assume that the en/us number notation is used
*/
$dec_point = '.';
$thousands_sep = ',';
$tmz = '+0100'; // TODO how ???
if ($this->locale!='' && is_a($this->locale,CLASS_NAME_LOCALE)) {
switch ($this->locale->getLanguage()) {
case LANGUAGE_DA:
case LANGUAGE_NO:
case LANGUAGE_SE:
$dec_point = ',';
$thousands_sep = '.';
$tmz = '+0100'; // TODO how ???
break;
}
}
switch ($this->style) {
case FORMAT_STYLE_DATE_RSS: // @todo And yes, I know about gmt+1 => +0100
$html .= date("D, d M Y H:i:s $tmz",strtotime($this->text));
break;
case FORMAT_STYLE_DATE_MYSQL:
$html .= date('YmdHis',strtotime($this->text));
break;
case FORMAT_STYLE_DATE_LONG:
$date = date('Y-m-d H:i:s',strtotime($this->text));
$html .= str_replace(' '," ",$date); // In order to avoid line breaks
break;
case FORMAT_STYLE_DATE_SHORT:
case FORMAT_STYLE_DATE:
$html .= date('d-M-Y',strtotime($this->text));
break;
case FORMAT_STYLE_TIME:
$html .= date('H:i:s',strtotime($this->text));
break;
case FORMAT_STYLE_HEX:
$hex = strtoupper(dechex($this->text)); // Translate from dec to hex
$html .= '0x'.substr($this->pattern,0,strlen($this->pattern)-strlen($hex)).$hex;
break;
case FORMAT_STYLE_BIN:
$bin = decbin($this->text); // Translate from dec to bin
$html .= substr($this->pattern,0,strlen($this->pattern)-strlen($bin)).$bin;
break;
case FORMAT_STYLE_MONEY_SIMPLE:
case FORMAT_STYLE_DKK_SIMPLE:
$number = ($this->text*100)/100;
$html .= number_format($number,2,$dec_point,''); // 2062,50
$html = str_replace('-',HTML_ENTITY_MINUS,$html); // In order to avoid line breaks
$html = str_replace(' '," ",$html); // In order to avoid line breaks
break;
case FORMAT_STYLE_MONEY_SHORT:
case FORMAT_STYLE_DKK_SHORT:
$number = ($this->text*100)/100;
$html .= number_format($number,2,$dec_point,$thousands_sep); // 2.062,50
$html = str_replace('-',HTML_ENTITY_MINUS,$html); // In order to avoid line breaks
$html = str_replace(' '," ",$html); // In order to avoid line breaks
break;
case FORMAT_STYLE_MONEY_LONG:
case FORMAT_STYLE_DKK_LONG:
$number = ($this->text*100)/100;
$currencyText = '';
if ($this->locale!='' && is_a($this->locale,CLASS_NAME_LOCALE)) {
$currency = new Currency($this->locale);
$amount = $currency->calculate($number);
$calculated = number_format($amount,2,$dec_point,$thousands_sep); // EUR 2.062,50
$currencyText .= '('.$currency->getHtml().' '.$calculated.')';
}
$original = number_format($number,2,$dec_point,$thousands_sep); // DKK 2.062,50
$html .= $currencyText.' DKK '.$original;
$html = str_replace('-',HTML_ENTITY_MINUS,$html); // In order to avoid line breaks
$html = str_replace(' '," ",$html); // In order to avoid line breaks
break;
case FORMAT_STYLE_LEFT:
case FORMAT_STYLE_CENTER:
case FORMAT_STYLE_RIGHT:
$html .= '<div class="'.$this->style.'">'.$this->text."</div>\r\n"; // Text align
break;
default:
$html .= $this->text; // Raw
break;
}
return $html;
}
/**
* Returns the text as formatted html
* @return String The text formatted as specified by style
*/
function getHtml() {
$html = $this->html;
$html .= $this->convert();
return $html;
}
/**
* Get the text as formatted html.
* The Locale object is used to decide how the decimal and thousand seperators lool like
* <code>
* Usage:
* $text = '20081103142103';
* $style = FORMAT_STYLE_DATE_LONG;
* $newtext = Format::get($text,$style,$pattern,$locale);
* print $newtext; // 2008-11-03 14:21:03
* </code>
* @static
* @param String $text The text to format accordingly to style
* @param String $style The format style
* @param String $pattern The pattern to use
* @param Locale $locale The Locale object to use
* @return String The text formatted as specified by style
*/
function get($text,$style='',$pattern='',$locale='') {
$format = new Format($text,$style,$pattern,$locale);
return $format->getHtml();
}
/**
* Display html
* <code>
* Usage:
* $text = '20081103142103';
* $style = FORMAT_STYLE_DATE_LONG;
* Format::display($text,$style,$pattern,$locale);
* </code>
* @static
* @param String $text The text to format accordingly to style
* @param String $style The format style
* @param String $pattern The pattern to use
* @param Locale $locale The Locale object to use
*/
function display($text,$style='',$pattern='',$locale='') {
$html = new Format($text,$style,$pattern,$locale);
$html->addHtml();
}
}
?>