Sådan benyttes komponenten Format klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Format.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Format::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Format($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Format klassen
Den fulde PHP kildekode for Format klassen
<?php/** * @package util * @see HTML_UTIL_COMPONENT_PATH.'/Format.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * 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; * $pattern = ""; * $locale = ""; * $linebreak = true; // The linebreak is NOT allowed, if true * * $html = new Format($text, $style, $pattern, $locale, $linebreak); * print $html->getHtml(); // 2008-11-03 14:21:03 * OR * Format::display($text, $style, $pattern, $locale, $linebreak); * OR * $newtext = Format::get($text, $style, $pattern, $locale, $linebreak); * </code> * @package util * @todo use number_format() with locale */class Format extends Object { /** * @var String $text The text to format */ protected $text = ''; /** * @see FORMAT_STYLE_TEXT * @var String $style The style to use */ protected $style = ''; /** * @see FORMAT_PATTERN_HEX * @var String $pattern The pattern to use */ protected $pattern = ''; /** * @var Locale $locale The locale object to use */ protected $locale = ''; /** * @var boolean $linebreak The linebreak is NOT allowed, if true */ protected $linebreak = ''; /** * 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 * @param boolean $linebreak The linebreak is NOT allowed, if true */ function __construct($text, $style='', $pattern='', $locale='', $linebreak='') { parent::__construct(); $this->text = $text; $this->style = $style != '' ? $style : FORMAT_STYLE_TEXT; $this->pattern = $pattern != '' ? $pattern : ''; $this->linebreak = $linebreak !== '' ? $linebreak : false; 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 (is_object($locale) && $locale instanceof Locale ) { $this->locale = $locale; } else if ($this->locale === '') { if (defined('HTML_LANGUAGE_UTIL_PATH')) { $this->locale = new Locale(); } } } /** * Get the language from the Locale, if defined * @return String The language which could be 'fr' for France or 'da' for Denmark */ private function getLanguage() { $language = ""; if ($this->locale !== '' && $this->locale instanceof Locale ) { $language = $this->locale->getLanguage(); } return $language; } /** * Get the decimal point seperator, like in 1,234.00 which will return '.' for France * @param String $language The language in question, like 'da' for Denmark * @return string Return '.' or ',' depending of Locale language */ private function getDecimalPoint($language) { $dec_point = '.'; switch ($language) { case LANGUAGE_DA: case LANGUAGE_NO: case LANGUAGE_SE: $dec_point = ','; break; } return $dec_point; } /** * Get the thousands separator, like in 1,2345.00 which will return ',' for France * @param String $language The language in question, like 'da' for Denmark * @return string Return ',' or '.' depending of Locale language */ private function getThousandsSeparator($language) { $thousands_sep = ','; switch ($language) { case LANGUAGE_DA: case LANGUAGE_NO: case LANGUAGE_SE: $thousands_sep = '.'; break; } return $thousands_sep; } /** * Get the time zone, which will return '+0100' for Denmark * @param String $language The language in question, like 'da' for Denmark * @return string Return the time zone depending of Locale language */ private function getTimeZone($language) { $tmz = '+0100'; switch ($language) { case LANGUAGE_DA: case LANGUAGE_NO: case LANGUAGE_SE: $tmz = '+0100'; break; } return $tmz; } /** * Get the area code for a phone number, which will return '45' for Denmark * @param String $language The language in question, like 'da' for Denmark * @return string Return the area code depending of Locale language */ private function getAreaCode($language) { $areacode = '00'; switch ($language) { case LANGUAGE_DA: $areacode = '45'; break; case LANGUAGE_NO: case LANGUAGE_SE: break; } return $areacode; } /** * Get the formatted phone number, which will return 40506069 for Denmark for this phone number (+45) 40 50 60 69 * @param String $language The language in question, like 'da' for Denmark * @return string Return the formatted phone number depending of Locale language */ private function getFormattedPhoneNumer($areacode, $strip=true) { $phone = str_replace(' ','', $this->text); if ($strip) { $phone = str_replace("(+".$areacode.")", "", $phone); } return $phone; } /** * 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 = ''; $language = $this->getLanguage(); $dec_point = $this->getDecimalPoint($language); $thousands_sep = $this->getThousandsSeparator($language); $tmz = $this->getTimeZone($language); $areacode = $this->getAreaCode($language); switch ($this->style) { case FORMAT_STYLE_PHONE_LONG: // like (+45)40506069 $strip = false; $phone = $this->getFormattedPhoneNumer($areacode, $strip); if (strlen($phone) == 8) { $html .= "(+".$areacode.")"; } $html .= $phone; break; case FORMAT_STYLE_PHONE_SHORT: // like 40506069 $strip = true; $phone = $this->getFormattedPhoneNumer($areacode, $strip); $html .= $phone; break; 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('d-m-Y H:i:s', strtotime($this->text)); if ($this->linebreak) { $html .= str_replace(' '," ", $date); // In order to avoid line breaks } else { $html .= $date; } 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_SQL: $number = $this->text; $html .= $this->reverseNumberFormat($number, FORMAT_STYLE_NUMBER_OF_DIGITS, $dec_point, $thousands_sep); break; case FORMAT_STYLE_MONEY_LONG: case FORMAT_STYLE_DKK_LONG: // DKK 2.062,50 or $number = ($this->text * 100) / 100; $currencyText = ''; if ($this->locale !== '' && $this->locale instanceof Locale ) { $currency = new Currency($this->locale); $amount = $currency->calculate($number); $calculated = number_format($amount, FORMAT_STYLE_NUMBER_OF_DIGITS, $dec_point, $thousands_sep); // EUR 2.062,50// $currencyText .= '('.$currency->getHtml().' '.$calculated.')'; $html .= $currencyText.' DKK '; } // Intentionally fall through case FORMAT_STYLE_MONEY_SIMPLE: case FORMAT_STYLE_DKK_SIMPLE: // 2062,50 case FORMAT_STYLE_MONEY_SHORT: case FORMAT_STYLE_DKK_SHORT: // 2.062,50 $number = ($this->text * 100) / 100; $html .= number_format($number, FORMAT_STYLE_NUMBER_OF_DIGITS, $dec_point, $thousands_sep); if ($this->linebreak) { $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;// case FORMAT_STYLE_IS_ONLINE:// case FORMAT_STYLE_IS_OFFLINE:// $class = $this->style == FORMAT_STYLE_IS_ONLINE ? CSS_GREEN : CSS_RED;// $html .= '<span class="'.$class.'">'.$this->text."</span>\r\n"; // color coding// break; default: $html .= $this->text; // Raw break; } return $html; } /** * TODO figure out, how to do this the right way * This is a problem in the ViewForm.php class, when updating/inserting formatted prices like, 1.234,56 (danish) * Get the reverse of the number_format() function * The following situations may occur * danish numbers 1.234,56 or 34,56 or 1.234 (this is illegal in danish: 1.23) * UK numbers 1,234.56 or 34.56 * @param String $number The number to be converted back to SQL number format * @param int $decimals The number of decimals * @param String $dec_point The decimal point to use [. or ,] * @param String $thousands_sep The thousands seperator to use [, or .] * @return String The SQL number, finally */ private function reverseNumberFormat($number, $noOfDecimals, $dec_point, $thousands_sep) { $html = $number; if (is_array($number)) { // TODO multiple insert of lines is NOT supported right now, like mosegrise, muldvarpe, hvepse @see Product*.php } else { /** * First try to decide if the thousands seperator and decimal point are interchanged * Assume the Locale are the right ones, and the user didn't type interchangeable */ $swap = false; $thousandSeperator = $thousands_sep; $decimalPoint = $dec_point; $posThousandSeperator = strpos($html, $thousandSeperator); $posDecimalPoint = strpos($html, $decimalPoint); if ($posThousandSeperator !== false) { if ($posDecimalPoint !== false) { /** * If the decimal point comes before the thousind seperator, then the format must be * 1,234.56 then swap the direction too */ if ($posDecimalPoint < $posThousandSeperator) { $swap = true; } } else { /** * If NO decimal point but found a thousind seperator, then the format must be * 1. or 1.2 or 1.23 then swap the direction too * But do not swap, if the thousand seperator is located in place * 1.234 (do not swap), assume 1234.00 */ $arrNumber = explode($thousandSeperator, $html); $count = count($arrNumber); $substr = $arrNumber[$count-1]; $length = strlen($substr); if ($length !== 3) { $swap = true; } } } if ($swap) { $thousandSeperator = $dec_point; $decimalPoint = $thousands_sep; } /** * First remove the thousands seperators, if any * Sample pattern: danish, 1.234,56 convert to 1234,56 */ $posThousandSeperator = strpos($html, $thousandSeperator); if ($posThousandSeperator !== false) { $html = str_replace($thousandSeperator, "", $html); } $arrNumber = explode($decimalPoint, $html); $count = count($arrNumber); // Sanity check $pattern = "000000000"; $integer = ""; $decimals = ""; switch ($count) { case 2: // MUST come before '1' $pattern = $arrNumber[1].$pattern; // Intentionally fall through case 1: $decimals = substr($pattern, 0, $noOfDecimals); $integer = $arrNumber[0]; break; default: $decimals = substr($pattern, 0, $noOfDecimals); $integer = $arrNumber[0]; $msg = "Format, More than one decimal point ($decimalPoint) detected in $number, count=$count"; Message::add($msg, __FILE__, __LINE__); break; } if ($integer == '') { $integer = 0; } $html = $integer . "." . $decimals; } 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; * $pattern = ""; * $locale = ""; * $linebreak = true; // Skip linebreak formatting * $newtext = Format::get($text, $style, $pattern, $locale, $linebreak); * 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 * @param boolean $linebreak The linebreak is NOT allowed, if true * @return String The text formatted as specified by style */ public static function get($text, $style='', $pattern='', $locale='', $linebreak='') { $format = new Format($text, $style, $pattern, $locale, $linebreak); return $format->convert(); } /** * Display html * <code> * Usage: * $text = '20081103142103'; * $style = FORMAT_STYLE_DATE_LONG; * $pattern = ""; * $locale = ""; * $linebreak = true; // Skip linebreak formatting * Format::display($text, $style, $pattern, $locale, $linebreak); * </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 * @param boolean $linebreak The linebreak is NOT allowed, if true */ public static function display($text, $style='', $pattern='', $locale='', $linebreak='') { $html = new Format($text, $style, $pattern, $locale, $linebreak); $html->addHtml(); }}?>
Den fulde HTML kildekode for Format klassen
<? <!-- DEBUG: Format --> 03-11-2008 14:21:03 ?>
Her er 'klasse metoderne' for Format klassen:
Her er 'objekt variable' for Format klassen: